gpt4 book ai didi

javax.mail.SendFailedException 发送邮件时地址无效

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

我在使用 java 发送邮件时遇到问题,这是我的代码

package callcentersystem;

public class Email {

private String topic = "my test";
private String myMassage = "nothinge";

public void untitledMethod(String to) {
String host = "my ip adresse";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(utils.Constants.From));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(topic);
message.setText(myMassage);
Transport transport = session.getTransport("smtp");
transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
System.out.println("the massage didnt send");
throw new RuntimeException(mex);
}
}
}

这是我的崩溃按摩

the massage didnt send
Exception in thread "main" java.lang.RuntimeException: javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 530 SMTP authentication is required.

at callcentersystem.Email.untitledMethod(Email.java:33)
at callcentersystem.CallCenterSystem.main(CallCenterSystem.java:10)
Caused by: javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 530 SMTP authentication is required.

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1446)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:736)
at javax.mail.Transport.send0(Transport.java:191)
at javax.mail.Transport.send(Transport.java:120)
at callcentersystem.Email.untitledMethod(Email.java:29)
... 1 more
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 530 SMTP authentication is required.

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1344)
... 5 more

我尝试了所有方法来解决这个问题,但它不起作用,我设置了一个 hmailserver 管理员,并使用它解决了我的本地主机问题

最佳答案

显然,该 SMTP 服务器需要身份验证凭据,从嵌套异常中可以清楚地看出:530 需要 SMTP 身份验证

您需要为 session 设置Authenticator实例:

...
Authenticatior authenticator =
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"youruser@yourhost.com", "yourpassword");
}
};
Session session = Session.getDefaultInstance(properties, authenticator);
...

youruser@yourhost.comyourpassword 更改为您尝试发送邮件的电子邮件服务器上的实际凭据。您需要在此服务器上有一个有效的帐户,因为它需要身份验证。

作为与问题无关的旁注,您确定要使用 Session.getDefaultInstance而不是Session.getInstance ?阅读有关影响的文档。

关于javax.mail.SendFailedException 发送邮件时地址无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41047416/

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