About deploy a web project from the local to Ali cloud server, you can not use bug stmp protocol to send mail

I explained the problem and its solution

Recently wrote javaweb useful items to send e-mail function, it will naturally think of using STMP protocol, java also has a corresponding jar package, so soon the successful commissioning locally, but Ali deployed to the cloud server (I was ubuntu16.04) on always being given:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 25;

Baidu after that Ali cloud server is not open 25 ports , it is necessary to (STMP protocol) to change the port to 465 (STMPS protocol) port 25, using the SSL encryption protocol to transfer mail.

Baidu Encyclopedia of STMPS defined as follows:
SMTPS: 465 port for SMTPS (SMTP-over-SSL) protocol open service, which is based on a variant of the protocol SMTP protocol over SSL security protocol, it inherits the SSL security protocol asymmetric encryption is highly secure reliability, prevent the disclosure of e-mail. SMTPS and SMTP protocols, but also to send messages, but safer, prevent messages from being intercepted by hackers leak, but also realize the mail sender non-repudiation capabilities. After prevent the sender from sending delete sent messages, sent a denial of such a message.

Since the problem found, we begin to solve the problem now, after modifying the configuration of the specific code as follows:

//设置SSL加密方式
MailSSLSocketFactory sf=new MailSSLSocketFactory();
sf.setTrustAllHosts(true);

//获取系统属性
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
properties.put("mail.smtp.port", "465");//使用465端口号
properties.put("mail.smtp.auth", "true");//开启认证
properties.put("mail.smtp.ssl.enable", "true");//启用ssl安全协议
properties.put("mail.smtp.ssl.socketFactory", sf);//设置ssl配置
properties.put("mail.debug", "true");// 显示debug信息

Later encounter a problem, is my mail.jar bag no MailSSLSocketFactory this class, think of this jar package two years ago to do is download the game, it may be too old version, download the latest version of the final in the github jar package, you can send messages correctly. The latest version of the jar package links:
JavaMail's github address

The complete code

/**
* @author Author:lhy
* @version created on :2019年12月18日 下午8:43:35
*/
package utils;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

import com.sun.mail.util.MailSSLSocketFactory;
 
public class EmailUtil {
	 
		private static String from = "[email protected]"; // 发件人邮箱地址
		private static String user = "咕咕单车"; // 发件人称号
		private static String password = "你的stmp授权码"; // 发件人邮箱客户端授权码,不是邮箱密码!!!
		
		/* 发送邮件 */
		public static boolean sendMail(String to, String text, String title) {
			try { 
					MailSSLSocketFactory sf=new MailSSLSocketFactory();
					sf.setTrustAllHosts(true);
					
					//获取系统属性
				  	Properties properties = new Properties();
			        
				  	properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
				  	properties.put("mail.smtp.port", "465");//使用465端口号
				  	properties.put("mail.smtp.auth", "true");//开启认证
				  	properties.put("mail.smtp.ssl.enable", "true");//允许使用ssl加密传输
				  	properties.put("mail.smtp.ssl.socketFactory", sf);
				  	properties.put("mail.debug", "true");// 显示debug信息
				  	
			        // 得到会话对象
			        Session session = Session.getInstance(properties);
			        // 获取邮件对象
			        Message message = new MimeMessage(session);
			        // 设置发件人邮箱地址
			        message.setFrom(new InternetAddress(from));
			        // 设置收件人邮箱地址,一次向多个邮箱发送
			        //message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("[email protected]"),new InternetAddress("[email protected]"),new InternetAddress("[email protected]")});
			        // 设置收件人邮箱地址,一次向一个邮箱发送
			        message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));//一个收件人
			        // 设置邮件标题
			        message.setSubject(title);
			        // 设置邮件内容
			        message.setText(text);
			        // 得到邮差对象
			        Transport transport = session.getTransport();
			        // 连接自己的邮箱账户
			        transport.connect(from, password);// 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
			        // 发送邮件
			        transport.sendMessage(message, message.getAllRecipients());
			        // 关闭资源
			        transport.close();
			        return true;
			}catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				return false;
			}
		}
	 
		public static void main(String[] args) { // 做测试用
			sendMail("[email protected]", "机器学习是一门多领域交叉学科,涉及概率论、统计学、逼近论、凸分析、算法复杂度理论等多门学科。专门研究计算机怎样模拟或实现人类的学习行为,以获取新的知识或技能,重新组", "什么是机器学习?");
		}
}
Published 61 original articles · won praise 16 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41427568/article/details/103753323