gpt4 book ai didi

javax.mail.AuthenticationFailedException : Error authenticating with server

转载 作者:太空宇宙 更新时间:2023-11-04 06:54:44 26 4
gpt4 key购买 nike

我正在尝试使用 Java Mail API 通过 Exchange Server (Office 365) 发送邮件。以下是我的代码:

package com.package;

import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class Mail {

ResourceBundle rb = ResourceBundle.getBundle("settings", Locale.ENGLISH);

public void sendMail(String body, String subject, String receipients) throws MessagingException ////this is used to send the emails
{

Message message = new MimeMessage(getSession());

message.addRecipient(RecipientType.TO, new InternetAddress(receipients));
message.addFrom(new InternetAddress[] { new InternetAddress(rb.getString("from")) });

message.setSubject(subject);
message.setContent(body, "text/plain");

Transport.send(message);
}

private Session getSession() {
Authenticator authenticator = new Authenticator();

Properties properties = new Properties();
System.out.println("Submitter : " + authenticator.getPasswordAuthentication().getUserName());
properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable","true");
properties.setProperty("mail.smtp.host", "smtp.office365.com");
properties.setProperty("mail.smtp.port", "587");

return Session.getInstance(properties, authenticator);
}

private class Authenticator extends javax.mail.Authenticator {

String username, password;
public Authenticator() {
username = rb.getString("from");
password = rb.getString("password");
}

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}

public static void main(String[] args) {
try {
new Mail().sendMail("Testing Mail", "Test", "someuser@ymail.com");
System.out.println("Mail sent");
} catch (MessagingException e) {
// TODO Auto-generated catch block
System.out.println("Unable to send mail");
e.printStackTrace();
}
}
}

当我运行此程序时,控制台显示以下内容:

>>>>>Sending data EHLO HP-WIN8<<<<<<
>>>>>Sending data STARTTLS<<<<<<
>>>>>Sending data EHLO HP-WIN8<<<<<<
>>>>>Sending data AUTH LOGIN<<<<<<
>>>>>Sending data ImFzaGZhcS5tZW1vbkBzdHJlZWJvLmNvbSI=<<<<<<
>>>>>Sending data IlN1bmlAMTk5MSI=<<<<<<
>>>>>Sending data QUIT<<<<<<
Unable to send mail
javax.mail.SendFailedException: Send failure (javax.mail.AuthenticationFailedException: Error authenticating with server)
at javax.mail.Transport.send(Transport.java:163)
at javax.mail.Transport.send(Transport.java:48)
at com.streebo.Mail.sendMail(Mail.java:33)
at com.streebo.Mail.main(Mail.java:66)
Caused by: javax.mail.AuthenticationFailedException: Error authenticating with server
at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:402)
at javax.mail.Service.connect(Service.java:265)
at javax.mail.Service.connect(Service.java:85)
at javax.mail.Service.connect(Service.java:70)
at javax.mail.Transport.send(Transport.java:94)
... 3 more

我已经检查了我的邮件 ID 和密码,它们是正确的,但我仍然遇到此异常。

请检查我的程序是否有问题。

最佳答案

由于我在使用创建的 gmail.com 测试帐户时遇到了同样的问题(假设使用了正确的凭据),一个可能的原因可能是

您可能使用了通过 gmail/yahoo/hotmail 托管/创建的电子邮件进行测试。

如果您收到 javax.mail.AuthenticationFailedException,请尝试使用凭据登录邮件帐户,gmail 会要求您输入验证码。登录成功后即可使用Java邮件API发送邮件。

服务提供商这样做是为了防止垃圾邮件发送者,为了进行测试,我们创建一个帐户,并且不会将其用于任何其他用途。

关于javax.mail.AuthenticationFailedException : Error authenticating with server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22905845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com