JavaMail - 发件人地址被拒绝:访问被拒绝 [英] JavaMail - Sender Address rejected: Access Denied

查看:200
本文介绍了JavaMail - 发件人地址被拒绝:访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Ok, I don't know what else to do. This code worked perfectly fine a week ago when I wrote and tested it. Then I embedded it into my program and realised I kept getting exceptions. Everything seems normal. The sender address is legit. The recipient addresses I used to test it are legit. What is wrong? I'm so frustrated:

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);

因此例外:

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

任何帮助都将非常受欢迎。谢谢!

Any help would be most appreciated. Thanks!

推荐答案

最后找到了解决办法(虽然我仍然无法理解为什么首先出现问题,看作是用来工作的代码。无论如何......)

Finally figured out a work-around (though I still can't understand why there's a problem in the first place, seeing as the codes used to work. Anyways...)

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;

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

You'll find that the main difference between the codes in the question and these ones are:

Transport.send(msg);

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

原来,运输对象必须使用适当的凭据(端口号,用户名,密码和邮件服务器)创建和连接。

Turns out, a Transport object had to be created and connected using the proper credentials (port number, username, password, and mail server).

此外,我做了一个消除过程并发现了如你所愿:

Also, I did a process of elimination and found out that as long as you have this:

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,以及来自电子邮件地址,用户名和密码,您就可以了:)

Anyway, that's the answer. You can also alter this answer for gmail. Just be sure to change the outgoing mail server to gmail's, as well as the from email address, username and password, and you'll be just fine :)

这篇关于JavaMail - 发件人地址被拒绝:访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆