JavaMail API : Username and Password not accepted (Gmail)

爱⌒轻易说出口 提交于 2020-02-02 13:56:09

问题


I want to send email from my java application to gmail account I used the following code and javaMail API ,But Gmail not accepted username and password and exception thrown Can anybody help me how can I fix this issue?

MailService.java

public class MailService {

    String email;
    String content;
    public void sendMail(String email,String content)
    {
        this.email=email;
        this.content=content;
        // Recipient's email ID needs to be mentioned.
          String to = email;

          // Sender's email ID needs to be mentioned
          String from = senderemail@gmail.com;
          final String username = "myusername";//change accordingly
          final String password = "*******";//change accordingly

          // Assuming you are sending email through relay.jangosmtp.net
          String host = "smtp.gmail.com";

          Properties props = new Properties();
          props.put("mail.smtp.auth", "true");
          props.put("mail.smtp.starttls.enable", "true");
          props.put("mail.smtp.host", host);
          props.put("mail.smtp.port", "587");

          // Get the Session object.
          Session session = Session.getInstance(props,
             new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                   return new PasswordAuthentication(username, password);
           }
             });

          try {
           // Create a default MimeMessage object.
           Message message = new MimeMessage(session);

           // Set From: header field of the header.
           message.setFrom(new InternetAddress(from));

           // Set To: header field of the header.
           message.setRecipients(Message.RecipientType.TO,
                   InternetAddress.parse(to));

           // Set Subject: header field
           message.setSubject("Did you get my message?");

           // Now set the actual message
           message.setText(content);

           // Send message
           Transport.send(message);

           System.out.println("Sent message successfully....");

          } catch (MessagingException e) {
             throw new RuntimeException(e);
          }
       }
    }

回答1:


To be able send mail messages via your gmail account, you should allow unsecure applications (which your application is by gmail's point of view) in google account security settings.

UPD: Also, if you want to see debug messages, use next java mail property:

props.put("mail.debug", "true");

It can help you to find out what happening behind the scenes.




回答2:


You can try to allow unsecure applications, it worked for me.

Steps for Gmail account :

  1. Sign in to Gmail
  2. Click here to access Less Secure App Access in My Account.
  3. Next to “Allow less secure apps: OFF,” select the toggle switch to turn ON.

See this link for Gmail and G-Suite.



来源:https://stackoverflow.com/questions/43406528/javamail-api-username-and-password-not-accepted-gmail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!