admin管理员组文章数量:1532012
阿里云JavaMail发送邮报错 No appropriate protocol
错误 Could not connect to SMTP host: smtp.qq, port: 465
错误 Caused by: javax.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
阿里云把邮件端口25给屏蔽了,需要换端口为465 另外需要ssl加密,但是阿里云的openjdk1.8不支持,需要替换为原始的jdk1.8亲自测试有效
邮件代码
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.util.ByteArrayDataSource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Properties;
/**
* @className SendMailUtil
* @description
* @author cong.huo
* @date 2020/10/29 2:48 下午
* @version V1.0
**/
@Slf4j
@Component
public class MailUtil {
/**
* 发件人
**/
@Value("${spring.mail.username}")
private String username;
/**
* 邮件发送的服务器host
**/
@Value("${spring.mail.host}")
private String host;
/**
* 邮件发送的服务器密码
**/
@Value("${spring.mail.password}")
private String password;
/**
* 邮件发送的服务器密码
**/
@Value("${spring.mail.port}")
private String port;
public boolean sendMailUtil(ByteArrayOutputStream byteArrayOutputStream , String toEmail,String fileName,String subject,String text) {
try {
// 配置发送邮件的环境属性
final Properties props = new Properties();
// 表示SMTP发送邮件,需要进行身份验证
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", host);
// props.put("mail.smtp.port", ALIDM_SMTP_PORT);
// 如果使用ssl,则去掉使用25端口的配置,进行如下配置,
props.put("mail.smtp.socketFactory.class","javax.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.port", port);
// 发件人的账号
props.put("mail.user", username);
// 访问SMTP服务时需要提供的密码
props.put("mail.password", password);
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
@Override
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);
Message message = new MimeMessage(mailSession);
//防止邮件被当然垃圾邮件处理,披上Outlook的马甲
message.addHeader("X-Mailer","Microsoft Outlook Express 6.00.2900.2869");
message.setFrom(new InternetAddress(username));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
//邮件标题
message.setSubject(subject);
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setText(text);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(bodyPart);
//附件
bodyPart = new MimeBodyPart()</
版权声明:本文标题:阿里云JavaMail发送邮报错 No appropriate protocol 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1725958881a1050662.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论