简体   繁体   English

TLS上的Java Mail

[英]Java Mail over TLS

I am trying to send an email from my program through a TLS connection. 我试图通过TLS连接从我的程序发送电子邮件。 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)

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. 我的电子邮件网关具有启用了SSL的传入邮件设置,并在端口587上启用了TLS传出。我能够在Outlook中配置此设置并且它正常工作。 But through my java program it's saying "Connection Refused". 但通过我的java程序,它说“拒绝连接”。 Help Appreciated! 帮助感谢!

Worked Finally: 最后工作:

I used the InstallCert program to import the certicate to generate jssecacerts file and I added the file to my /jre/lib/security/ path. 我使用InstallCert程序导入certicate以生成jssecacerts文件,然后将文件添加到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"); 

You need to use the smtps protocol instead of smtp 您需要使用smtps协议而不是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");

You can also try to set the protocol specificly for rfc822, this helps some times 您还可以尝试特定地为rfc822设置协议,这有时会有所帮助

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 邮件无法使用 tls 或 ssl 连接到 smtp - Java mail cannot connect to smtp using tls or ssl 通过Java程序发送邮件(无法将套接字转换为TLS错误) - Sending Mail through Java program(Could not convert socket to TLS error) javax.mail.MessagingException:无法在 JAVA 中将套接字转换为 TLS 异常 - javax.mail.MessagingException: Could not convert socket to TLS exception in JAVA 如何使用 java 邮件解决“此域需要 TLS” - How to solve "TLS required to proceed for this domain" with java mail java邮件aws smtp:javax.mail.AuthenticationFailedException:220准备启动TLS - java mail aws smtp: javax.mail.AuthenticationFailedException: 220 Ready to start TLS 通过java servlet实现处理邮件协议 - Processing mail protocol over java servlet implementation Java 8,JCE无限强度策略和TLS上的SSL握手 - Java 8 , JCE Unlimited Strength Policy and SSL Handshake over TLS 如何在 Java 中通过 TLS/SSL (FTPS) 服务器连接到 FTP - How to connect to FTP over TLS/SSL (FTPS) server in Java 如何使用Java Mail API验证SMTP服务器的SSL / TLS证书? - How to validate SMTP server's SSL/TLS certificate using java mail API? Java中基于TLS / SSL(FTPS-隐式)服务器的Java FTP-若干错误 - Java FTP over TLS/SSL (FTPS- Implicit) Server in Java - Several Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM