【问题标题】:javax.mail.sendfailedexception: Sending failed; nested exception is: class javax.mail.authenticationfailedexception at sendjavax.mail.sendfailexception:发送失败;嵌套异常是:发送时的类 javax.mail.authenticationfailexception
【发布时间】:2014-08-28 17:24:45
【问题描述】:

我是 java 邮件的新手,我想发送一封带有附件的邮件,所以我想用来自互联网的示例代码创建一个测试邮件,但我得到了

javax.mail.sendfailedexception: sending failed;
nested exception is: class javax.mail.authenticationfailedexception 
at javax.mail.transport.send0(Transport.java.218)
at javax.mail.transport.send(Transport.java.80)

我尝试了不同的身份验证,但我失败了

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

public class SendEmail
{
   public static void main(String [] args)
   {

      // Recipient's email ID needs to be mentioned.
      String to = "hari@gmail.com";

      // Sender's email My Office mail server
      String from = "hari8750@access.co.in";
      String pass = "Password";
      String host = "172.23.5.10";
      String port = "25";

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server
      properties.setProperty("mail.smtp.host", host);
      properties.setProperty("mail.smtp.port", port);
      properties.setProperty("mail.smtp.auth", "true");

      // Get the default Session object.
      Session session = Session.getInstance(properties,new MailAuthentication(from,pass));

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

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

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

         // Set Subject: header field
         message.setSubject("Test Mail");

         // Create the message part 
         BodyPart messageBodyPart = new MimeBodyPart();

         // Fill the message
         messageBodyPart.setText("Test Mail Success Hari");

         // Create a multipar message
         Multipart multipart = new MimeMultipart();

         // Set text message part
         multipart.addBodyPart(messageBodyPart);

         // Send the complete message parts
         message.setContent(multipart );

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

import javax.mail.*
public class MailAuthentication extends Authentication
{

    String _user;
    String _pass;
    public GMailAuthenticator (String username, String password)
    {
        super();
        this._user = username;
        this._pass = password;
    }
    public PasswordAuthentication getPasswordAuthentication()
    {
        return new PasswordAuthentication(_user, _pass);
    }
}

我也试过

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

Session session = Session.getDefaultInstance(properties);

Session session = Session.getDefaultInstance(properties);

但我遇到了同样的错误。

请帮我解决这个问题

我正在使用类似 java 环境变量的 Windows PC SMTP 有什么步骤吗??

提前致谢。

【问题讨论】:

  • 检查您的用户名和密码值。他们正确吗?
  • 亲爱的@SamwiseGamgee 用户名和密码是正确的,我在浏览器中直接登录端口 172.23.5.10(公司邮件服务器),Outlook 也配置了这个 IP 和用户名密码。
  • 检查protocol trace 以获取有关服务器拒绝您的用户名和密码的更多线索。您可能必须将“mail.debug.auth”属性设置为“true”才能获得完整的协议跟踪。请注意不要在此处发布它,因为它可能会暴露您的密码。

标签: java email smtp jakarta-mail smtp-auth


【解决方案1】:

您可以切换到 Gmail 等 Google 开发的应用来访问您的帐户(推荐)或更改您的 settings 以便您的 account is no longer protected by modern security standards

【讨论】:

    【解决方案2】:

    您必须添加mail.smtp.sslmail.smtp.sender.address 属性.. mail.smtp.sender.addressfrom 属性相同。 它必须工作..它按我的预期工作.. 希望对你有帮助

    如果还不能解决你的问题,就分享一下引起的异常堆栈跟踪..

    【讨论】:

    • 亲爱的 @Mr.Chowdary 我将属性修改为 properties.setProperty("mail.smtp.host", host); properties.setProperty("mail.smtp.port", 端口); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.smtp.ssl.enable", "true"); properties.setProperty("mail.smtp.ssl.trust", "*"); properties.setProperty("mail.smtp.sender.address", username); properties.setProperty("mail.smtp.from", 用户名);根据你的建议,但我也没有得到
    • javax.mail.SendFailedException:发送失败;嵌套异常是: com.access.SendEmail.main(SendEmail) 的 javax.mail.transport.send(Transport.java:80) 的 javax.mail.transport.send0(Transport.java:218) 的类 javax.mail.authenticationfailedexception .java:90) 错误。
    • @Hari 通过将主机作为 smtp.gmail.com 和 gmail id 和密码传递给 gmail.com 首先检查它。然后再次检查您的具体值。如果两者都不起作用意味着您的代码问题.. 如果 gmail 工作意味着您的 access.co.in 邮件服务器的输入值是错误的.. 检查它并让我知道.. :)
    猜你喜欢
    • 1970-01-01
    • 2015-11-20
    • 2023-03-10
    • 2012-11-10
    • 1970-01-01
    • 2014-04-15
    • 2016-11-22
    • 2017-07-11
    • 2013-03-29
    相关资源
    最近更新 更多