JavaMail

When sending a mail with Java mail API how do I detect if the email failed delivery due to for example an incorrect email address - I am catching SendFailedException but even after giving a incorrect email address the exception does not appear to get thrown ? Any ideas ?
Thanks
abuyusuf35Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:

I don't think it can always diagnose wrong address, only in some obvious cases.
 It will bounce back to From Address otherwise.
for_yanCommented:
My impression was that SendFailedException will mostly happen if there is some problem before or in communication with smtp server
If it submits to smtp server - it is in the hands of the server - you proabbly need to do
something very special to figure out the future fate of your message
for_yanCommented:

You can look at this discussion, pretty much in the same vein and same opinion:
http://www.velocityreviews.com/forums/t127909-javamail-does-not-throw-exceptions.html
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

abuyusuf35Author Commented:
Anyone has a solid example which uses DeliveryStatus, MessageHeaders & MultipartReport ?
Thanks
for_yanCommented:
This worked for me with one SMTP server (sends delivery notification and
return -receript), but didn't work with another one

import com.sun.mail.smtp.SMTPMessage;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;

public class SendEmailWithImage {

    public static void main(String[] args) {

                       try{
                 Properties props = new Properties();
        props.put("mail.smtp.host","smtp.company.com");
                    Session mailSession = Session.getDefaultInstance(props, null);
        //mailSession.setDebug(true);
         MimeMessage message = new MimeMessage(mailSession);
       // Transport transport = mailSession.getTransport();
   MimeMultipart multipart = new MimeMultipart("related");
// first part  (the html)
        BodyPart messageBodyPart = new MimeBodyPart();

        message.setSubject("Emailer Test - HTML  mail with images - please confirm");
        message.setFrom(new InternetAddress("my.address@company.com"));

        String htmltext = "<HTML><BODY border=\"3\" background=\"cid:bg\">" +
        "<TABLE border=3 background=cid:bg><p> running testing with embedded images, please confirm to the following email if you can see the images." +
        		"<h1>This is test 1</h1><img src=\"cid:image\"> <br/> " +
        		"<h1>This is test 2</h1><img src=\"cid:image2\"></TABLE></BODY></HTML>";
        System.out.println("html text: \n" + htmltext);
           messageBodyPart.setContent(htmltext, "text/html");
             multipart.addBodyPart(messageBodyPart);
          message.addRecipient(Message.RecipientType.TO,
             new InternetAddress("my.address@company.com"));
                  message.addRecipient(Message.RecipientType.BCC,
                  new InternetAddress("my.address@company.com"));
// second part (the image)
        messageBodyPart = new MimeBodyPart();
        DataSource fds = new FileDataSource
          ("C:\\temp\\test\\Example_1.gif");
        messageBodyPart.setDataHandler(new DataHandler(fds));
        messageBodyPart.setHeader("Content-ID","<image>");
         // add it
        multipart.addBodyPart(messageBodyPart);

     // second image
        messageBodyPart = new MimeBodyPart();
        DataSource fds2 = new FileDataSource
          ("C:\\temp\\test\\Example_2.gif");
        messageBodyPart.setDataHandler(new DataHandler(fds2));
        messageBodyPart.setHeader("Content-ID","<image2>");
         // add it
        multipart.addBodyPart(messageBodyPart);
        DataSource fds3 = new FileDataSource
        ("C:\\temp\\test\\Example_3.gif");
      messageBodyPart.setDataHandler(new DataHandler(fds3));
      messageBodyPart.setHeader("Content-ID","<bg>");
       // add it
      multipart.addBodyPart(messageBodyPart);

        // put everything together
        message.setContent(multipart);


// this is start of critical part

                              message.setHeader("Return-Receipt-To", "my.address@company.com");
             message.setHeader("Disposition-Notification-To","my.address@company.com");        
                            
                          SMTPMessage smtpMsg = new SMTPMessage(message);
smtpMsg.setReturnOption(SMTPMessage.RETURN_HDRS);
smtpMsg.setNotifyOptions(
  SMTPMessage.NOTIFY_DELAY|SMTPMessage.NOTIFY_FAILURE| SMTPMessage.NOTIFY_SUCCESS);


//this is end of crutical part      
                     
      //  transport.connect();
        System.out.println("message " + message.toString());
        Transport.send(smtpMsg);
      //  transport.close();
              System.out.println("message sent");

    }catch(Exception ex){
     ex.printStackTrace();


}
}
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
abuyusuf35Author Commented:
This didn't work for me. I have a class implementing TransportListener which returns a status on messages delivered or not - the only issue is that for some email addresses I don't get a status until the email has bounced. Is there a way to get this information before hand or by reading messages from the SMTP server.
for_yanCommented:
it doesn't look that you can have uniform way - it seems to depend on configuration of smtp server - what works on one doesn't work on another - even withe same server it may depend on the next level contact configuration
abuyusuf35Author Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for abuyusuf35's comment http:/Q_27379988.html#36917479

for the following reason:

none of the suggested solutions answered my question
abuyusuf35Author Commented:
cancel
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.