基于JavaMail向邮箱发送邮件

参考:http://blog.csdn.net/ghsau/article/details/17839983

http://blog.csdn.net/never_cxb/article/details/50543289


最近想写一个注册界面,好多的网站注册的时候需要填写邮箱,向邮箱发一个验证链接,怎么实现向邮箱发送验证邮件呢?

Java提供了一个编写邮件,搭建连接,发送邮件的jar包,JavaMail提供了操作的所有工具,我们只需要简单的调用,设置参数,就可以实现Java发送邮件

jar包下载链接:http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html#javamail-1.4.7-oth-JPR

下载之后解压里面有一个mail.jar,添加到项目里。


示例代码:

public static void sendMail (){
        Properties props = new Properties();
        // 开启debug调试
        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", "smtp.qq.com");
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");

        MailSSLSocketFactory sf = null;
        Message msg = null;
        Transport transport = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.ssl.socketFactory", sf);

            // 设置环境信息
            Session session = Session.getInstance(props);

            // 创建邮件对象
            msg = new MimeMessage(session);
            msg.setSubject("JavaMail测试");
            // 设置邮件内容
            msg.setText("这是一封由JavaMail发送的邮件!");
            // 设置发件人
            msg.setFrom(new InternetAddress("19583219822@qq.com"));

            transport = session.getTransport();
            // 连接邮件服务器
            transport.connect("发件邮箱", "发件邮箱登录授权码");
            // 发送邮件
            transport.sendMessage(msg, new Address[] {new InternetAddress("收件邮箱")});
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }finally {
         // 关闭连接
            try {
                transport.close();
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
}


在第三方客户端登录时,密码框请输入授权码

获取授权码的方法(QQ邮箱):登录到qq邮箱找到设置


点击账户

找到POP3服务,点击开启,之后经过验证就可以得到一个授权码


实际测试中发现向qq邮箱中发送邮件程序总会报错,后来经过百度到大神的文章,才知道 QQ 邮箱需要 SSL 加密。

开启 SSL 加密,其他比如163就不需要 SSL 加密……

既然需要加密就加上SSL加密的代码:

MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);

完整代码:

public static void sendMail (){

        Properties props = new Properties();

        // 开启debug调试
        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", "smtp.qq.com");
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");

        MailSSLSocketFactory sf = null;
        Message msg = null;
        Transport transport = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.ssl.socketFactory", sf);

            Session session = Session.getInstance(props);

            msg = new MimeMessage(session);
            msg.setSubject("标题");
            // 设置邮件内容
            msg.setText("邮件内容………………………………");
            msg.setFrom(new InternetAddress("发件邮箱"));

            transport = session.getTransport();
            transport.connect("smtp.qq.com", "发件邮箱", "授权码");

            transport.sendMessage(msg, new Address[] { new InternetAddress("收件邮箱") });
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }finally {
            try {
                transport.close();
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    }


前面的代码固定,参数也是固定的,其实也很好理解,搭建链接,设置参数,设置邮件内容,发送

运行成功后控制台显示:


为了验证程序,作者注册了一个163邮箱,用QQ邮箱向163邮箱发送邮件,实测是可以接收到的。


原文地址:https://www.cnblogs.com/duzhentong/p/7816577.html