When using Java Mail to send emails, the exception "Could not connect to SMTP host: [email protected], port: 25" may be thrown.

question

When using javamail to send emails, an error occurs:

come mex:javax.mail.MessagingException: Could not connect to SMTP host: xxx.xxx.com.cn, port: 25;
  nested exception is:
	java.net.ConnectException: Connection timed out (Connection timed out)

solve

The server operator has blocked port 25! (Alibaba Cloud's port 25 is blocked by default)

Therefore, in response to this situation, I changed the port to port 465 and added SSL verification!

Code configuration

        
        //输入参数:String host 邮件服务器地址名称
        //输入参数:String port 邮件服务器地址端口
        //输入参数:Boolean isSsl 是否使用安全连接(SSL)

        // 构造mail session
        Properties props = new Properties() ;
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.socketFactory.class", "javax.NET.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.port", port);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.ssl.enable",isSsl);

————————————————————————————————

Extension-mail service port (original link: https://blog.csdn.net/u012424783/article/details/79429621 )


Port 25 (SMTP) : Port 25 is opened by the SMTP (Simple Mail Transfer Protocol) service and is used for sending emails.

The vast majority of mail servers today use this protocol. When you send an email to someone else, a certain dynamic port (greater than 1024) of your machine will establish a connection with port 25 of the mail server. The email you send will be transmitted to the mail server through this connection and saved. .

Port 109 (POP2) : Port 109 is open for the POP2 (Post Office Protocol Version 2, Post Office Protocol 2) service and is used to receive mail.

Port 110 (POP3) : Port 110 is open for POP3 (Post Office Protocol Version 3, Post Office Protocol 3) service and is used to receive mail.

Port 143 (IMAP) : Port 143 is open for the IMAP (INTERNET MESSAGE ACCESS PROTOCOL) service and is used to receive mail.

At present, POP3 is much more widely used than POP2. POP2 has almost been eliminated. There are also some servers that support both POP2 and POP3 protocols. The client can use the POP3 protocol to access the server's mail service. Nowadays, most of the ISP's mail servers use the POP3 protocol (the POP2 protocol is rarely used). When using the mail client program, you will be asked to enter the POP3 server address. By default, port 110 is used. When you log in with an email client (such as Thunderbird, foxmail, MS Outlook Express and various mail wizards), your machine will automatically use a dynamic port (greater than 1024) of the machine to connect to the 110 port of the mail server. Just send the emails sent to you by others (previously saved on the mail server) to your machine, so that you can see the new emails in the inbox on your client tool.

The IMAP protocol, like the POP3 protocol, is used to receive emails, but it has its own special and novel features. It is user-oriented. The main difference between it and the POP3 protocol is that users do not need to download all email content. , but only downloads basic information such as the email title and sender. The user can decide whether to download the full text of the email based on the basic information such as the title. The user can directly operate the email on the server through the client's browser (for example: open and read Full text, throw into trash, permanently delete, organize into a folder, archive,). To put it simply: the browser uses the IMAP protocol (port 143) to receive emails for you and allow you to conveniently operate emails on the server. The mail client uses the POP3 protocol (port 110) to save all the information and full-text content of the mail you receive to your local machine as a copy. Any operations you perform on the copied mail on the mail client are performed on the copy. Do not interfere with the original emails saved for you on the mail server.

The SMTP protocol, POP2 protocol, POP3 protocol, and IMAP protocol introduced above are all unsafe protocols. Taking into account network security factors, the following will introduce to you a secure email sending and receiving protocol based on the SSL (Secure Sockets Layer) protocol. Your email may be intercepted by cyber hackers during the transmission process. If your email is very confidential and you do not want it to be intercepted by anyone other than the recipient or any hacker, or it involves national security, etc. Then your emails should not be sent and received using the above three protocols.

If you use the SMTP protocol to send emails, the emails you send may be intercepted by hackers and leaked when they are transmitted from your machine to the server. ****If you use the POP2 or POP3 protocol to receive emails, your emails may be intercepted by hackers and leaked as they are transferred from the server to your current machine.

Port 465 (SMTPS) : Port 465 is open for the SMTPS (SMTP-over-SSL) protocol service. This is a variant of the SMTP protocol based on the SSL security protocol. It inherits the asymmetric encryption of the SSL security protocol. Highly secure and reliable to prevent email leakage. SMTPS, like the SMTP protocol, is also used to send emails, but it is more secure to prevent emails from being intercepted and leaked by hackers, and it can also implement the anti-repudiation function of the email sender. Prevents the sender from deleting the sent email after sending it and refusing to admit that such an email was sent.

Port 995 (POP3S) : Port 995 is open for the POP3S (POP3-over-SSL) protocol service. This is a variant of the POP3 protocol based on the SSL security protocol. It inherits the asymmetric encryption of the SSL security protocol. Highly secure and reliable to prevent email leakage. POP3S, like the POP3 protocol, is also used to receive emails, but it is more secure to prevent emails from being intercepted and leaked by hackers, and it can also implement the non-repudiation function of the email recipient. Prevent the recipient from deleting the received email after receiving it and refusing to acknowledge receipt of such an email.

Port 993 (IMAPS) : Port 993 is open for the IMAPS (IMAP-over-SSL) protocol service. This is a variant of the IMAP protocol based on the SSL security protocol. It inherits the asymmetric encryption of the SSL security protocol. Highly secure and reliable to prevent email leakage. IMAPS, like the IMAP protocol, is also used to receive emails, but it is more secure to prevent emails from being intercepted and leaked by hackers, and it can also implement the non-repudiation function of the email recipient. Prevent the recipient from deleting the received email after receiving it and refusing to acknowledge receipt of such an email.
——————————————————————————————

Guess you like

Origin blog.csdn.net/qq_40861800/article/details/123042779