Getting error while sending email through Gmail

2019-07-23 16:06发布

问题:

Here I am trying to send mail using gmail but getting error below

  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)

I have been logged-in successfully inside my web browser but getting error while sending through code. I haven't activated 2-step verification. I am following these link

回答1:

Do login-to gmail via web browser and click Clik-this.select turn-on button.Once you select you will be able to send mail.



回答2:

  1. JavaMail – GMail via TLS Send an Email via Gmail SMTP server using TLS connection.

    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 – GMail via SSL Send an Email via Gmail SMTP server using SSL connection.

    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. Here is my reference link enter link and go reference website