【问题标题】:I'm getting an error in the authentication process in java mail API我在 java 邮件 API 的身份验证过程中遇到错误
【发布时间】:2019-03-08 01:42:26
【问题描述】:

这是我的 java 邮件 API 代码:

import java.io.File;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.activation.*;

import hostel.BabyAuthenticator;

class BabyAuthenticator extends Authenticator
{
   @Override
   protected PasswordAuthentication

   getPasswordAuthentication() 
   {
     PasswordAuthentication pa = new 
     PasswordAuthentication("my email-id","password of my account"); 

     return pa;
   }
}

我已经尝试了 2-3 个不同的电子邮件 ID 作为发件人的 ID ..但仍然出现错误。

  public class SendMail {
    public static void main(String[] args) {
      try 
      {
        Properties p = new Properties();

        p.put("mail.smtp.host","smtp.gmail.com");
        p.put("mail.smtp.port","587");
        p.put("mail.smtp.starttls.enable","true");
        p.put("mail.smtp.auth","true");
        p.put("mail.smtp.ssl.trust", "smtp.gmail.com");

        BabyAuthenticator auth = new BabyAuthenticator();
        Session session = Session.getInstance(p,auth);

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("sender's email id"));

        InternetAddress receiver1 = new InternetAddress("receiver's email id");
        InternetAddress[] rcvrs = {receiver1};

        // specify the the type of Recipients
        message.addRecipients(RecipientType.TO,rcvrs);

        // provide the subject of email
        message.setSubject("First mail");

        // create object of MimeBodyPart to denote the body parts of mail 
        MimeBodyPart part1 = new MimeBodyPart();

        // associate some text to the body part 
        part1.setContent("<i style='color : blue'>"+ "This is my first mail"+" 
            </i>","text/html");

        MimeMultipart allParts = new MimeMultipart();

        allParts.addBodyPart(part1);
        message.setContent(allParts);
        Transport.send(message);

        // show message on console
        System.out.println("Mail has been sent to the mail server...");  
      } 
      catch (Exception e) 
      {
       System.out.println("Some error has occured, and error is "+e);
      }
  }
} 

这是代码的输出:

发生了一些错误,错误是 javax.mail.AuthenticationFailedException:535-5.7.8 用户名和 不接受密码。在 535 5.7.8 了解更多信息 https://support.google.com/mail/?p=BadCredentialsi72sm13717325pfj.147 -gsmtp

【问题讨论】:

    标签: java


    【解决方案1】:

    我也遇到了这个问题,并通过以下方式解决了这个问题:

    • 允许不太安全的应用访问您的谷歌帐户Learn more
    • 已切换到网络提供商,是的,这是真的,我尝试了 3 个网络提供商,并且发送/接收邮件适用于 2 个但不适用于其他网络提供商,所以我建议您也尝试切换网络提供商。

    【讨论】:

      猜你喜欢
      • 2017-01-29
      • 2015-02-27
      • 2018-01-29
      • 2021-10-06
      • 2012-02-14
      • 2014-06-08
      • 1970-01-01
      • 2013-05-25
      • 2015-06-18
      相关资源
      最近更新 更多