简体   繁体   English

在 Tomcat7 中获取 javax.mail.AuthenticationFailedException

[英]Getting javax.mail.AuthenticationFailedException in Tomcat7

The program attempts to send e-mail but throws a run time exception:AuthenticationFailedException .I have used smtp protocol for sending the mail.I have used TLS.Below is my Mailsender class named SendMail.java程序尝试发送电子邮件,但抛出运行时异常:AuthenticationFailedException 。我使用 smtp 协议发送邮件。我使用了 TLS。下面是名为 SendMail.java 的邮件发送器类

  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); 
  }
}

When I am choosing Run as Java Application from Eclipse I am getting the following error log当我从 Eclipse 中选择 Run as Java Application 时,我收到以下错误日志

       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

I have a properties file named config.properties where I have kept the username and password,and other credentials like port no.Please find below its code:我有一个名为 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

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

I was also trying out the simple javax mail program using the google smtp server .我也在尝试使用 google smtp 服务器的简单 javax 邮件程序。 On receiving Authentication failed messages ,I had to goto https://www.google.com/settings/security/lesssecureapps and turn on access for less secure apps在收到身份验证失败的消息时,我必须转到https://www.google.com/settings/security/lesssecureapps并打开对不太安全的应用程序的访问在此处输入图片说明

  1. Log in to the gmail account specified in your properties file登录到您的属性文件中指定的 gmail 帐户
  2. Click on the circle on your top right corner -> manage your google account -> security -> Less secure app access .点击右上角的圆圈 -> 管理您的谷歌帐户 -> 安全 -> 不太安全的应用程序访问。 Turn it on打开它
  3. Go to https://accounts.google.com/DisplayUnlockCaptcha .转到https://accounts.google.com/DisplayUnlockCaptcha
  4. Click on continue and logout of the gmail.单击继续并注销 Gmail。
  5. Test your service.测试您的服务。 Now it will establish a connection to send mails现在它将建立一个连接来发送邮件

Try with these properties:尝试使用这些属性:

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

Your code sent mail without any exceptions in eclipse on Debian OS.您的代码在 Debian 操作系统上的 eclipse 中毫无例外地发送了邮件。 Are you running any firewall or anti-virus software that might be interfering with the connection attempt?您是否正在运行任何可能干扰连接尝试的防火墙或防病毒软件?

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

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