我的电子邮件有问题

时间:2017-04-02 17:20:00

标签: java email javamail

我在使用下面的代码处理我的程序中的电子邮件时遇到问题。 我收到这个错误: 线程“main”中的异常java.lang.RuntimeException:javax.mail.AuthenticationFailedException:535-5.7.8不接受用户名和密码。了解更多信息 535 5.7.8 https://support.google.com/mail/?p=BadCredentials u15sm14739609wrc.10 - gsmtp

at SendEmail.main(SendEmail.java:36)

引起:javax.mail.AuthenticationFailedException:535-5.7.8不接受用户名和密码。了解更多信息 535 5.7.8 https://support.google.com/mail/?p=BadCredentials u15sm14739609wrc.10 - gsmtp

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:932)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:843)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:748)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:195)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
at SendEmail.main(SendEmail.java:31)

这是我的代码:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEmail {

   public static void main(String [] args) {    

   final String username = "ae513309@mail.com"; //sender's email
   final String password = "xxxxxxxxxxx";//sender's 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"); //This is the smtp server address
   props.put("mail.smtp.port", "587");//This is the port for the smtp server

   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("ae513309@mail.com")); //from sender's email address
       message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("be513309@mail.com")); //to receivere's email address
       message.setSubject("Module Registration"); //Sets the subject of the email
       message.setContent("You're selected modules have been registered","text/html; charset=utf-8");//sets the body of the email
       Transport.send(message);//sends the entire message object

       System.out.println("Email was sent!");
   }
   catch (MessagingException fnfe){
       throw new RuntimeException(fnfe);//if the email address is bad or doesn't exist
   }       
  }
}

1 个答案:

答案 0 :(得分:0)

=> OP,你读过JavaMail with Gmail: 535-5.7.1 Username and Password not accepted吗? - @RC。

谢谢@RC。 :)我使用链接中的代码并调整了一些内容:)