javaMail learning (6) - problems encountered when using javaMail to send simple emails to QQ mailbox accounts

Although there are many problems in the process of using JavaMail to send emails to QQ mailboxes, they can be roughly divided into the following categories:

The first category: smtp service is not enabled (no authorization code) :

Because before using the code to send emails, I used Foxmail to add a QQ mailbox account , and an error was reported when adding: -ERR Please use authorized code to login . More information at http://service.mail.qq.com/cgi-bin /help?subtype=1&&id=28&&no=1001256

The solution is naturally to start the service and obtain the authorization code .

 

The second category: wrong email account, wrong authorization code, wrong smpt server address.

Symptoms are as follows:

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

 My error is that smtp.host is misconfigured , because the local one does not need to be configured, and the QQ mailbox needs to be configured.

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)

The connection in the exception code points to obtaining the QQ mailbox authorization code. As the name suggests, this is an authorization code error .

It is worth mentioning that:

It will also report this error when mail.host is written as smtp.exmail.qq.com, so host must be written as smtp.qq.com.

When obtaining the authorization code, QQ said that one email account can have multiple authorization codes , but according to my test, only the latest one is valid . I started to apply for an authorization code, and I could send emails successfully using JavaMail. Later, I forgot the authorization code and applied for another one. I failed to send emails using the old authorization code, and the error was also 535Error .

Therefore, make sure that: QQ mailbox account is matched with the authorization code, the authorization code is the latest, and the mail.host is smtp.qq.com .

 

The third category: the port number is wrong .

The official port number of the SMTP server of QQ mailbox is 465 or 587 ( View )

When the port number is set to 587, an error will be reported: 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

So it is better to use 465.

 

Category 4: SSL encryption is not enabled .

There are several symptoms:

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

2. The last line in debug: " DEBUG SMTP: trying to connect to host "smtp.qq.com", port 465, isSSL false ", this line has always been the same, the program has not stopped, it has always been this line

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

4. When using Foxmail to add a QQ account, if you do not check "SSL", click the "Create" button and it will always be "Verifying..."

Solution:

Foxmail is checking SSL,

The following code is added to javaMail:

//Enable SSL encryption, otherwise it will fail
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);

 

Category 5: Inexplicable category

Symptoms: 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.<init>(MailSSLSocketFactory.java:329)
	at com.sun.mail.util.MailSSLSocketFactory$MailTrustManager.<init>(MailSSLSocketFactory.java:319)
	at com.sun.mail.util.MailSSLSocketFactory.<init>(MailSSLSocketFactory.java:111)
	at com.sun.mail.util.MailSSLSocketFactory.<init>(MailSSLSocketFactory.java:90)
	at com.wjl.mail.utils.SendSimpleMail.<clinit>(SendSimpleMail.java:52)

Why do you say it's weird? Because this kind of problem should not appear here, but it just came out, which is very inexplicable.

The solution is also very strange: reinstall jdk or change jdk . I am changing jdk here. The JRE System Library of my project previously used jre6 (jre directory: D:\Java\jre6), and I reported this error, and later changed it to jdk (JDK installation directory: D:\Java\jdk1.6.0_45) and no error was reported. , and then change to jre6 and still report an error.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326366380&siteId=291194637