简体   繁体   English

JavaMail不适用于Amazon EC2

[英]JavaMail Not Working on Amazon EC2


PROBLEM: 问题:

I'm trying to use JavaMail to send email from an EC2 Ubuntu 12.04 Server but it's not working. 我正在尝试使用JavaMail从EC2 Ubuntu 12.04服务器发送电子邮件,但它无法正常工作。


BACKGROUND: 背景:

I intend to send an auto generated mail from a Tomcat Server to a few team members. 我打算从Tomcat服务器向少数团队成员发送自动生成的邮件。 I used JavaMail 1.4.7 for this task and I've successfully tried it on a local installation of Tomcat (on a personal laptop). 我使用JavaMail 1.4.7来完成这项任务,并且我已经在Tomcat的本地安装上(在个人笔记本电脑上) 成功地尝试了它。

The next step was where I put this module on an EC2 Server but it started failing with the below given error: 下一步是我将此模块放在EC2服务器上,但它开始失败并出现以下给定错误:


ERROR: 错误:

DEBUG SMTP: AUTH LOGIN failed javax.mail.AuthenticationFailedException: 535-5.7.1 Username and Password not accepted. DEBUG SMTP:AUTH LOGIN失败javax.mail.AuthenticationFailedException:535-5.7.1不接受用户名和密码。 Learn more at 535 5.7.1 http://support.google.com/mail/bin/answer.py?answer=14257 j13sm1603739pat.17 - gsmtp 如需了解更多信息,请访问535 5.7.1 http://support.google.com/mail/bin/answer.py?answer=14257 j13sm1603739pat.17 - gsmtp

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)

Code Snippet: 代码片段:

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = new Properties();
props.put("mail.smtps.host","smtp.gmail.com");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtps.auth", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.port", 465);
props.put("mail.smtp.socketFactory.port", 465);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.ssl.enable", true);

Session session = Session.getDefaultInstance(props);
session.setDebug(debug);

Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress("fromId@gmail.com");
msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
msg.setSubject("test subject");
msg.setContent("test message", "text/plain");
Transport tp = session.getTransport("smtp");
tp.connect(SMTP_HOST_NAME, "dummyId@gmail.com", "dummypassword");
tp.sendMessage(msg, addressTo);
tp.close();

ADDITIONAL INFO: 附加信息:

  1. I've added permissions for all ports on the EC2 instance. 我已经为EC2实例上的所有端口添加了权限。 Also, I've added explicit permissions for ports 465(SMTPS) and 25(SMTP). 此外,我已为端口465(SMTPS)和25(SMTP)添加了显式权限。
  2. I've cross checked the correctness of the username and password provided in the code. 我已经交叉检查了代码中提供的用户名和密码的正确性。

In the past, spam sent from servers within EC2 has been a major problem for both Amazon and mail service providers. 过去,从EC2内的服务器发送的垃圾邮件一直是亚马逊和邮件服务提供商的主要问题。 Initially, Amazon deliberately had insufficient DNS information to pass anti-spam checking of common email services (lack of DNS reverse lookup PTR records), then Amazon introduced low mail quotas, followed approval processes to give full access to EC2 or SES mail (provides specific technical setup to fully enable outgoing email). 最初,亚马逊故意没有足够的DNS信息来传递常见电子邮件服务的反垃圾邮件检查(缺乏DNS反向查找PTR记录),然后亚马逊推出了低邮件配额,遵循审批流程以提供对EC2或SES邮件的完全访问权限(具体提供)技术设置,以完全启用外发电子邮件)。

Here's a link describing some of the history: http://shlomoswidler.com/2009/07/sending-email-from-ec2.html 这是一个描述一些历史的链接: http//shlomoswidler.com/2009/07/sending-email-from-ec2.html

While the error you're getting is a failed authentication at Gmail server, it may be ( probably is ) because Gmail identifies your sender IP address as a potential spammer, because you didn't setup Amazon reverse DNS lookup properties correctly. 虽然您收到的错误是Gmail服务器上的身份验证失败,但可能( 可能是 )因为Gmail将您的发件人IP地址识别为潜在的垃圾邮件发送者,因为您没有正确设置Amazon反向DNS查找属性。

  1. Setup Amazon Reverse DNS lookup details, so that your mail is not detected as spam by Gmail. 设置Amazon反向DNS查找详细信息,以便Gmail不会将您的邮件检测为垃圾邮件。 It seems this can only be done by submitting a request form to lift Email Sending quota. 这似乎只能通过提交申请表来解除电子邮件发送配额来完成。 From Amazon EC2 IP Information FAQs: 来自Amazon EC2 IP信息常见问题解答:

    Q: Can I configure the reverse DNS record for my Elastic IP address? 问:我可以为弹性IP地址配置反向DNS记录吗?

    Yes, you can configure the reverse DNS record of your Elastic IP address (submit the Request to Remove Email Sending Limitations Form ). 是的,您可以配置弹性IP地址的反向DNS记录(提交请求以删除电子邮件发送限制表 )。 Note that a corresponding forward DNS record pointing to that Elastic IP address must exist before we can create the reverse DNS record. 请注意,在我们创建反向DNS记录之前,必须存在指向该弹性IP地址的相应转发DNS记录。

    Here's an example of an email sent out by Amazon when an email quota is exhausted - again spelling out request & approval for DNS PTR configuration: http://www.practicalclouds.com/content/guide/sending-email-ec2-instances 以下是亚马逊在电子邮件配额耗尽时发送的电子邮件示例 - 再次列出DNS PTR配置的请求和批准: http//www.practicalclouds.com/content/guide/sending-email-ec2-instances

  2. Precisely follow all the instructions in the link included in you Gmail error message: http://support.google.com/mail/bin/answer.py?answer=14257 . 请严格按照Gmail错误消息中包含的所有说明进行操作: http//support.google.com/mail/bin/answer.py?answer = 14257 This will reset your Gmail account status, if Gmail thinks your account is spamming from your IP address. 如果Gmail认为您的帐户是通过您的IP地址发送垃圾邮件,则会重置您的Gmail帐户状态。

  3. Alternatively, you can send mail via Amazon SES, also requiring request, approval and appropriate Amazon-provided configuration. 或者,您可以通过Amazon SES发送邮件,还需要请求,批准和适当的亚马逊提供的配置。 This allows mail to be sent via com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient.sendEmail . 这允许通过com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient.sendEmail发送邮件。 If you use this, add authorisation for your code to sendmail: Unable to send email from Amazon EC2 Server in Java . 如果您使用此功能,请将代码的授权添加到sendmail: 无法使用Java从Amazon EC2 Server发送电子邮件

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

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