mail No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
程序员文章站
2022-03-04 13:17:09
...
No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.5</version>
</dependency>
oracle jdk_8u291 使用 mail.1.4.5 smtp 发送邮件 报错:No appropriate protocol (protocol is disabled or cipher suites are inappropriate),发送方式比较常规
private boolean sendMailSmtp(SendMailParam param) {
// 返回值
boolean result = true;
try {
// 服务器地址
String mailServer = param.getMailServer();
Long port = param.getPort();
final String from = param.getFrom();
final String authCode = param.getAuthCode();
String subject = param.getSubject();
String content = param.getContent();
List<String> to = param.getTo();
List<String> cc = param.getCc();
List<String> bcc = param.getBcc();
List<AttachFile> attachFiles = param.getAttachFiles();
boolean ssl = param.isSsl();
boolean debug = param.isDebug();
Properties props = new Properties();
props.setProperty("mail.smtp.host", mailServer);
props.setProperty("mail.transport.protocol", AgreementType.SMTP.name().toLowerCase());
props.setProperty("mail.smtp.auth", "true");
if (port != null) {
props.setProperty("mail.smtp.port", port.toString());
}
if (ssl) {
// SSL加密
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
}
// 配置发送地址的授权码
Authenticator authenticator = new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, authCode);
}
};
// 设置收发地址
Session session = Session.getDefaultInstance(props, authenticator);
session.setDebug(debug);
Message message = new MimeMessage(session);
//发件人
message.setFrom(new InternetAddress(from));
//主题
message.setSubject(subject);
//收件人
InternetAddress[] mailToAddrs = new InternetAddress[to.size()];
for (int i = 0; i < to.size(); i++) {
mailToAddrs[i] = new InternetAddress(to.get(i));
}
message.setRecipients(RecipientType.TO, mailToAddrs);
//抄送人
InternetAddress[] mailCcAddrs = new InternetAddress[cc.size()];
for (int i = 0; i < cc.size(); i++) {
mailCcAddrs[i] = new InternetAddress(cc.get(i));
}
if (mailCcAddrs.length > 0) {
message.setRecipients(RecipientType.CC, mailCcAddrs);
}
//密送人
InternetAddress[] mailBccAddrs = new InternetAddress[bcc.size()];
for (int i = 0; i < bcc.size(); i++) {
mailBccAddrs[i] = new InternetAddress(bcc.get(i));
}
if (mailBccAddrs.length > 0) {
message.setRecipients(RecipientType.BCC, mailBccAddrs);
}
//内容
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(content, "text/html;charset=UTF-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
//附件
for (AttachFile file : attachFiles) {
MimeBodyPart attachPart = new MimeBodyPart();
attachPart.attachFile(file.getAttachPath());
multipart.addBodyPart(attachPart);
}
message.setContent(multipart);
// 发送
// Transport.send(message);
Transport transport = session.getTransport();
transport.connect();
transport.sendMessage(message, message.getAllRecipients());
} catch (Exception e) {
result = false;
logger.error("发送邮件出现错误:", e);
}
return result;
}
当把mail依赖 切换完 commons-email-1.5 后 测试正常,而 commons-email 使用的是 javax.mail-1.5.6…
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.5</version>
</dependency>
原因: transport.connect();
jdk8 调用 https 代码
解决方式:找到jdk/jre/lib/security/java.security
文件进行修改 ,去除 jdk.tls.disabledAlgorithms
中 SSLv3
和TLSv1
两种协议
# Example:
# jdk.tls.disabledAlgorithms=MD5, SSLv3, DSA, RSA keySize < 2048
# jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1, RC4, DES, MD5withRSA, \
jdk.tls.disabledAlgorithms=TLSv1.1, RC4, DES, MD5withRSA, \
DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, \
include jdk.disabled.namedCurves
上一篇: 知识图谱多数据源融合
下一篇: C# MVC获取表单(form)键值对