java - 通过 gmail 发送电子邮件时身份验证失败

标签 java email jakarta-mail

我尝试从我的应用程序发送电子邮件,但一次又一次收到身份验证失败错误。我已经尝试了给定的解决方案,但它们不起作用,因此我想放置我的代码。请查看下面的代码并告诉我是否缺少重要的内容。

package managimg.stud.data;

import java.util.Date; 
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import sendmail.Sendmail;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMail {
   public static void main(String...a){
      SendMail s = new SendMail();  
   }

   public SendMail(){

      Authenticator authenticate = new Authenticator(){
         public PasswordAuthentication getPasswrodAuthentication(){
            return new PasswordAuthentication("abcd","xyz");
         }
      };

      String to = "toUser@gmail.com";
      String from = "abcd@gmail.com";
      String host = "smtp.gmail.com";

      Properties properties = System.getProperties();
      properties.setProperty("mail.smtp.host", "smtp.gmail.com");
      properties.setProperty("mail.smtp.port", "587");
      properties.setProperty("mail.smtp.starttls.enable", "true") ;
      properties.setProperty("mail.smtp.auth", "true") ;
      properties.setProperty("mail.smtp.user", "abcd"); 
      properties.setProperty("mail.smtp.password", "xyz");
      properties.setProperty("mail.smtp.debug", "true");

      Session session = Session.getDefaultInstance(properties,authenticate);

      try{

         MimeMessage message = new MimeMessage(session);
         message.setFrom(new InternetAddress(from));
         message.addRecipient(Message.RecipientType.TO,
                              new InternetAddress(to));
         message.setSubject("This is the Subject Line!");
         message.setText("This is actual message");

         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }

   }

}

有人可以告诉我这里出了什么问题吗?我缺少什么?我一次又一次地收到以下错误。我已经尝试了很多解决方案,但它不起作用。有人可以帮我解决这个问题吗?

javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:306)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at managimg.stud.data.SendMail.<init>(SendMail.java:139)
at managimg.stud.data.SendMail.main(SendMail.java:31)

最佳答案

这就是我解决错误的方法。请找到下面的代码,刚才我发送了一封测试电子邮件。

package gmail.email;

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
import javax.mail.PasswordAuthentication;


public class GmailEmail {

    final String userName ="pan54321@gmail.com";
    final String password="tqw12";

    public static void main(String[] args) {
        new GmailEmail();
    }

    public GmailEmail(){
        Properties properties = new Properties();
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.port", "587");
        properties.put("mail.smtp.starttls.enable", "true") ;
        properties.put("mail.smtp.auth", "true") ;

        Session session = Session.getInstance(properties,new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication(){
                return new PasswordAuthentication(userName, password);
            }

        });

        try{
            Message message = new MimeMessage(session);

            message.setFrom(new InternetAddress("pan54321@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                                  InternetAddress.parse("vi@gmail.com"));
            message.setSubject("my First Email");
            message.setContent("<h:body>You wrote first email</body>","text/html;     charset=utf-8");
            Transport.send(message);
        }catch(MessagingException
               messageException){
            throw new RuntimeException(messageException);
        }

    }

}

我只使用了两个 jar :

  1. 邮件.jar
  2. javaee-api-6.0.jar

我的JDK版本是JDK6.0。

关于java - 通过 gmail 发送电子邮件时身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25592992/

相关文章:

java - 如何在java中的arraylist中添加异常

Microsoft Azure 中自定义域的电子邮件设置

spring - 如何为使用 Spring 的 JavaMailSenderImpl 发送的电子邮件设置内容类型

java.lang.NoClassDefFoundError : Could not initialize class javax. mail.internet.InternetAddress

通过 JavaMail 发送邮件时,Message-Id 被替换

java - 如何访问数组的枚举

java - 在浏览器上实现视频聊天的最佳方式

ios - UIActivityViewController - 保留从 URL 读取的附加文件的文件名

python - 转换字符串参数以发送电子邮件可以帮助我(python)

java - 监听某些变化并将其更新到数据库