how resolve Address Invalid exception

浪子不回头ぞ 提交于 2019-12-06 23:43:14

问题


We tried to send mail using javax.mail. While sending mails we got following exception:

    **sendMail - Message Sending Failed: Invalid Addresses;
    nested exception is: 
javax.mail.SendFailedException: 550 #5.1.0 Address rejected.2013-02-28 13:17:08,236** 

What might be the problem?


回答1:


It means that the receiving server does not recognise the mailbox (the part before the '@') of the e-mail address. It could be that it was misspelled, that it is simply a non-existing name, or it could even be that the receiving server was set to reject a message (e.g. spam) by replying with code 550.

Here is one of many pages that summarises the SMTP reply codes, and gives links to various relevant RFCs: http://www.greenend.org.uk/rjk/tech/smtpreplies.html.

EDIT: I need a bit more space to answer your question than the comments allow.

@RaghuKing, if you look at the Javadoc for javax.mail.SendFailedException, you will notice that you can call 3 methods on such an exception object (inside the catch block):

  • getInvalidAddresses() to get an array of addresses that are invalid and thus not sent to,
  • getValidSentAddresses() to get an array of addresses to which this message was sent succesfully, and
  • getValidUnsentAddresses() to get an array of addresses that are valid but to which the message was not sent to.

(Obviously, if one sends a message to multiple recipients, some may succeed and some fail, but the exception is thrown if there is at least one failure, regardless of how many successes. Obviously also if you are sending to only one address, you will have that one address in only one of these arrays, and it will probably NOT be in the ValidSent list.

These arrays will give you more information how to handle the exception, depending of the type of array an address is in. This will obviously depend on you application, but these might be reasonable suggestions:

  • Invalid Addresses: tell the user that the message was not sent because the address was wrong, for each invalid address in the list, and provide a way to correct the address, then try to resend to the correct address (or cancel if the user does not provide a different address);
  • Valid Sent Addresses: Don't resend;
  • Valid Unsent Addresses: Try to resend to these addresses. Sending probably stopped before getting to these addresses because of a previous incorrect address.

But in the end it is you who has to apply common sense, and perhaps experiment a little with the functions you don't understand until you understand them.




回答2:


This code can print logs for invalid address(es):

    try {
        sender.send(message);
    }catch (MailSendException me){
        detectInvalidAddress(me);
    } 

private void detectInvalidAddress(MailSendException me) {
    Exception[] messageExceptions = me.getMessageExceptions();
    if (messageExceptions.length > 0) {
        Exception messageException = messageExceptions[0];
        if (messageException instanceof SendFailedException) {
            SendFailedException sfe = (SendFailedException) messageException;
            Address[] invalidAddresses = sfe.getInvalidAddresses();
            StringBuilder addressStr = new StringBuilder();
            for (Address address : invalidAddresses) {
                addressStr.append(address.toString()).append("; ");
            }

            logger.error("invalid address(es):{}", addressStr);
            return;
        }
    }

    logger.error("exception while sending mail.", me);
}



回答3:


Had experienced this same exception .I realized that i could not send email to unknown users . After consulting , i found out that our SMTP server was not an open mail relay server read Open mail Relay.



来源:https://stackoverflow.com/questions/15131868/how-resolve-address-invalid-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!