在Tomcat7中获取javax.mail.AuthenticationFailedException

时间:2014-06-25 10:39:03

标签: java smtp javamail

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

  package com.edifixio.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.edifixio2014@gamil.com\">warm.edifixio2014@gamil.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@edifixio.com", "Hello", "h", "Rakesh", "rakesh@gmail.com");
    System.out.println("Successfully send mail to - "+toAddresses); 
  }
}

当我从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.edifixio.dashboard.mail.SendMail.sendMail(SendMail.java:80)
       at com.edifixio.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=test
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=edifixio,dc=co,dc=in
ldap_dir_context=dc=edifixio,dc=co,dc=in
ldap_security_principal=cn=Directory Manager,dc=edifixio,dc=co,dc=in
ldap_security_credential=edifixio

5 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

我也在使用google smtp服务器尝试简单的javax邮件程序。收到身份验证失败的消息后,我不得不转到https://www.google.com/settings/security/lesssecureapps并为不太安全的应用打开访问权限 enter image description here

答案 2 :(得分:1)

  1. 登录到您的属性文件中指定的gmail帐户
  2. 单击右上角的圆圈->管理您的Google帐户->安全->安全访问权限较低的应用。开启
  3. 转到https://accounts.google.com/DisplayUnlockCaptcha
  4. 单击“继续”并注销gmail。
  5. 测试您的服务。现在它将建立连接以发送邮件

答案 3 :(得分:0)

尝试使用这些属性:

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

答案 4 :(得分:0)

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