JavaMail实现邮件的发送

1,拷贝mail.jar 和activation.jar到项目中

2,开启邮箱的 POP3/SMTP服务,以QQ邮箱为例

进去QQ邮箱-->设置-->账号-->进行设置如下图

注意:开启完之后,QQ 邮箱会生成一个授权码,在代码里连接邮箱使用这个授权码而不是原始的邮箱密码,这样可以避免使用明文密码。








完整代码示例

public class MailTool {
  public static void main(String[] args) throws MessagingException, GeneralSecurityException {
    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 = new MailSSLSocketFactory();
    sf.setTrustAllHosts(true);
    props.put("mail.smtp.ssl.enable", "true");
    props.put("mail.smtp.ssl.socketFactory", sf);
 
    Session session = Session.getInstance(props);
 
    Message msg = new MimeMessage(session);
    msg.setSubject("seenews 错误");
    StringBuilder builder = new StringBuilder();
    builder.append("url = " + "http://blog.csdn.net/never_cxb/article/details/50524571");
    builder.append("
页面爬虫错误");
    builder.append("
时间 " + TimeTool.getCurrentTime());
    msg.setText(builder.toString());
    msg.setFrom(new InternetAddress("**发送人的邮箱地址**"));
 
    Transport transport = session.getTransport();
    transport.connect("smtp.qq.com", "**发送人的邮箱地址**", "**你的邮箱密码或者授权码**");
 
    transport.sendMessage(msg, new Address[] { new InternetAddress("**接收人的邮箱地址**") });
    transport.close();
  }
}



开启 SSL 加密

网上搜了一下,其他比如163、新浪邮箱不需要 SSL 加密,可以放弃 QQ 邮箱。

网上还有种说法,把 smtp.qq.com 换成 smtp.exmail.qq.com也不需要 SSL加密,但是笔者没有run成功。所以还是老老实实加上 SSL 加密吧。

下面的代码开启了 SSL 加密


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







注意:如果发送内容有敏感词汇,QQ会自动把邮件放入垃圾箱。


下面是使用163邮箱发送邮件的代码示例:


public static void sendEmail(User user) 
			throws MessagingException, GeneralSecurityException {
		Properties prop = new Properties();
        prop.setProperty("mail.transport.protocol", "smtp");
        prop.setProperty("mail.smtp.host", "smtp.163.com");
        prop.setProperty("mail.smtp.auth", "true");
        prop.setProperty("mail.debug", "true");
        Authenticator authenticator = new PopAuthenticator("邮箱登录用户名", "授权码");
             
        //创建会话
        Session session = Session.getInstance(prop,authenticator);
        //填写信封写信
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("********@163.com"));   //发件人
        msg.setRecipient(RecipientType.TO, new InternetAddress(user.getEmail()));   //收件人,可以是任意的邮箱账号,可以不是163邮箱
        msg.setSubject("时尚轻纺");
        msg.setText("尊敬的"+user.getUsername()+"用户,您好,********");
        //验证用户名密码发送邮件
        Transport transport = session.getTransport();
        transport.send(msg);
	}



注意:当项目发布到云服务器的时候,我是发布到腾讯云,不能使用域名  "smtp.163.com"  连接到163邮箱服务器,这时候要使用IP地址连接



修改为  prop.setProperty("mail.smtp.host", "220.181.12.13"); 

这样云服务器就可以连上邮箱服务器了。


参考文章:http://blog.csdn.net/lcy_dandan/article/details/46380933


原文地址:https://www.cnblogs.com/molashaonian/p/7242045.html