4

わかりました、他に何をすべきかわかりません。このコードは、1週間前に作成してテストしたときに、完全に正常に機能しました。次に、それをプログラムに埋め込んで、例外が発生し続けることに気付きました。すべてが正常に見えます。送信者アドレスは正当です。私がそれをテストするために使用した受信者のアドレスは合法です。なにが問題ですか?私はとてもイライラしています:

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

どんな助けでも大歓迎です。ありがとう!

4

1 に答える 1

5

最終的に回避策を見つけました(コードが機能していたので、そもそもなぜ問題があるのか​​まだ理解できません。とにかく...)

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に変更し、差出人のメールアドレス、ユーザー名、パスワードを変更してください。問題はありません:)

于 2012-04-05T21:28:41.113 に答える