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

javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 465;,第1张

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

1、报错信息如下:

trying to connect to host "smtp.163.com", port 465, isSSL true
javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 465;
  nested exception is:
        javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
        at javax.mail.Service.connect(Service.java:295)
        at javax.mail.Service.connect(Service.java:176)
        at javax.mail.Service.connect(Service.java:196)
        at com.xnpool.smsServer.Tools.SendEmailUtils.sendEmail(SendEmailUtils.java:66)
        at com.xnpool.smsServer.controller.sms.SmsCodeServicerApi.sendEmail(SmsCodeServicerApi.java:151)
        at sun.reflect.GeneratedMethodAccessor109.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

2、代码

public class SendEmailRequest {

    
    private String email;
    
    private String title;
    
    private String content;
    
    private String host;
    
    private String from;
    
    private String secrect;

}


@Slf4j
public class SendEmailUtils {

    public static void sendEmail(SendEmailRequest request) {

        try {
            log.info(log.getName() + " 发送邮件请求参数【{}】", JSONObject.toJSONString(request));

            Properties properties = new Properties();

            properties.put("mail.transport.protocol", "smtp");// 连接协议

            properties.put("mail.smtp.host", request.getHost());// 163主机名

            properties.put("mail.smtp.port", 465);// 端口号

            properties.put("mail.smtp.auth", "true");

            properties.put("mail.smtp.ssl.enable", "true");//设置是否使用ssl安全连接  ---一般都使用

            properties.put("mail.debug", "true");//设置是否显示debug信息  true 会在控制台显示相关信息

            //得到回话对象

            Session session = Session.getInstance(properties);

            // 获取邮件对象

            Message message = new MimeMessage(session);

            //设置发件人邮箱地址

            message.setFrom(new InternetAddress(request.getFrom()));

            //设置收件人地址
            message.setRecipients(MimeMessage.RecipientType.TO, new InternetAddress[]{new InternetAddress(request.getEmail())});

            //抄送自己
            message.addRecipients(Message.RecipientType.CC, InternetAddress.parse(request.getFrom()));
            //设置邮件标题

            message.setSubject(request.getTitle());

            //设置邮件内容

            message.setText(request.getContent());

            //得到邮差对象
            Transport transport = session.getTransport();

            //连接自己的邮箱账户

            transport.connect(request.getFrom(), request.getSecrect());//密码为刚才得到的授权码

            //发送邮件
            transport.sendMessage(message, message.getAllRecipients());

            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info(log.getName() + " 发送邮件发送成功【{}】", JSONObject.toJSONString(request));
    }
}

3、问题解决
发现自己的邮箱设置:MAP/POP3 都已经开启,且都已经增加了授权码还报错,那就是正式环境JDK 的验证问题

	which java
	
	ls -lrt /usr/bin/java
	
	ls -lrt /etc/alternatives/java	

找到目录

	/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/security

修改java.security:不要SSL3,TLSv1, TLSv1.1

# dk.tls.disabledAlgorithms=MD5, SSLv3, DSA, RSA keySize < 2048
jdk.tls.disabledAlgorithms=RC4, DES, MD5withRSA, 
    DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, 
    include jdk.disabled.namedCurves

重启JAVA程序

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5575542.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存