发送邮件发生的一些常见异常:
530 Error: A secure connection is requiered(such as ssl)错误
原因是你没有设置qq的加密方式,加上这句就OK了
props.put(“mail.smtp.ssl.enable”, “true”);//QQ邮箱的SSL加密
501 mail from address must be same as authorization user
这是因为你的fromEmail(发件人)和tran.connect(邮件发送对象)使用的邮箱不一致,解决办法:保持一致
535 Error:ÇëʹÓÃÊÚȨÂëµÇ¼¡£ÏêÇéÇë¿´http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
代码如下:
Properties prop = new Properties();
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(prop);
session.setDebug(true);
Transport ts = session.getTransport();
ts.connect("smtp."+eMap.get("fromEmailprotocol").toString(), eMap.get("fromEmailUser").toString(), eMap.get("fromEmailPassword").toString());
完整的异常日志如下:
javax.mail.AuthenticationFailedException: 535 Error: ÇëʹÓÃÊÚȨÂëµÇ¼¡£ÏêÇéÇë¿´: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
at javax.mail.Service.connect(Service.java:345)
at javax.mail.Service.connect(Service.java:226)
at net.northking.util.EmailUtil.sendEmails(EmailUtil.java:177)
at net.northking.controller.info.AutoSendMessage.demandWorkFlowMessage(AutoSendMessage.java:1289)
at net.northking.controller.demand.MisDemandController.sendEmail(MisDemandController.java:523)
连接邮件对象使用的不是16位的授权码,得到授权码赋值:
调用qq邮箱 来发送邮件,我们需要开启POP3/SMTP服务,这时qq邮件会让我们设置客户端授权码,这个授权码替代上面代码部分的fromEmailPassword
即可成功发送邮件
java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
这是因为你导入的包只有api,没有包含sun的实现,重新导入javax.mail包即可。
网友评论