Perfect solution to Javax.mail.SendFailedException: with com.sun.mail.smtp.SMTPAddressFailedException: Throw the wrong

Source: Internet
Author: User

Since a module is used to run the program, automatically collect the results to send messages to the relevant people.


There is an address in the message that looks like a problem, causing all the recipients to send out.


The following errors are thrown:

Javax.mail.SendFailedException:Invalid Addresses;
Nested exception is:
com.sun.mail.smtp.smtpaddressfailedexception:550 5.7.1 Unable to relay for [email protected]


At Com.sun.mail.smtp.SMTPTransport.rcptTo (smtptransport.java:1196)
At Com.sun.mail.smtp.SMTPTransport.sendMessage (smtptransport.java:584)
At Javax.mail.Transport.send0 (transport.java:169)
At Javax.mail.Transport.send (transport.java:98)
At AtGroup.ServerBasicCheck.ServerCheck.sendMail (servercheck.java:1651)
At AtGroup.ServerBasicCheck.ServerCheck.callRun (servercheck.java:500)
At AtGroup.ServerBasicCheck.ServerCheck.main (servercheck.java:310)
caused by:com.sun.mail.smtp.smtpaddressfailedexception:550 5.7.1 Unable to relay for [email protected]


At Com.sun.mail.smtp.SMTPTransport.rcptTo (smtptransport.java:1047)
... 6 more

Illustration: Wrong throw



This is the cause of the mistake Ah, studied for half a day.

The improvement code is as follows:


Figure A: Sending the message for the first time.


Figure B: Capturing and outputting unreachable messages, assembling new unreachable messages.



Figure C: Send the message two times.



Some of the code below


<span style= "White-space:pre" ></span>try{<span style= "White-space:pre" ></span> Transport.send (msg); Out.println ("The Mail send successful first." +out.getnowdate ());} catch (Unsupportedencodingexception e) {e.printstacktrace ();}  catch (Sendfailedexception se) {se.printstacktrace ();//Exception ex = me;//if (ex instanceof sendfailedexception) {}  address[] unsend = se.getvalidunsentaddresses (); if (null!=unsend) {//Out.println ("==valid Addresses"); String validaddress = ""; for (int i=0;i<unsend.length;i++) {validaddress + = Unsend[i] + ";"; /Out.println ((i+1) + ":" + Unsend[i]);} validaddress = validaddress.substring (0,validaddress.length ()-1);//Out.println ("All:" +validaddress);//Send the Mail when mail address wrong.<span style= "White-space:pre" ></span>//Call again to send code sendfailmail (new MimeMessage (mailsession), Mailbody, Mailfrom, validaddress); }}catch (Messagingexception me) {me.printstacktrace ();} ----------------------------------------------------------------------------Fill Call code: /**     * Email: [email protected] * site:www . aerchi.com * Blog:http://blog.csdn.net/aerchi<span style= "White-space:pre" ></span>      */   //send The mail address wrong.    public static void Sendfailmail (Message msg, Bodypar T mailbody, Address Mailfrom, String mailtoaddress)     {    <span style= "White-space:pre" >< /span>try{      <span style= "White-space:pre" ></span>out.println ("... Send the mail second time. ");     <span style= "White-space:pre" ></span>msg.setsentdate (New Date ()); <span style= " White-space:pre "></span><span style=" White-space:pre "></span>msg.setfrom (mailFrom);< Span style= "White-space:pre" ></span><span style= "White-space:pre" ></span><span style= " White-space:pre "></span>string[] Mailtoarray=nuLl;<span style= "White-space:pre" ></span><span style= "White-space:pre" ></span>mailTOArray =mailtoaddress.split (";"); <span style= "White-space:pre" ></span><span style= "White-space:pre" ></span> internetaddress[] Mailtoadd = Null;<span style= "white-space:pre" ></span>mailtoadd = new InternetAddress[ Mailtoarray.length];<span style= "White-space:pre" ></span>for (int a=0;a<mailtoarray.length;a++) <span style= "White-space:pre" ></span><span style= "White-space:pre" ></span>{//<span Style= "White-space:pre" ></span>out.println (Mailtoarray[a]); <span style= "White-space:pre" ></ span>mailtoadd[a]= new InternetAddress (Mailtoarray[a]) <span style= "White-space:pre" ></span>< Span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></span><span style= " White-space:pre "></span><span style=" White-space:pre "></span>msg.setrecipientS (Message.RecipientType.TO, mailtoadd); <span style= "White-space:pre" ></span><span style= " White-space:pre "></span><span style=" White-space:pre "></span>msg.setsubject (MailSubject); <span style= "White-space:pre" ></span><span style= "White-space:pre" ></span>Multipart Mailmulti = new Mimemultipart (); <span style= "White-space:pre" ></span>//<span style= "White-space:pre" ></span> Mailbody.setcontent (MainText, "Text/html;charset=utf-8"); <span style= "White-space:pre" ></span>< Span style= "White-space:pre" ></span>mailmulti.addbodypart (mailbody); <span style= "White-space:pre" ></span><span style= "White-space:pre" ></span>msg.setcontent (mailMulti); <span style= " White-space:pre "></span><span style=" White-space:pre "></span><span style=" White-space: Pre "></span>transport.send (msg);    <span style= "White-space:pre" ></span>out.println ("... the Mail send successful Second." +out.getnowdate ());    <span style= "White-space:pre" ></span>}catch (messagingexception me) { Me.printstacktrace ();}    <span style= "White-space:pre" ></span>   }





Finally, the merit depends. If you have an address that doesn't arrive, don't worry about causing other recipients to receive the email.





This version of all, reproduced please specify the original address.


This address: http://blog.csdn.net/aerchi/article/details/41692913



Perfect solution to Javax.mail.SendFailedException: with com.sun.mail.smtp.SMTPAddressFailedException: Throw the wrong

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.