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 ?
Who is Participating?

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

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.


I don't think it can always diagnose wrong address, only in some obvious cases.
 It will bounce back to From Address otherwise.
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

You can look at this discussion, pretty much in the same vein and same opinion:
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 ?
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) {

                 Properties props = new Properties();
                    Session mailSession = Session.getDefaultInstance(props, null);
         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(""));

        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");
             new InternetAddress(""));
                  new InternetAddress(""));
// second part (the image)
        messageBodyPart = new MimeBodyPart();
        DataSource fds = new FileDataSource
        messageBodyPart.setDataHandler(new DataHandler(fds));
         // add it

     // second image
        messageBodyPart = new MimeBodyPart();
        DataSource fds2 = new FileDataSource
        messageBodyPart.setDataHandler(new DataHandler(fds2));
         // add it
        DataSource fds3 = new FileDataSource
      messageBodyPart.setDataHandler(new DataHandler(fds3));
       // add it

        // put everything together

// this is start of critical part

                              message.setHeader("Return-Receipt-To", "");
                          SMTPMessage smtpMsg = new SMTPMessage(message);

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

    }catch(Exception ex){


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.
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:
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

From novice to tech pro — start learning today.