简体   繁体   English

javamail返回smtp作为传输,而不是smtps

[英]javamail returning smtp as transport, instead of smtps

I set the mail.transport property to smtps, beside the very basic information for connecting to a smtps server: 我将mail.transport属性设置为smtps,除了用于连接到smtps服务器的非常基本的信息之外:

    Properties p = new Properties();
    p.put("mail.transport.protocol", "smtps");
    p.put("mail.smtps.host", "smtp.gmail.com");
    p.put("mail.smtps.auth", true);

    Session s = Session.getDefaultInstance(p,new Authenticator(){/*authenticator impl.*/});

    MimeMessage mm = new MimeMessage(s); /*then i set the subject, then the body... */
    mm.setRecipients(RecipientType.TO, "myfakeaddress@gmail.com");

And now, i try to send my message. 现在,我尝试发送我的消息。 I want to try the static method; 我想尝试静态方法; using the instance method sendMessage it works fine. 使用实例方法sendMessage可以正常工作。 Here it is: 这里是:

    Transport.send(mm);

It tries to connect to a smtp server, instead of a smtps server . 它尝试连接到smtp服务器,而不是smtps服务器 Stepping inside the implementation of javamail (btw, my version is 1.4.5) i've discovered that the method that fails is: 逐步进入javamail的实现(顺便说一句,我的版本是1.4.5),我发现失败的方法是:

 transport = s.getTransport(addresses[0]);

because it returns an SMTPTransport instead of SMTPSSLTransport ; 因为它返回的是SMTPTransport而不是SMTPSSLTransport this even if i've set the mail.transport.protocol property to smtps as you can see in the second line of code. 即使您已将mail.transport.protocol属性设置为mail.transport.protocol ,正如您在第二行代码中看到的那样。 Is my procedure buggy anywhere or it isn't possible to send smtps mails via Transport.send static method? 我的程序是否在任何地方都有错误,或者无法通过Transport.send静态方法发送smtps邮件?

Bill Shannon (current maintainer of Javamail) suggests in this question Bill Shannon (Javamail的当前维护者)在此问题中提出了建议

Get rid of all the socket factory properties; 摆脱所有套接字工厂属性; if you're using a reasonably recent version of JavaMail you don't need them. 如果您使用的是JavaMail的较新版本,则不需要它们。 See the JavaMail FAQ for how to configure JavaMail to access Gmail. 有关如何配置JavaMail以访问Gmail的信息,请参阅JavaMail常见问题解答。 You'll also find debugging tips there if it still doesn't work. 如果仍然无法使用,您还将在其中找到调试提示。

Also, change Session.getDefaultInstance to Session.getInstance. 另外,将Session.getDefaultInstance更改为Session.getInstance。

Here is the relevant code from the Javamail FAQ 这是Javamail常见问题解答中的相关代码

String host = "smtp.gmail.com";
String username = "user";
String password = "passwd";

Properties props = new Properties();
props.put("mail.smtps.auth", "true");
props.put("mail.debug", "true");

MimeMessage msg = new MimeMessage(session);
// set the message content here

Transport t = session.getTransport("smtps");

try {
  t.connect(host, username, password);
  t.sendMessage(msg, msg.getAllRecipients());
} finally {
  t.close();
}

Transport.send(msg) is looking up the protocol(s) associated with the recipients of your email, for each type of recipient. Transport.send(msg)正在针对每种收件人类型查找与电子邮件收件人相关的协议。

All your recipients are InternetAddress es, which have the type rfc822 您的所有收件人都是InternetAddress es,其类型为rfc822

Here are three ways to set JavaMail to use smtps protocol for rfc822 addresses: 这是将JavaMail设置为对rfc822地址使用smtps协议的三种方法:

  1. Add the line rfc822=smtps in the property files javamail.address.map or javamail.default.address.map (as described in the Session javadoc ) 在属性文件javamail.address.mapjavamail.default.address.map添加rfc822=smtps行(如Session javadoc中所述
  2. Call s.setProtocolForAddress("rfc822", "smtps") ` on your instantiated session (requires JavaMail 1.4 or later) 在实例化会话上调用s.setProtocolForAddress("rfc822", "smtps") `(需要JavaMail 1.4或更高版本)
  3. Set the property mail.transport.protocol.rfc822 to smtps when instantiating your session (requires JavaMail 1.4.3 or later) 实例化会话时,将属性mail.transport.protocol.rfc822设置为smtps (需要JavaMail 1.4.3或更高版本)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM