Hello All,
         I am using JavaMail API to send emails in a loop,but the problem is that, if any one of the email address in that loop is Invalid,it throws me an Exception as follows and the loop terminates....without send the remaining mails....
 
 
 
"javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.SendFailedException:  Invalid Addresses;
nested exception is:
javax.mail.SendFailedException: 450 <
[EMAIL PROTECTED]>: Recipient
address rejected: Domain not found"
 
 
 
       The Loop from where i am calling the JavaMail API,i.e is MailBean, is as follows....
 

<code>...
 
Object objEmail[] = emailAddressesCol.toArray();
for(int j=0;j<objEmail.length;j++)
{
String emailAddresses = (String)objEmail[j];
EMailMessage eMess = new EMailMessage(subject,message,emailAddresses,fromEmailAddress,newsLetterUrl);
MailerHome mailerHome = EJBUtil.getMailerHome();
Mailer mailer = mailerHome.create();
mailer.sendMail(eMess);
}
 
</code>...
 
 
 
The code for sending mail in the JavaMail API (Mail Bean) is as follows....
 
<code>...
 
public void createAndSendMail(EMailMessage eMess) throws MailerAppException {
      
try {
    InitialContext ic = new InitialContext();
    Session session = (Session) ic.lookup(JNDINames.MAIL_SESSION);
    if (Debug.debuggingOn)
        session.setDebug(true);
 
// construct the message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(eMess.getEmailSender()));
String to = "";
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));
msg.setRecipients(Message.RecipientType.BCC,
                     InternetAddress.parse(eMess.getEmailReceiver(), false));          
msg.setSubject(eMess.getSubject());
msg.setContent(eMess.getHtmlContents(),"text/plain"); 
msg.saveChanges();         
Transport.send(msg);
System.out.println ("The Email Has been Sent Successfully..... ");
 
Debug.println("\nMail sent successfully.");
} catch (Exception e) {
    System.out.println ("the MailHelper step is............ 6 "+e);
    Debug.print("createAndSendMail exception : " + e);
    throw new MailerAppException("Failure while sending mail");
}
}//end of method...
 
<code>..

 I am sending hundereds of emails every day(into a loop one after the other) but the problem is that if any one of the email is Invalid in that loop, the loop gets Terminated at that Invalid Address and throws me an exception and because of which i could not send the remaining Emails....
 What i really want to do is ...i want to catch the Exception because of an Invalid Address,handle it accordingly and continue with the loop,so that i will be able to send all the emails in that loop even though an exception has occurred.....
 
 Can anybody guide me in this problem...any suggestions will be greatly appreciated...
 
Thanks a million in Advance...
Regards
Sam
 
      
 
 

Reply via email to