javaMail学习(六)——使用javaMail给QQ邮箱账户发简单邮件时碰到的问题

在使用JavaMail给QQ邮箱发邮件的过程中虽然碰到了很多问题,但是总结起来大致可以分成一下几类:

第一类:smtp 服 务 未开 启(没有 授 权 码)

因为在用代码发邮件之前,我用的Foxmail添加了QQ邮箱账户,添加的时候报错:-ERR Please using authorized code to login. More information at http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

解决办法自然是开 启 服 务且获取 授 权 码

 

第二类:邮箱账户错误、授权码错误、smpt服务器地址错误类。

症状有以下几个:

1、Invalid Addresses

javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 [email protected] relay mail disabled.(from "[email protected]" to "[email protected]" via "wjl.com") 

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1607)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:887)
at javax.mail.Transport.send0(Transport.java:191)
at javax.mail.Transport.send(Transport.java:120)
at com.wjl.mail.utils.MailUtils.sendMain(MailUtils.java:64)
at com.wjl.mail.utils.MailUtils.main(MailUtils.java:71)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 [email protected] relay mail disabled.(from "[email protected]" to "[email protected]" via "wjl.com") 

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1458)
... 5 more

 我这个错误是smtp.host配置错误,因为本地的不用配置,而使用Q Q 邮 箱 是需要配置的。

2、535 Error: ��ʹ����Ȩ���¼�������뿴

535 Error: ��ʹ����Ȩ���¼�������뿴: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
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:648)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:583)
	at javax.mail.Service.connect(Service.java:313)
	at javax.mail.Service.connect(Service.java:172)
	at javax.mail.Service.connect(Service.java:121)
	at javax.mail.Transport.send0(Transport.java:190)
	at javax.mail.Transport.send(Transport.java:120)
	at com.wjl.mail.utils.MailUtils.sendMain(MailUtils.java:76)
	at com.wjl.mail.utils.MailUtils.main(MailUtils.java:83)

异常代码中的连接指向的是获取QQ邮箱 授 权 码,顾名思义这是授 权 码 错 误

值得一提的是:

当mail.host写成smtp.exmail.qq.com时它也会报这个错,所以host必须写成smtp.qq.com。

获取授权码时QQ虽说一个邮箱账号可以拥有多个授权码,但是据本人测试,只有最近一个才有效。我开始申请了一个授权码,使用JavaMail发邮件能成功,后来授权码忘记了又申请了一个,使用老的授权码发送邮件失败,报错也是535Error,换成新的之后才发送成功

因此一定要确保:QQ邮箱账户与授权码配套,授权码使用最新的,mail.host使用smtp.qq.com

 

第三类:端口号错误

QQ邮箱的SMTP服务器官方上说的端口号465或587(查看)
javaMail学习(六)——使用javaMail给QQ邮箱账户发简单邮件时碰到的问题_第1张图片

当端口号设置为587时将报错:Could not connect to SMTP host: smtp.qq.com, port: 587;

javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 587;
nested exception is:
	javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
	at javax.mail.Service.connect(Service.java:291)
	at javax.mail.Service.connect(Service.java:172)
	at javax.mail.Service.connect(Service.java:192)
	at com.wjl.mail.utils.MailUtils.sendMain(MailUtils.java:92)
	at com.wjl.mail.utils.MailUtils.main(MailUtils.java:104)
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
	at com.sun.net.ssl.internal.ssl.InputRecord.handleUnknownRecord(InputRecord.java:652)
	at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:484)
	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:863)
	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1188)
	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1215)
	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1199)
	at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:503)
	at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:234)
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
	... 6 more

因此还是使用465比较好。

 

第四类:未开 启 SSL加 密

症状有好几个:

1、530 Error: A secure connection is requiered(such as ssl)

2、debug中最后一行:"DEBUG SMTP: trying to connect to host "smtp.qq.com", port 465, isSSL false",一直都是这一行,程序也没有停止,一直都是这一行

3、javax.mail.AuthenticationFailedException: 220 Ready to start TLS

4、使用Foxmail添加QQ账户时,若不勾选"SSL",点击“创建”按钮后就一直处于“验证中.....”

解决办法:

Foxmail是勾选SSL,

javaMail中是添加如下代码:

//开 启SSL加密,否则会失败
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);

 

第五类:莫名其妙类

症状:jks not found

java.security.KeyStoreException: problem accessing trust storejava.security.KeyStoreException: jks not found
	at com.sun.net.ssl.internal.ssl.TrustManagerFactoryImpl.engineInit(Unknown Source)
	at javax.net.ssl.TrustManagerFactory.init(Unknown Source)
	at com.sun.mail.util.MailSSLSocketFactory$MailTrustManager.(MailSSLSocketFactory.java:329)
	at com.sun.mail.util.MailSSLSocketFactory$MailTrustManager.(MailSSLSocketFactory.java:319)
	at com.sun.mail.util.MailSSLSocketFactory.(MailSSLSocketFactory.java:111)
	at com.sun.mail.util.MailSSLSocketFactory.(MailSSLSocketFactory.java:90)
	at com.wjl.mail.utils.SendSimpleMail.(SendSimpleMail.java:52)

为什么说是莫名其妙啊?因为这类问题在此处感觉就不应该出现,可就是出来了,很莫名其妙。

解决办法也很怪:重装jdk或者换jdk。我这里是换的jdk。我这个项目的JRE System Library先前用的jre6(jre目录:D:\Java\jre6),报这个错,后来改成jdk(JDK安装目录:D:\Java\jdk1.6.0_45)就不报错了,再换成jre6依旧报错。

你可能感兴趣的:(javaMail学习(六)——使用javaMail给QQ邮箱账户发简单邮件时碰到的问题)