阿里云出于安全考虑默认关闭25端口,开发的是465端口,我们只讲怎么发送邮件。
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.18</version>
</dependency>
12345678910
demo代码:
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import org.springframework.stereotype.Component;
import com.sun.mail.util.MailSSLSocketFactory;
//关键代码
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
MailAccount account = new MailAccount();
account.setHost("smtp.mxhichina.com");
account.setPort(465);
account.setAuth(true);
account.setSslEnable(true);
//发送邮件的邮箱
account.setFrom("[email protected]");
//发送邮件的邮箱
account.setUser("[email protected]");
//发送邮件的邮箱密码
account.setPass("xxxxxxxxxxxxxx");
account.setCharset(CharsetUtil.CHARSET_UTF_8);
account.setCustomProperty("mail.smtp.ssl.socketFactory", sf);
MailUtil.send(account,
CollUtil.newArrayList(“接收邮件的邮箱,如:[email protected]”),
"标题提示文字",
“正文HTML内容文字”,
true);
标签:25,account,Java,端口,hutool,mail,import,com,javax
From: https://blog.51cto.com/u_16265692/7872112