在之前一篇博客中:通过javax.mail实现java程序发送邮件功能(示例QQ邮箱),通过javax.mail确实可以做到发送简单邮件的功能,但是当我们将它部署在阿里云服务器中(这里我使用的是阿里云的轻量级服务器),就会莫名出现Could not connect to SMTP host: smtp.qq.com, port: 25, response: -1这样的错误。
在网上找了比较多的技术贴,发现是阿里云服务器将25端口禁用了。至于为什么禁用25端口,答案好像是ECS基于安全考虑,对垃圾邮箱进行了管理和规范,才禁用的。。等等。。。。。具体不详谈了。
当然能封禁25端口,也就可以解封啦,下面呈上解封教程地址:https://yq.aliyun.com/articles/701303
除了解封,也可以考虑SSL加密SMTP通过465或者587端口进行发件。我这边是通过465端口成功了,没有尝试587,如果465不成功小伙伴也可以考虑587来实现。这样阿里云是不会拦截的。
怎样使用呢? 需要给你的程序中加入以下代码(我这里使用的是QQ邮箱,其他邮箱大致类似):
//使用SSL加密SMTP通过465端口进行邮件发送
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable","true");
props.put("mail.smtp.ssl.socketFactory",sf);
下面贴上完整实现代码:
import com.sun.mail.util.MailSSLSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;
@Service
public class MailSender {
private static final Logger logger = LoggerFactory.getLogger(MailSender.class);
/**
* @param strMail 收件人
* @param strTitle 邮件标题
* @param strText 邮件内容
* @return
*/
public boolean sendEmail(String strMail, String strTitle, String strText){
boolean bret = false;
try {
final Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");
//使用SSL加密SMTP通过465端口进行邮件发送
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable","true");
props.put("mail.smtp.ssl.socketFactory",sf);
//你自己的邮箱
props.put("mail.user", "xxxxxx@qq.com");
//你开启pop3/smtp时的验证码
props.put("mail.password", "xxxxxxxx");
//此时将端口设置为465
props.put("mail.smtp.port", "465");
props.put("mail.smtp.starttls.enable", "true");
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
String username = props.getProperty("mail.user");
InternetAddress form = new InternetAddress(username);
message.setFrom(form);
InternetAddress to = new InternetAddress(strMail);
message.setRecipient(Message.RecipientType.TO, to);
// 设置邮件标题
message.setSubject(strTitle);
// 设置邮件的内容体
message.setContent(strText, "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
bret = true;
} catch (MessagingException e) {
logger.error("发送邮件失败:"+e.getMessage());
} catch (GeneralSecurityException e) {
logger.error("发送邮件失败:"+e.getMessage());
}
return bret;
}
}
接下来,部署服务器,启动。。。。。。OK问题解决!
在阿里云服务器上使用javax.mail发送邮件时遇到Could not connect to SMTP host: smtp.qq.com, port: 25的错误。原因是阿里云为防止垃圾邮件禁用了25端口。解决方法包括解封25端口或使用SSL加密通过465或587端口发送。本文提供了一种通过465端口成功发送邮件的Java代码示例。"
131075198,17348508,C#死锁深度解析:原理、预防与排查,"['C#编程', '多线程', '死锁处理', '软件调试', '并发编程']

7648

被折叠的 条评论
为什么被折叠?



