Java Mail 无法远程工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23206844/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:20:02  来源:igfitidea点击:

Java Mail won't work remotely

javajavamail

提问by user3473776

I've made a Java Mail Servlet to obviously send emails. It works perfectly fine on the production server but when I try and run it on my own home server I get this following error message

我已经制作了一个 Java Mail Servlet 来明显地发送电子邮件。它在生产服务器上运行良好,但是当我尝试在我自己的家庭服务器上运行它时,我收到以下错误消息

javax.mail.SendFailedException: Invalid Addresses;

javax.mail.SendFailedException:无效地址;

javax.servlet.ServletException: javax.mail.SendFailedException: Invalid Addresses; nested exception is: com.sun.mail.smtp.SMTPAddressFailedException: 554 5.7.1 <*@googlemail.com>: Relay access denied

javax.servlet.ServletException:javax.mail.SendFailedException:无效地址;嵌套异常是:com.sun.mail.smtp.SMTPAddressFailedException:554 5.7.1 < *@googlemail.com>:中继访问被拒绝

evo.net.Mail.service(Mail.java:60)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52

I changed the hostname variable from localhost to the server's IP in the servlet and it didnt make a difference.

我将主机名变量从 localhost 更改为 servlet 中服务器的 IP,但没有任何区别。

My code:

我的代码:

package evo.net;
import java.util.*;

import javax.mail.*;
import javax.mail.Session;
import javax.mail.internet.*;
import javax.activation.*;

import java.io.IOException;
import java.io.PrintWriter;

 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

 @WebServlet("/Mail")
 public class Mail extends HttpServlet {
private static final long serialVersionUID = 1L;

 public Mail() {
    super();
 }

protected void service(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {

    PrintWriter out = response.getWriter();

    String to = "********";
    String from = "*******@codeprojects.co.uk";
    String host = "codeprojects.co.uk";

    Properties props = System.getProperties();

    props.put("mail.smtp.host", host);
    props.put("mail.smtp.auth", "true");

    Session session = Session.getDefaultInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("root", "*****");
            }
      });

    try {
        Transport trans = session.getTransport("smtp");
        trans.connect("root", "******");

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new             InternetAddress(to));

        message.setSubject("The subject");
        message.setText("Hello World");

        trans.sendMessage(message, message.getAllRecipients());;
        out.println("Test");

    } catch(MessagingException mex) {
        throw new ServletException(mex);
    }
}
  }

回答by Christian Bongiorno

Google validates the domain you are sending from (or saying you're sending from).

Google 会验证您发送的域(或说您发送的域)。

If you're using a development account you will get different behavior than production accounts. It goes this way with every major mail provider out there (including amazon SES)

如果您使用的是开发帐户,您将获得与生产帐户不同的行为。每个主要的邮件提供商(包括亚马逊 SES)都是如此

Your answer lies in Domain Keys

您的答案在于域密钥

回答by Eric J.

The line

线

SMTPAddressFailedException: 554 5.7.1 <*@googlemail.com>: Relay access denied

says it all.

都说了。

For some reason, either your home ISP or Gmail are disallowing your request to relay the email.

出于某种原因,您的家庭 ISP 或 Gmail 不允许您转发电子邮件的请求。

Your home ISP might require you to connect to their own SMTP server (this is a common anti-spam consideration).

您的家庭 ISP 可能要求您连接到他们自己的 SMTP 服务器(这是一个常见的反垃圾邮件注意事项)。

Gmail might have your home IP on a blacklist (perhaps because your ISP does not carefully prevent spam sending).

Gmail 可能将您的家庭 IP 列入黑名单(可能是因为您的 ISP 没有仔细阻止垃圾邮件发送)。

Update

更新

String host = "codeprojects.co.uk";
props.put("mail.smtp.host", host);

indicates you are using an external SMTP server. Many home ISP's don't allow that, and would give an error like the one you describe. For running this at home, try changing the SMTP server to that of your ISP.

表示您正在使用外部 SMTP 服务器。许多家庭 ISP 不允许这样做,并且会出现您描述的错误。要在家中运行它,请尝试将 SMTP 服务器更改为您的 ISP 的服务器。