我编写了代码,用于从我公司的域名向客户端发送邮件。现在我想跟踪,邮件是否发送给接收者。如果邮件被退回,我也想要通知。那我能做些什么呢?
@Override
public void sendEmail(EmailDTO emailDTO) throws EmailException {
Session emailSession = authenticateMailServer();
if (emailSession != null && StringUtils.isNotBlank(emailDTO.getEmailTo())) {
try {
Message message = new MimeMessage(emailSession);
if (PMBSSubstypeConstants.CURRENT_SUBS_TYPE_ID <= 0) {
SubsType subsType = em.find(SubsType.class, 1);
SubsTypeUtils.setCurrentSubsType(subsType);
}
//if (StringUtils.isBlank(emailDTO.getEmailFrom())) {
emailDTO.setEmailFrom(PMBSSubstypeConstants.MAIL_SERVER_EMAIL); //Fallback if from email is not received from UI
//}
message.setFrom(new InternetAddress(PMBSSubstypeConstants.MAIL_SERVER_EMAIL));//emailDTO.getEmailFrom()));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailDTO.getEmailTo()));
if (StringUtils.isNotBlank(emailDTO.getEmailCC())) {
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(emailDTO.getEmailCC()));
}
if (StringUtils.isNotBlank(emailDTO.getEmailBCC())) {
message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(emailDTO.getEmailBCC()));
}
message.setSubject(emailDTO.getEmailSubject());
message.setContent(emailDTO.getEmailBody(), "text/html; charset=utf-8");
if (StringUtils.isNotBlank(emailDTO.getReplayTO())) {
Address address[] = new Address[]{
new InternetAddress(emailDTO.getReplayTO())
};
message.setReplyTo(address);
}
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}