简体   繁体   English

使用JavaMail发送邮件-端口25,STARTTLS,身份验证

[英]Sending mail using JavaMail - port 25, STARTTLS, authentication

I'm trying to send an email message to a SMTP server that listens on port 25, uses STARTTLS and requires authentication. 我正在尝试向SMTP服务器发送电子邮件,该服务器在端口25上侦听,使用STARTTLS并需要身份验证。

As far as I understand, the client should 据我了解,客户应

  • greet the server with EHLO clientName EHLO clientName向服务器打招呼
  • initiate TLS using STARTTLS 使用STARTTLS启动TLS
  • authenticate itself using AUTH LOGIN 使用AUTH LOGIN身份验证
  • go on with deliverying the email using RCPT TO , etc 继续使用RCPT TO等发送电子邮件

My simplified code is 我的简化代码是

String protocol = "smtp";

Properties props = new Properties();
props.put("mail.debug", "true");
props.put("mail." + protocol + ".auth", true);
props.put("mail." + protocol + ".host", smtpHost);
props.put("mail." + protocol + ".starttls.required", true);

Session session = Session.getInstance(props);

InternetAddress[] recipients = InternetAddress.parse(username);

Message message = buildMessage(session, username, recipients);

Transport t = session.getTransport(protocol);
t.connect(username, password);

t.sendMessage(message, recipients);

As far as I understand this should work, but the debug output shows that it hangs at 据我了解,这应该工作,但调试输出显示它挂在

STARTTLS
220 2.0.0 Ready to start TLS

Removing the starttls.required property leads to the server denying access, since the AUTH command is never present unless STARTTLS is issued 删除starttls.required属性会导致服务器拒绝访问,因为除非发出STARTTLS ,否则AUTH命令永远不会存在

MAIL FROM:<deliverytest@somedomain.com>
250 2.1.0 Ok
RCPT TO:<deliverytest@somedomain.com>
554 5.7.1 Service unavailable; Client host [50.16.63.26] blocked using zen.spamhaus.org; http://www.spamhaus.org/query/bl?ip=50.16.63.26

What's the proper incantation to get Javamail to work with my setup? 使Javamail与我的设置配合使用的正确方法是什么?

Two things are wrong here 这里有两件事是错误的

  1. It appears the remote does not support TLS on port 25 看来遥控器在端口25上不支持TLS
  2. It connected successfully WITHOUT TLS, but the IP address you're sending from is identified as a SPAM sender (or maybe just an ISP DHCP address) on Spamhaus, and the use of that server to send mail is blocked. 它在没有TLS的情况下成功连接,但是您要从中发送的IP地址被标识为Spamhaus上的SPAM发件人(或者可能只是ISP DHCP地址),并且该服务器发送邮件的使用被阻止。

If you follow the link given in the error ( http://www.spamhaus.org/query/bl?ip=50.16.63.26 ) you get to a page that says 如果您按照错误中给出的链接( http://www.spamhaus.org/query/bl?ip=50.16.63.26 )转到页面,该页面显示

Ref: PBL1522093 参考:PBL1522093

50.16.0.0/16 is listed on the Policy Block List (PBL) 50.16.0.0/16在策略阻止列表(PBL)上列出

Outbound Email Policy of Amazon Web Services EC2 for this IP range: 此IP范围的Amazon Web Services EC2的出站电子邮件策略:

It is the policy of Amazon Web Services EC2 that unauthenticated email sent from this IP address should be sent out only via the designated outbound mail server allocated to Amazon Web Services EC2 customers. Amazon Web Services EC2的政策是,仅通过分配给Amazon Web Services EC2客户的指定出站邮件服务器发送从该IP地址发送的未经身份验证的电子邮件。 To find the hostname of the correct mail server to use, customers should consult the original signup documentation or contact Amazon Web Services EC2 Technical Support. 要查找要使用的正确邮件服务器的主机名,客户应查阅原始注册文档或联系Amazon Web Services EC2技术支持。

In other words, you must send all outgoing mail via the SMTP server provided for you by Amazon. 换句话说,您必须通过Amazon为您提供的SMTP服务器发送所有传出邮件。 You are not permitted to sent email directly, and any receiving host that uses Spamhaus (many, if not most) will block your attempts to do so. 您不可以直接发送电子邮件,任何使用Spamhaus的接收主机(很多,如果不是大多数)都将阻止您进行发送。

You're doing it right. 您做对了。 Are you sure it's hanging and not throwing an unexpected exception? 您确定它已挂起并且不会引发意外异常吗? If it's hanging, what's the stack trace show? 如果挂起了,堆栈跟踪显示什么?

Turns out that the original code was working. 原来的代码在工作。 I was sent of by two mistakes: 我被两个错误送出:

  • I did not check the actual email account to see whether the email was delivered - and it was. 我没有检查实际的电子邮件帐户,以查看是否已发送电子邮件。
  • I was using a thread pool which was not shut down therefore the program appeared to be hanging 我正在使用未关闭的线程池,因此程序似乎正在挂起

OTOH, it would be nice for the Javamail debug mode to let me know when the message was actually sent. OTOH,对于Javamail调试模式,最好在实际发送消息时通知我。

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

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