我的要求是为我们的客户向各种客户发送电子邮件警报,我们计划为此使用 JavaMail API。但是,一旦邮件发送完毕,我们需要根据邮件的状态将数据库中的状态更新为已发送/已送达/失败。请告诉我如何获取到达收件人邮件服务器的邮件的送达通知。没有必要确保此人是否已阅读邮件,但是,如果我们能够知道这一点,那就太好了。必须检查的是交货。我们如何获得状态。我读到的是使用“SMTPMessage”我们可以获得状态,但是,我找不到有关如何读取通知的代码示例。我正在放置我的代码,这是我到目前为止所做的非常示例。请让我知道如何实现我们正在努力实现的目标。
public class MailSender {
private int port = 25;
private String host = "testmailsrvr";
private String from = "test@test.com";
private boolean auth = true;
private String username = "test";
private String password = "test@123";
private Protocol protocol = Protocol.SMTP;
private boolean debug = true;
public void sendEmail(String strMailID, String strSubject, String strBody) throws MessagingException{
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
switch (protocol) {
case SMTPS:
props.put("mail.smtp.ssl.enable", true);
break;
case TLS:
props.put("mail.smtp.starttls.enable", true);
break;
case SMTP:
props.put("mail.smtp.ssl.enable", false);
break;
}
Authenticator authenticator = null;
if (auth) {
props.put("mail.smtp.auth", true);
authenticator = new Authenticator() {
private PasswordAuthentication pa = new PasswordAuthentication(username, password);
@Override
public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
};
}
Session session = Session.getInstance(props, authenticator);
session.setDebug(debug);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = {new InternetAddress(strMailID)};
message.setRecipients(Message.RecipientType.TO, toAddress);
message.setSubject(strSubject);
message.setSentDate(new Date());
message.setText(strBody);
Transport.send(message);
}
}
最佳答案
您需要阅读这些 JavaMail FAQ 条目:
- > If I send a message to a bad address, why don't I get a SendFailedException or TransportEvent indicating that the address is bad?
- > When a message can't be delivered, a failure message is returned. How can I detect these "bounced" messages?
然后阅读 com.sun.mail.smtp 的 javadocs和 com.sun.mail.dsn包裹以了解递送状态通知。
关于java - 已发送邮件的送达确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21089347/