• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 514
  • Last Modified:

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
0
abuyusuf35
Asked:
abuyusuf35
  • 5
  • 4
1 Solution
 
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.
0
 
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
0
 
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
abuyusuf35Author Commented:
Anyone has a solid example which uses DeliveryStatus, MessageHeaders & MultipartReport ?
Thanks
0
 
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

0
 
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.
0
 
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
0
 
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
0
 
abuyusuf35Author Commented:
cancel
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now