【问题标题】:Getting error while sending email through Gmail通过 Gmail 发送电子邮件时出错
【发布时间】:2016-06-03 16:26:07
【问题描述】:

我在这里尝试使用 gmail 发送邮件,但出现以下错误

  javax.mail.AuthenticationFailedException: 534-5.7.14 
  <https://accounts.google.com/ContinueSignIn? 
  sarp=1&scc=1&plt=AKgnsbu7f
  534-5.7.14 Zt- 
  ibTxZf1iCvRrx_zqZ2e0gyU7UDBdKNf3Skj3y1daBQ4lwKDtlbWjuZVSBdqqJvWssPG
  534-5.7.14 
  axQ9afV4DYvgwRA6V94E2JKjGlqxgk8V7wxG9-lgPZoqbzI4rgBIk8SjDYwFt06r7tzWjs

  534-5.7.14 gn4zN1UWm-
 _BhrTGzjP02vV710gi2NHsgX7efxMTbZSowI02n1DL31Qhf_ba5vvtN8mSkI
  534-5.7.14 mutNhiGJSG0_sSI0ZAiblBGGfc1o> Please log in via your web browser and
  534-5.7.14 then try again.
  534-5.7.14  Learn more at
  534 5.7.14  https://support.google.com/mail/answer/78754     vy6sm35491986pac.38 - gsmtp

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:648)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:583)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)

我已在网络浏览器中成功登录,但在通过代码发送时出错。我还没有激活两步验证。 我正在关注这些link

【问题讨论】:

    标签: java gmail gmail-api


    【解决方案1】:

    通过网络浏览器登录 gmail 并点击Clik-this。选择开启按钮。选择后您就可以发送邮件了。

    【讨论】:

    • 这正是我所缺少的。非常感谢@manikant Gautam
    【解决方案2】:
    1. JavaMail – 通过 TLS 的 GMail 使用 TLS 连接通过 Gmail SMTP 服务器发送电子邮件。

      package com.mkyong.common;
      
      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;
      
      public class SendMailTLS {
      
      public static void main(String[] args) {
      
      final String username = "username@gmail.com";
      final String password = "password";
      
      Properties props = new Properties();
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.host", "smtp.gmail.com");
      props.put("mail.smtp.port", "587");
      
      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("from-email@gmail.com"));
          message.setRecipients(Message.RecipientType.TO,
              InternetAddress.parse("to-email@gmail.com"));
          message.setSubject("Testing Subject");
          message.setText("Dear Mail Crawler,"
              + "\n\n No spam to my email, please!");
      
          Transport.send(message);
      
          System.out.println("Done");
      
      } catch (MessagingException e) {
          throw new RuntimeException(e);
      }
      

      } }

    2. JavaMail – 通过 SSL 的 GMail 使用 SSL 连接通过 Gmail SMTP 服务器发送电子邮件。

      package com.mkyong.common;
      
      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;
      
      public class SendMailSSL {
      public static void main(String[] args) {
      Properties props = new Properties();
      props.put("mail.smtp.host", "smtp.gmail.com");
      props.put("mail.smtp.socketFactory.port", "465");
      props.put("mail.smtp.socketFactory.class",
              "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.port", "465");
      
      Session session = Session.getDefaultInstance(props,
          new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication("username","password");
              }
          });
      
      try {
      
          Message message = new MimeMessage(session);
          message.setFrom(new InternetAddress("from@no-spam.com"));
          message.setRecipients(Message.RecipientType.TO,
                  InternetAddress.parse("to@no-spam.com"));
          message.setSubject("Testing Subject");
          message.setText("Dear Mail Crawler," +
                  "\n\n No spam to my email, please!");
      
          Transport.send(message);
      
          System.out.println("Done");
      
      } catch (MessagingException e) {
          throw new RuntimeException(e);
      }
      

      } }

    3. 这是我的参考链接enter link and go reference website

    【讨论】:

      猜你喜欢
      • 2014-01-04
      • 2013-12-16
      • 2017-09-09
      • 1970-01-01
      • 2012-01-07
      • 2023-03-19
      • 2016-07-18
      相关资源
      最近更新 更多