完美解决 javax.mail.SendFailedException: 与 com.sun.mail.smtp.SMTPAddressFailedException: 抛错
邮件里有个地址貌似出问题,导致所有的收件人都发送不出去。
抛出的错误如下:
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
图示: 抛错
这个是致使的错误呀,研究了半天。
改进代码如下:
图A: 首次发送邮件。
图B: 捕获并输出不可达邮件,组装新的可达邮件。
图C: 二次发送邮件。
部分代码如下
<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> //调用再次发送代码 sendFailMail(new MimeMessage(mailSession), mailBody, mailFrom, validAddress); } }catch(MessagingException me) {me.printStackTrace();} ---------------------------------------------------------------------------- 补调用代码: /** * email: [email protected] * site: www.aerchi.com * blog: http://blog.csdn.net/aerchi<span style="white-space:pre"> </span> */ //send the mail when mail address wrong. public static void sendFailMail(Message msg, BodyPart 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> }
最后,大功靠成。如有地址不到到达,再也不用担心导致其它收件人不能收到邮件啦。
本文版本所 有,转载请注明原文地址。
本文地址: http://blog.csdn.net/aerchi/article/details/41692913
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。