简体   繁体   English

无法通过centos VPS上的谷歌smtp发送电子邮件

[英]Unable to send email via google smtp on centos VPS

I am trying to send email via google SMTP. 我想通过谷歌SMTP发送电子邮件。

The code is working fine in my local windows PC in tomcat. 代码在tomcat中的本地Windows PC中运行良好。 But i got this error on my centos VPS 但我在我的centos VPS上遇到了这个错误

org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.googlemail.com:465 

Here is code, please check it - 这是代码,请检查 -

// 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

Please check the stacktrace and please tell me what is wrong 请检查堆栈跟踪,请告诉我有什么问题

Even if the Gmail account credentials are correct, Google may still block authentication attempt, suspecting a robot mail sender. 即使Gmail帐户凭据正确无误,Google仍可能会阻止身份验证尝试,怀疑机器人邮件发件人。 To fix it, make sure you are logged in using the same Gmail account in your browser, and then open the link below and step through the process of verification: 要解决此问题,请确保您使用浏览器中的相同Gmail帐户登录,然后打开以下链接并逐步完成验证过程:

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

This will allow access to your Gmail account for about 10 minutes. 这将允许访问您的Gmail帐户大约10分钟。 Be sure to try your code within this time frame. 请务必在此时间范围内尝试使用您的代码。

This trick helped me about half a year ago, hope nothing has been changed since then. 大约半年前,这个技巧对我有所帮助,希望从那时起没有任何改变。

Whenever a script tries to login automatically, Gmail generally blocks the script. 每当脚本尝试自动登录时,Gmail通常会阻止该脚本。 To enable login through script do this: 要通过脚本启用登录,请执

  1. Go here https://security.google.com/settings/security/activity?hl=en&pli=1 请访问https://security.google.com/settings/security/activity?hl=zh-CN&pli=1
  2. In the list of signin, select the one which was blocked and contains the IP of your server. 在登录列表中,选择被阻止的一个并包含服务器的IP。
  3. Under Unusual activity click Change and Recognize the activity as yours. 在“异常”活动下,单击“更改”并将活动识别为您的活动。

If you do not have the activity in the list goto https://accounts.google.com/b/0/DisplayUnlockCaptcha and then try. 如果您没有列表中的活动,请转到https://accounts.google.com/b/0/DisplayUnlockCaptcha ,然后尝试。

Both Tricks worked for me many times. 这两个技巧都为我工作了很多次。

Your host name must be smtp.gmail.com if you are using SMTP and port number can be either 465 or 587 . 如果使用SMTP,则主机名必须为smtp.gmail.com ,端口号可以是465或587 Your error log informs like your username or password is wrong so please ensure your account credentials and try again. 您的错误日志通知您的用户名或密码错误,因此请确保您的帐户凭据,然后重试。 Please have a look at this google mail client link. 请看看这个谷歌邮件客户端链接。

要通过Gmail启用发送邮件,您必须在Gmail帐户中停用一些安全选项:请参阅https://support.google.com/accounts/answer/6010255

I got hung up on this... Make sure you set "Protocol" to SMTP+SSL. 我挂了这个...确保你将“协议”设置为SMTP + SSL。 Otherwise follow instructions here: https://confluence.jetbrains.com/display/TCD8/Setting+up+Google+Mail+and+Google+Talk+as+Notification+Servers 否则请按照以下说明操作: https//confluence.jetbrains.com/display/TCD8/Setting+up+Google+Mail+and+Google+Talk+as+Notification+Servers

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

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