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", "[email protected]");
// mailSender.setJavaMailProperties(props);
mailSender.send(message);
}catch(Exception e){
System.out.println("Error sending amil.....");
}
}
There's no guaranteed way to determine whether a message was delivered successfully or not. If that's your requirement, give up now.
The JavaMail FAQ explains why you don't get an exception when you try to use an invalid address.
Many mail servers simply ignore messages sent to invalid addresses, to prevent spammers from "probing" for valid addresses.
There are internet standards for Delivery Status Notifications, but again many servers don't implement the standard or don't return notifications for bad addresses.
The most reliable way to detect whether a message was delivered successfully is to include a link in the message and request the recipient to click on the link to verify that the message was received. Obviously this is still not perfect because they might never click on the link.
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