无法通过CentOS VPS上的Google SMTP发送电子邮件

4
我正在尝试通过谷歌SMTP发送邮件。
这段代码在我的本地Windows PC上的Tomcat中运行良好。但是在我的CentOS VPS上出现了以下错误:
org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.googlemail.com:465 

这里有一些代码,请检查:

// Create the email message
HtmlEmail email = new HtmlEmail();

email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);

email.addTo(e_mail, f_name+" "+l_name);
email.setFrom("me@gmail.com", "something.com - Account activation");
email.setSubject("something.com - Account activation email");

  // embed the image and get the content id
URL url = new URL("http://something.com/out.php/i6964_logo-email.gif");
String cid = email.embed(url, "BizzKiss logo");

  // set the html message
email.setHtmlMsg("<html><body>Something</body></html>");

  // set the alternative message
email.setTextMsg("Your email client does not support HTML messages");
email.setTLS(true);
  // send the email
email.send();

out.println("Email sent to "+e_mail+"<br/>Please check your email for activation message.Not found? Please check your spam folder.");



org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.googlemail.com:465
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1401)
    at org.apache.commons.mail.Email.send(Email.java:1428)
    at org.apache.jsp.email_jsp._jspService(email_jsp.java:104)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
    at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
    at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
    at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14  Please log in via your web browser and then try again.
534-5.7.14 Learn more at https://support.google.com/mail/bin/answer.py?answer=787
534 5.7.14 54 qf7sm12328634pac.14 - 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:317)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1391)
    ... 30 more

请检查堆栈跟踪并告诉我哪里出了问题。


你的目标VPS上是否有防火墙阻止了请求?异常堆栈跟踪看起来是什么样子? - Bimalesh Jha
如何检查防火墙,没有控制面板,只有SSH,以及堆栈跟踪在Tomcat中的位置。 - Lovepreet Singh Batth
请检查以上的堆栈跟踪信息。 - Lovepreet Singh Batth
5个回答

16
即使Gmail帐户凭据正确,但Google仍可能阻止认证尝试,怀疑是机器人邮件发送者。为了解决这个问题,请确保您在浏览器中使用相同的Gmail帐户登录,然后打开下面的链接并按照验证过程进行操作:

https://accounts.google.com/b/0/DisplayUnlockCaptcha

这将允许访问您的Gmail帐户约10分钟。请确保在此时间范围内尝试您的代码。
这个技巧在半年前曾帮助过我,希望那时以来没有发生任何更改。

在我的情况下,我的脚本在VPS上,所以我没有办法用浏览器加载任何URL。我的做法是:更改了我的Gmail密码。Gmail > 设置 > 帐户。然后在Google账户中,他们列出了被Google阻止的可疑登录尝试(这些是我的脚本尝试的登录)。然后我点击了选项“是的,那是我”。之后,我的脚本就可以工作了(使用新密码)。 - Donn Lee

9
每当脚本尝试自动登录时,Gmail通常会阻止脚本。要通过脚本启用登录,请执行以下操作:
  1. 进入此处https://security.google.com/settings/security/activity?hl=en&pli=1
  2. 在登录列表中,选择被阻止且包含您服务器IP的登录信息。
  3. 在异常活动下点击“更改”并将该活动识别为您的活动。

如果您在列表中没有该活动,请前往https://accounts.google.com/b/0/DisplayUnlockCaptcha,然后重试。

这两个技巧对我多次有效。


0

如果您正在使用SMTP,则主机名必须为smtp.gmail.com,端口号可以是465或587。如果您的用户名或密码错误,则错误日志会通知您,请确保您的帐户凭据并重试。请查看此Google邮件客户端链接。


0

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接