Java邮件通过TLS [英] Java Mail over TLS

查看:276
本文介绍了Java邮件通过TLS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过TLS连接从我的程序发送电子邮件。这是我的代码

I am trying to send an email from my program through a TLS connection. Here is my code

    final String username = "XXXXXX";
    final String password = "XXXXX"; 
    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.starttls.enable","true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.host", "mail.xxxx.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(username, password);
              }
            });

    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("from@xxxx.com"));
    message.setRecipients(Message.RecipientType.TO,
        InternetAddress.parse(to_address));
    message.setSubject("Test Mail"); 
    message.setText("TestMail ");
    Transport.send(message)

我的电子邮件网关具有已启用SSL的传入邮件设置,在587端口上启用了TLS功能。我可以在outlook中配置这个设置,它的工作正常。但是通过我的java程序,它说连接拒绝。
帮助欣赏!

My email gateway has incoming Mail settings with SSL enabled and outgoing with TLS enabled on port 587. I am able to configure this settings in outlook and it's working fine. But through my java program it's saying "Connection Refused". Help Appreciated!

我使用了InstallCert 程序导入证书以生成jssecacerts文件,并将该文件添加到我的/ jre / lib / security / path中。这里是我的工作代码

I used the InstallCert program to import the certicate to generate jssecacerts file and I added the file to my /jre/lib/security/ path. here is my working code

    properties.put("mail.transport.protocol", "smtp");
    properties.put("mail.smtp.host", "XXXXXX");  
    properties.put("mail.smtp.port", "465"); 
    properties.put("mail.smtp.ssl.enable", true);
    properties.put("mail.smtp.socketFactory.port", "465");
    properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
    properties.put("mail.smtp.socketFactory.fallback", "false"); 
    properties.put("mail.smtp.quitwait", "false"); 
    properties.put("mail.smtp.auth", "true"); 


推荐答案

您需要使用 smtps 协议而不是 smtp

You need to use the smtps protocol instead of smtp

props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.starttls.enable","true");
props.put("mail.smtps.auth", "true");
props.put("mail.smtps.host", "mail.xxxx.com");
props.put("mail.smtps.port", "587");

您还可以尝试为rfc822设置特定的协议,这有助于某些时间

You can also try to set the protocol specificly for rfc822, this helps some times

props.put("mail.transport.protocol.rfc822", "smtps");

这篇关于Java邮件通过TLS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆