通过Gmail发送电子邮件时出错

时间:2016-02-22 09:45:50

标签: java gmail gmail-api

这里我尝试使用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

2 个答案:

答案 0 :(得分:2)

通过网络浏览器登录Gmail,然后点击Clik-this。选择开启按钮。一旦选择,您就可以发送邮件。

答案 1 :(得分:0)

  1. JavaMail - GMail通过TLS 使用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