最初的程序是这样的:
package com.zhyea.zytools; import java.util.Date; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class MailSender { private static final String MAIL_SMTP_HOST = "smtp.exmail.qq.com"; private static final Integer MAIL_SMTP_PORT = 587; private static final Boolean MAIL_SMTP_AUTH = true; private static final String MAIL_SMTP_USER = "robin@zhyea.com"; private static final String MAIL_SMTP_PASSWORD = "robinzhyea"; private static Propertiesprops = new Properties(); static { props.put("mail.smtp.host", MAIL_SMTP_HOST); props.put("mail.smtp.auth", MAIL_SMTP_AUTH); props.put("mail.smtp.user", MAIL_SMTP_USER); props.put("mail.smtp.password", MAIL_SMTP_PASSWORD); } /** * 发送邮件 */ public static void send(String to, String title, String content) { try { Sessionsession = Session.getInstance(props);//创建邮件会话 MimeMessagemessage = new MimeMessage(session);//由邮件会话新建一个消息对象 message.setFrom(new InternetAddress(MAIL_SMTP_PASSWORD));//设置发件人的地址 message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));//设置收件人,并设置其接收类型为TO //设置信件内容 //message.setText(mailContent); //发送 纯文本 邮件 TODO message.setSubject(title);//设置标题 message.setContent(content, "text/html;charset=gbk"); //发送HTML邮件,内容样式比较丰富 message.setSentDate(new Date());//设置发信时间 message.saveChanges();//存储邮件信息 //发送邮件 Transporttransport = session.getTransport(); transport.connect(MAIL_SMTP_USER, MAIL_SMTP_PASSWORD); transport.sendMessage(message, message.getAllRecipients());//发送邮件,其中第二个参数是所有已设好的收件人地址 transport.close(); } catch (Exception e) { e.printStackTrace(); } } }
这段程序使用的腾讯提供的邮件服务,执行起来没有任何问题出现。后来修改“mail.smtp.host”为微软提供office365邮件支持服务后,却连续遇到了几个问题。
异常信息如下:
Exceptionin thread "main" com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Clientwasnot authenticatedto sendanonymousmailduringMAILFROM
这个解决起来比较容易。找了些资料,添加如下配置即可:
mail.smtp.starttls.enable = true
异常信息如下:
Exceptionin thread "main" javax.mail.NoSuchProviderException: Invalidprotocol: null atjavax.mail.Session.getProvider(Session.java:449) atjavax.mail.Session.getTransport(Session.java:667) atjavax.mail.Session.getTransport(Session.java:648) atjavax.mail.Session.getTransport(Session.java:634)
这个问题是在将应用部署到生产环境后才遇到的。经检查后确认是应用使用的jar包和容器(就是jetty)使用的jar包冲突了。容器使用的jar版本较旧,不过默认优先加载容器的jar,这样问题解决思路有两个:
第二个选择多少有些危险,就采用第一个选项好了,只需要修改一行即可:
Transporttransport = session.getTransport("smtp");
修改后的程序:
package com . zhyea . zytools ; import java . util . Date ; import java . util . Properties ; import javax . mail . Message ; import javax . mail . Session ; import javax . mail . Transport ; import javax . mail . internet . InternetAddress ; import javax . mail . internet . MimeMessage ; public class MailSender { private static final String MAIL_SMTP_HOST = "smtp.exmail.qq.com" ; private static final Integer MAIL_SMTP_PORT = 587 ; private static final Boolean MAIL_SMTP_AUTH = true ; private static final String MAIL_SMTP_USER = "robin@zhyea.com" ; private static final String MAIL_SMTP_PASSWORD = "robinzhyea" ; private static Properties props = new Properties ( ) ; static { props . put ( "mail.smtp.host" , MAIL_SMTP_HOST ) ; props . put ( "mail.smtp.auth" , MAIL_SMTP_AUTH ) ; props . put ( "mail.smtp.user" , MAIL_SMTP_USER ) ; props . put ( "mail.smtp.password" , MAIL_SMTP_PASSWORD ) ; props . put ( "mail.smtp.starttls.enable" , true ) ; } /** * 发送邮件 */ public static void send ( String to , String title , String content ) { try { Session session = Session . getInstance ( props ) ; //创建邮件会话 MimeMessage message = new MimeMessage(session);//由邮件会话新建一个消息对象 message.setFrom(new InternetAddress(MAIL_SMTP_PASSWORD));//设置发件人的地址 message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));//设置收件人,并设置其接收类型为TO //设置信件内容 //message.setText(mailContent); //发送 纯文本 邮件 TODO message.setSubject(title);//设置标题 message.setContent(content, "text/html;charset=gbk"); //发送HTML邮件,内容样式比较丰富 message.setSentDate(new Date());//设置发信时间 message.saveChanges();//存储邮件信息 //发送邮件 Transport transport = session.getTransport("smtp"); transport.connect(MAIL_SMTP_USER, MAIL_SMTP_PASSWORD); transport.sendMessage(message, message.getAllRecipients());//发送邮件,其中第二个参数是所有已设好的收件人地址 transport.close(); } catch (Exception e) { e.printStackTrace(); } } }
##