Request a Delivery / Read Receipt in Javamail

The read receipt (i.e. “Disposition-Notification-To” SMTP header) is a request for the receiving email client to send a DSN (delivery status notification) as soon as the recipient opens the email.

The request for the receipt is sent as a header attached to the mail using the method MimeMessage.setHeader().

Keep in mind that a receipt request may not be always honored because
1) A mail client may not recognize the special Disposition-Notification-To header.
2) The end user may have that functionality turned off or not choose to send one for your particular email.

It’s possible to ask for a delivery receipt too (i.e. “Return-Receipt-To” SMTP header). This kind of receipt is sent as soon as the mail server receives the mail. This one is rarely enabled on a mail server and its recommended usage is for debugging purposes only.

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

class SimpleMail {
    public static void main(String[] args) throws Exception{
      Properties props = new Properties();
      props.setProperty("mail.transport.protocol", "smtp");
      props.setProperty("mail.host", "mydomain.com");
      props.setProperty("mail.user", "user");     // using the mail account
      props.setProperty("mail.password", "pwd");  //     user@mydomain.com

      Session mailSession = Session.getInstance(props, null);
      Transport transport = mailSession.getTransport();

      MimeMessage message = new MimeMessage(mailSession);

      message.setHeader("Disposition-Notification-To", "user@mydomain.com");
      //  message.setHeader("Return-Receipt-To", "user@mydomain.com");

      message.setSubject("Testing javamail plain");
      message.setContent("I'm alive", "text/plain");
      message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress("maixuanviet.com@gmail.com"));

      transport.connect
         ("mail.mydomain.com", 587, "user", "pwd");
      //   mail server       port    user    pwd
      transport.sendMessage(message,
          message.getRecipients(Message.RecipientType.TO));
      transport.close();

      System.out.println("Done.");
    }
}

Done! Happy Coding!

Related posts:

“Stream has already been operated upon or closed” Exception in Java
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Java Program to Generate Random Numbers Using Multiply with Carry Method
Java Program to Perform the Sorting Using Counting Sort
Java Program to Implement Pagoda
Spring Boot - Google Cloud Platform
Spring Boot - Apache Kafka
Multi Dimensional ArrayList in Java
Java InputStream to Byte Array and ByteBuffer
Java Program to Implement Hash Tables Chaining with Binary Trees
JUnit 5 for Kotlin Developers
Java – Reader to Byte Array
Spring Boot - Actuator
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Encode a String to UTF-8 in Java
Java Program to Compute the Area of a Triangle Using Determinants
Java Timer
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Phương thức tham chiếu trong Java 8 – Method References
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Java Program to Generate Random Numbers Using Middle Square Method
Java Program to Implement JobStateReasons API
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Chuyển đổi Array sang ArrayList và ngược lại
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Java Program to Perform Sorting Using B-Tree
Java Program to Find Maximum Element in an Array using Binary Search
Changing Annotation Parameters At Runtime
A Guide to HashSet in Java
Introduction to Using Thymeleaf in Spring