Java SMTP 邮件发送问题:com.sun.mail.smtp.SMTPAddressFailedException:550 5.7.1 无法中继
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47154643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SMTP Mail Sending Issue : com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay
提问by Nirav Prajapati
I am trying to send mail from Java. If i will send mail to same SMTP it working fine.If i will send mail to outside SMTP means like Gmail, Yahoo etc. it'as shows error like,
我正在尝试从 Java 发送邮件。 如果我将邮件发送到同一个 SMTP 它工作正常。如果我将邮件发送到外部 SMTP 方式,例如 Gmail、Yahoo 等,则会显示错误,例如,
[com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay][1]
[com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 无法中继][1]
ERROR :
错误 :
SimpleEmail Start
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay
Mail Send Successfully
at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:2064)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1286)
at javax.mail.Transport.send0(Transport.java:255)
at javax.mail.Transport.send(Transport.java:124)
at com.nirav.java.project.demo.JavaMailSend.sendEmail(JavaMailSend.java:26)
at com.nirav.java.project.demo.NewSimpleMail.main(NewSimpleMail.java:34)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay
at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1917)
... 5 more
Code For Mail Sending :
邮件发送代码:
try {
System.out.println("SimpleEmail Start");
String smtpHostServer = "XX.XX.XX.XXX";
final String toEmail = "[email protected]";
final String fromEmail = "[email protected]";
final String password = "XXXXXXXXXXXX";
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpHostServer);
props.put("mail.smtp.port", "25"); //If other then
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, password);
}
});
//Session session = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.addHeader("Content-type", "text/HTML; charset=UTF-8");
message.addHeader("format", "flowed");
message.addHeader("Content-Transfer-Encoding", "8bit");
message.setFrom(new InternetAddress("[email protected]", "NoReply-JD"));
message.setReplyTo(InternetAddress.parse("[email protected]", false));
message.setSubject(subject, "UTF-8");
message.setText(body, "UTF-8");
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
Transport.send(message);
System.out.println("Mail Send Successfully");
} catch (Exception e) {
e.printStackTrace();
}
Please help me. Thnaks.
请帮我。纳克斯。
回答by rkosegi
Originaly I was about to post this as comment, but it's too long.
原来我正要将此作为评论发布,但它太长了。
Error is quite obvious, you are not allowed to use given SMTP server as relay. (What are SMPT relays?)
错误很明显,您不能使用给定的 SMTP 服务器作为中继。(什么是 SMPT 继电器?)
There are couple of reasons why this can happen:
发生这种情况的原因有两个:
You are not authenticated (need to login before sending)
Recipient is not in list of domains allowed to relay to
IP address from which you are connecting is not in white list (aka mynetworks in postfix context)
您未通过身份验证(发送前需要登录)
收件人不在允许中继的域列表中
您连接的 IP 地址不在白名单中(在 postfix 上下文中也称为 mynetworks)
without providing more info (which SMTP server you are using, where are you sending mail, are you authenticated), I guess nobody will help you.
没有提供更多信息(您使用的是哪个 SMTP 服务器,您在哪里发送邮件,您是否经过身份验证),我想没有人会帮助您。
5.7.1 status code from IANA registry
The sender is not authorized to send to the destination. This can be the result of per-host or per-recipient filtering. This memo does not discuss the merits of any such filtering, but provides a mechanism to report such. This is useful only as a permanent error.
发件人无权发送到目的地。这可能是按主机或按收件人过滤的结果。本备忘录不讨论任何此类过滤的优点,但提供了一种报告此类过滤的机制。这仅作为永久错误有用。
回答by Hans-Martin Berg
Had this problem also but it was intermittent. We have 2 servers that are within a cluster and on one the ip address of system that sends the email was not defined correctly. Unfortunately the error message "javax.mail.SendFailedException: Invalid Addresses is confusing. the 2nd error message: "com.sun.mail.smtp.SMTPAddressFailedException: 550 relay not permitted" matches much better.
也有这个问题,但它是间歇性的。我们有 2 台服务器位于一个集群中,其中一台服务器发送电子邮件的系统的 ip 地址未正确定义。不幸的是,错误消息“javax.mail.SendFailedException:无效地址令人困惑。第二个错误消息:“com.sun.mail.smtp.SMTPAddressFailedException:550 不允许中继”匹配得更好。