gpt4 book ai didi

JavaMail - 发件人地址被拒绝 : Access Denied

转载 作者:搜寻专家 更新时间:2023-11-01 02:51:43 29 4
gpt4 key购买 nike

好吧,我不知道还能做什么。一周前,当我编写和测试这段代码时,它运行良好。然后我将它嵌入到我的程序中,并意识到我不断收到异常。一切似乎都很正常。发件人地址是合法的。我用来测试的收件人地址是合法的。怎么了?我很沮丧:

private String outgoingMailServer = "smtp.mail.yahoo.com";

boolean debug = true;

//set the host outgoing mail smtp server.
Properties properties = new Properties();
properties.put("mail.smtp.host", outgoingMailServer);
properties.put("mail.smtp.auth", "true");

Authenticator authenticator = new SMTPAuthentication();
Session session = Session.getDefaultInstance(properties, authenticator);

session.setDebug(debug);

//create a message session
Message msg = new MimeMessage(session);

//set the addresses, to and from
InternetAddress fromAddress;
fromAddress = new InternetAddress(emailFromAddress);
msg.setFrom(fromAddress);

//since mail can be sent to more than one recipient, create loop
//to add all addresses into InternetAddress, addressTo.
//InternetAddress[] toAddress = new InternetAddress[recipients.length];
InternetAddress[] toAddress = new InternetAddress[recipients.size()];
for (int i = 0; i < recipients.size(); i++) {
toAddress[i] = new InternetAddress(recipients.get(i));
}
msg.setRecipients(Message.RecipientType.TO, toAddress);

//set the subject and content type
msg.setSubject(emailSubject);
msg.setContent(actualMessage, "text/html; charset=utf-8");

//send the email
Transport.send(msg);

异常(exception)情况是:

javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 554 5.7.1 <blank@yahoo.com>: Sender address rejected: Access denied

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1835)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1098)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at internalLogicEngine.LogicEngine.sendReminder(LogicEngine.java:4282)
at testPackage.Test.main(Test.java:169)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 554 5.7.1 <blank@yahoo.com>: Sender address rejected: Access denied

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

任何帮助将不胜感激。谢谢!

最佳答案

终于想出了一个变通办法(虽然我仍然不明白为什么首先会出现问题,因为代码曾经有效。无论如何......)

private String outgoingMailServer = "smtp.mail.yahoo.com";    
boolean debug = false;

//set the host outgoing mail smtp server.
Properties properties = new Properties();
properties.put("mail.smtp.host", outgoingMailServer);
properties.put("mail.smtps.auth", "true");

Authenticator authenticator = new SMTPAuthentication();
Session session = Session.getDefaultInstance(properties, authenticator);
session.setDebug(debug);

//create a message session
Message msg = new MimeMessage(session);

//set the addresses, to and from
InternetAddress fromAddress;
fromAddress = new InternetAddress(emailFromAddress);
msg.setFrom(fromAddress);

//since mail can be sent to more than one recipient, create loop
//to add all addresses into InternetAddress, addressTo.
//InternetAddress[] toAddress = new InternetAddress[recipients.length];
InternetAddress[] toAddress = new InternetAddress[recipients.size()];
for (int i = 0; i < recipients.size(); i++) {
toAddress[i] = new InternetAddress(recipients.get(i));
}
msg.setRecipients(Message.RecipientType.TO, toAddress);

//set the subject and content type
msg.setSubject(emailSubject);
msg.setContent(actualMessage, "text/html; charset=utf-8");

//send the email
Transport transport = session.getTransport("smtps");
transport.connect(outgoingMailServer, 465, emailUserName, emailPassword);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();

//email sent
//note, this does not necessarily mean the email was delivered. The
//sysetm has no control over that
emailSent = true;

您会发现问题中的代码与这些代码之间的主要区别是:

Transport.send(msg);

Transport transport = session.getTransport("smtps");
transport.connect(outgoingMailServer, 465, emailUserName, emailPassword);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();

事实证明,必须使用正确的凭据(端口号、用户名、密码和邮件服务器)创建并连接一个Transport对象。

另外,我做了一个淘汰的过程,发现只要你有这个:

Transport transport = session.getTransport("smtps");
transport.connect(outgoingMailServer, 465, emailUserName, emailPassword);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();

你不需要这个:

Authenticator authenticator = new SMTPAuthentication();
Session session = Session.getDefaultInstance(properties, authenticator);

以上也可以是:

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

无论如何,这就是答案。您还可以更改 gmail 的此答案。只需确保将外发邮件服务器更改为 gmail 的服务器,以及发件人电子邮件地址、用户名和密码,就可以了:)

关于JavaMail - 发件人地址被拒绝 : Access Denied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10005837/

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