I have a simple java mail code where I am trying to get a confirmation via an email or through any means possible of whether or not the email was delivered successfully, for now I have tried out the following code, the Email is sent fine, just that I am not able to get a delivery confirmation.
import java.io.File;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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 com.sun.mail.smtp.SMTPMessage;
import net.htmlparser.jericho.Source;
public class SmtpMail {
/**
* @param args
*/
public static void main(String[] args) {
String to = "[email protected]"; //both emails are valid
String from = "[email protected]";
String host = "relay.myorg.com";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.transport.protocol", "SMTP");
Session session = Session.getDefaultInstance(properties);
SMTPMessage smtpMessage = new SMTPMessage(session);
try {
smtpMessage.setSender(new InternetAddress(from));
smtpMessage.setRecipient(Message.RecipientType.TO,
new InternetAddress(to));
smtpMessage.setSubject("Hello Test Email");
smtpMessage.setText("Test Email to check Delivery Report");
smtpMessage.setReturnOption(SMTPMessage.RETURN_FULL);
smtpMessage.setNotifyOptions(SMTPMessage.NOTIFY_SUCCESS);
Transport.send(smtpMessage);
} catch (Exception e) {
System.out.println("Error while building smpt email");
}
}
}
Am I on the right track? Do I need to refer something else? I tried to learn more about Delivery Status Notification but so far have not been able to find a working example.
Looks good. Make sure that your smtp server supports Delivery Status Notification.
Sending an EHLO using telnet should be good enough; it should output DSN. Something like
[root@blah ~]$ telnet localhost 25
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 blah ESMTP Sendmail 8.14.3/8.14.3/Debian-9.1ubuntu1; Fri, 28 Jun 2013 10:30:28 -0400; (No UCE/UBE) logging access from: localhost(OK)-localhost [127.0.0.1]
EHLO toto
250-blah Hello localhost [127.0.0.1], pleased to meet you
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-EXPN
250-VERB
250-8BITMIME
250-SIZE
250-DSN
250-ETRN
250-AUTH DIGEST-MD5 CRAM-MD5
250-DELIVERBY
250 HELP
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With