get delivery and send receipt using java email

Viewed 2019

I am using below code. I am tring to get send receipt and delivery receipt for mail. still its not working for this code.

I have tried to add Properties, but of no use. I have a requirement in which i need to check whether an email is delivered successfully or not. I have written the code for email delivery but it prints success even for invalid email address.How to track whether email is delivered or not using javamail API

import java.io.ByteArrayOutputStream;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import javax.activation.DataSource;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.util.ByteArrayDataSource;

import org.apache.log4j.Logger;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.util.StringUtils;

@Service
public class VelocityMailServiceImpl implements MailService {

    private static final Logger logger = Logger.getLogger(MailService.class);

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private VelocityEngine velocityEngine;

    @Autowired
    private MessageSource messageSource;

    @Value("$env{email.smtp.host}")
    private String emailHost;

    /**
     * This method will send compose and send the message
     * */
    public void sendMail(String to, String from, String subject, String body) {
        SimpleMailMessage message = new SimpleMailMessage();
        String[] emailTos = null;

        try{

        if (to.contains(";")) {
            emailTos = to.split(";");
        } else if (to.contains(",")) {
            emailTos = to.split(",");
        } else {
            emailTos = new String[] { to };
        }
        message.setTo(emailTos);
        message.setFrom(from);
        //message.addFrom(InternetAddress.parse(from)); 
        message.setSubject(subject);
        message.setText(body);
        Properties props = new Properties();
    //  props.put("Return-Receipt-To", "admin@xyz.com"); 
    //  mailSender.setJavaMailProperties(props);
        mailSender.send(message);
        }catch(Exception e){
            System.out.println("Error sending amil.....");
        }
    }
2 Answers

The Read Receipt 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("Disposition-Notification-To", "email-id@domain.com");

It's possible to ask for a delivery receipt too. This kind of receipt is sent as soon as the mail server receives the email. The request for the receipt is sent as a header attached to the mail using the method MimeMessage.setHeader("Return-Receipt-To", "email-id@domain.com");

Note : There are 2 scenarios in which read receipts won't work as expected,

  1. When user opens an email for the message a pop-up confirmation window opens, if user selects No then end user wont receive a read receipt.
  2. From email settings, If user selects Never send a read receipt then also end user wont receive a read receipt.
Related