Perfect solution for javax. mail. SendFailedException: an error is thrown for Invalid Address and SMTPAddressFailedException, invalidaddress
A module automatically collects results and sends emails to relevant persons after running the program. An address in the email recipient seems to be faulty, and all recipients cannot be sent.
The error is as follows:
Javax. mail. SendFailedException: Invalid Addresses;
Nested exception is:
Com. sun. mail. smtp. SMTPAddressFailedException: 550 5.7.1 Unable to relay for aerchi@gmail.com
At com. sun. mail. smtp. SMTPTransport. rcp.pdf (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 aerchi@gmail.com
At com. sun. mail. smtp. SMTPTransport. rcp.pdf (SMTPTransport. java: 1047)
... 6 more
Figure: throwing an error
This is the cause of the error. I studied it for a long time.
The improved code is as follows:
Figure A: send the email for the first time.
Figure B: capture and output a new reachable email and assemble it.
Figure C: send an email twice.
Some code is as follows:
try{ Transport.send(msg);//Transport.sendMessage(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. sendFailMail(new MimeMessage(mailSession), mailBody, mailFrom, validAddress); } }catch(MessagingException me) {me.printStackTrace();}} /** * email: aerchi@gmail.com * site: www.aerchi.com * blog: http://blog.csdn.net/aerchi */ //send the mail when mail address wrong. public static void sendFailMail(Message msg, BodyPart mailBody, Address mailFrom, String mailTOAddress ) { try{ Out.println("...Send the mail second time."); msg.setSentDate(new Date());msg.setFrom(mailFrom);String[] mailTOArray=null;mailTOArray=mailTOAddress.split(";");InternetAddress[] mailTOAdd = null;mailTOAdd = new InternetAddress[mailTOArray.length];for(int a=0;a<mailTOArray.length;a++){//Out.println(mailTOArray[a]);mailTOAdd[a]= new InternetAddress(mailTOArray[a]);}msg.setRecipients(Message.RecipientType.TO, mailTOAdd);msg.setSubject(mailSubject);Multipart mailMulti = new MimeMultipart(); //mailBody.setContent(mainText, "text/html;charset=utf-8");mailMulti.addBodyPart(mailBody);msg.setContent(mailMulti);Transport.send(msg); Out.println("...the mail send successful Second. "+Out.getNowDate()); }catch(MessagingException me) {me.printStackTrace();} }
Finally, success depends on success. If the address cannot be reached, you don't have to worry about sending emails to other recipients.
Figure: Send success log
The copyright of the article is owned by the author [Aerchi]. Do not collect it. for reprinting, please indicate the author and the original article address.
Address: http://blog.csdn.net/aerchi/article/details/41692913