【问题标题】:Getting javax.mail.AuthenticationFailedException in Tomcat7在 Tomcat7 中获取 javax.mail.AuthenticationFailedException
【发布时间】:2014-08-15 21:12:25
【问题描述】:

程序尝试发送电子邮件,但抛出运行时异常:AuthenticationFailedException。我使用 smtp 协议发送邮件。我使用了 TLS。下面是我的 Mailsender 类,名为 SendMail。爪哇

  package com.xxx.dashboard.mail;

  import java.util.Map;
  import java.util.Properties;
  import javax.mail.Message;
  import javax.mail.MessagingException;
  import javax.mail.PasswordAuthentication;
  import javax.mail.Session;
  import javax.mail.Transport;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeMessage;
  import com.edifixio.dashboard.configuration.ConfigurationBundle;

  public class SendMail {

  public static void sendMail(String toAddresses, String subject, String url, String name, String email){

    Map<String, String> mapdata = ConfigurationBundle.getMapdata();
    final String emailId = mapdata.get("mail_id");
    final String username = mapdata.get("mail_username");
    final String password = mapdata.get("mail_password");
    final String smtpHost = mapdata.get("mail_smtp_host");
    final String smtpPort = mapdata.get("mail_smtp_port");

    Properties props = new Properties();
    props.put("mail.smtp.auth", true);
    props.put("mail.smtp.starttls.enable", true);
    props.put("mail.smtp.host", smtpHost);
    props.put("mail.smtp.port", smtpPort);


    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                                protected PasswordAuthentication getPasswordAuthentication() {

                                  return new PasswordAuthentication(username, password);
                           }
                    });

       try {
              Message message = new MimeMessage(session);
              message.setFrom(new InternetAddress(emailId));
              message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(toAddresses));
              message.setSubject(subject);

              String text =  "<b>Hello "+name+",</b><br/><br/> Thank you for registering with WARM for monitoring of your URL."
                    + "<br/>In order to activate your account please <a href="+url+"><b>Click Here</b></a><br/><br/>"
                            + "Once you have activated your WARM account you can login to the WARM Dashboard using your Username "
                            + "<a href=\"mailto:"+email+"\">"+email+"</a>.<br/>If you are having problems with the given link, "
                                    + "please reply back to the following Email address: <a href=\"mailto:warm.xxx@gmail.com\">warm.xxx@gmail.com</a>.</br>This is a system generated mail. Please do not reply to this mail.<br/><br/>Thanks and Regards<br/>WARM Admin.";

              message.setContent(text,"text/html");
              Transport.send(message);
       } catch (MessagingException e) {
              throw new RuntimeException(e);
       }
 }

public static void main(String args[]) {
    sendMail("sumit.ghosh@xxx.com", "Hello", "h", "Rakesh", "rakesh@gmail.com");
    System.out.println("Successfully send mail to - "+toAddresses); 
  }
}

当我从 Eclipse 中选择作为 Java 应用程序运行时,我收到以下错误日志

       Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.9 Please log in with your web browser and then try again. Learn more at 534 5.7.9 https://support.google.com/mail/bin/answer.py?answer=78754 qk9sm15826619pac.16 - gsmtp
       at com.xxx.dashboard.mail.SendMail.sendMail(SendMail.java:80)
       at com.xxx.dashboard.mail.SendMail.main(SendMail.java:87)
       Caused by: javax.mail.AuthenticationFailedException: 534-5.7.9 Please log in with your web browser and then try again. Learn more at 534 5.7.9 https://support.google.com/mail/bin/answer.py?answer=78754 qk9sm15826619pac.16 - 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 com.edifixio.dashboard.mail.SendMail.sendMail(SendMail.java:74)
       ... 1 more

我有一个名为 config.properties 的属性文件,其中保存了用户名和密码,以及端口号等其他凭据。请在下面找到它的代码:

mail_id=test@gmail.com
mail_username=test@gmail.com
mail_password=testxxx
mail_smtp_host=smtp.gmail.com
mail_smtp_port=587
ldap_provider_ip=192.168.15.50
ldap_port=1666
ldap_base=ou=users,ou=warm,dc=xxx,dc=co,dc=in
ldap_dir_context=dc=edifixio,dc=co,dc=in
ldap_security_principal=cn=Directory Manager,dc=xxx,dc=co,dc=in
ldap_security_credential=testxxx

【问题讨论】:

  • 通过mail.smtp.protocol=smtps
  • 不,它不起作用。我已经完成了 props.put("mail.smtp.protocol", "smtps");但它给出了同样的异常

标签: java smtp jakarta-mail


【解决方案1】:

试试这些属性:

mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.auth.mechanisms=login
mail.smtp.quitwait=false
mail.debug = false
mail.username=test@gmail.com
mail.password=test
mail.smtp.host=smtp.gmail.com
mail.smtp.port=587
mail.smtp.protocol=smtps

【讨论】:

  • 不幸的是它也不起作用。这次我给了端口号 465。还有其他解决方案吗???
  • 有了这些确切的属性,我在我的 java 应用程序中使用 gmail 服务器和 TLS。尝试使调试属性为真并检查是否有任何特定错误。你的例外有这个链接,你也可以查看support.google.com/mail/answer/78754
  • 好吧它终于工作了。但不是465端口而是587端口。我必须清除验证码。
【解决方案2】:

您的代码在 Debian OS 上的 eclipse 中发送邮件时没有任何异常。您是否正在运行任何可能会干扰连接尝试的防火墙或防病毒软件?

【讨论】:

  • 问题现已解决。我的 gmail id 中有一些与验证码相关的问题。
【解决方案3】:

我也遇到了这个问题,我必须进入我的 gmail 帐户设置并在安全性下启用“访问不太安全的应用程序”来修复它。

【讨论】:

  • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方发表评论 - 您可以随时评论自己的帖子,一旦您有足够的reputation,您就可以comment on any post
  • @Hamad,它可能很短,但这在什么方面不能回答问题?
  • 这对我有用.... :) 只需要更改帐户权限。这可能是安全威胁吗??
【解决方案4】:

我还在尝试使用 google smtp 服务器的简单 javax 邮件程序。在收到 Authentication failed 消息时,我必须转到 https://www.google.com/settings/security/lesssecureapps 并为不太安全的应用程序打开访问权限

【讨论】:

    【解决方案5】:
    1. 登录到您的属性文件中指定的 gmail 帐户
    2. 点击右上角的圆圈 -> 管理您的 google 帐户 -> 安全性 -> 不太安全的应用访问。开启它
    3. 转到https://accounts.google.com/DisplayUnlockCaptcha
    4. 点击继续并退出 gmail。
    5. 测试您的服务。现在它将建立一个连接来发送邮件

    【讨论】:

      猜你喜欢
      • 2020-11-06
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 2017-08-01
      • 2012-12-23
      • 2013-05-30
      • 2012-03-14
      • 2015-11-25
      相关资源
      最近更新 更多