Reputation: 53
Is there any way to check email status when I'm sending email? I know at least 3 statuses: sent, deferred, denied, and I want to do some logic depends on it. I've tried a lot of libraries like javax.mail, simpejavamail and didn't find any way to get status.
My last attempt was:
String from = "********";
String pass = "*********";
// String[] to = { "to_mail_address@****.com" };
String host = "smtp.yandex.ru";
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.quitwait", "false");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.debug", "true");
Session session = Session.getDefaultInstance(props, null);
SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
transport.connect(from,pass);
SMTPMessage message = new SMTPMessage(session);
message.setReturnOption(2);
message.setNotifyOptions(7);
message.setFrom(new InternetAddress("******"));
message.setRecipients(
Message.RecipientType.TO, InternetAddress.parse("******"));
message.setSubject("Test mail");
String msg = "This is my first email using JavaMailer";
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(msg, "text/html; charset=utf-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);
message.setContent(multipart);
transport.sendMessage(message,InternetAddress.parse("[email protected]"));
var i1 = transport.getLastReturnCode();
var i2 = transport.getReportSuccess();
return transport.getLastServerResponse();
}
I get response from server like this:
250 2.0.0 Ok: queued on vla5-3832771863b8.qloud-c.yandex.net 1656683490-NIUBgcx5PS-pUSKMoc1
It does not change if I send email to an invalid address.
So, I have a couple of questions
Upvotes: 0
Views: 1003
Reputation: 109077
Sending email is asynchronous, your SMTP server accepted the message, and will try to deliver it. If delivery fails (from your SMTP to the next SMTP server, or from an intermediate SMTP server to the final SMTP server, or from the final SMTP server to the mailbox), a bounce message will be generated, which will be sent to the envelope sender address of the message.
If you want to know about delivery failures, you need to monitor the mailbox of the envelope sender address, and parse the received bounces (e.g. using JakartaMail's dsn.jar, specifically com.sun.mail.dsn.DeliveryStatus
). See also RFC 3464.
In theory, you can track successful delivery by requesting delivery status notification (which are basically "positive" bounces, delivered in the same way), but in my experience, a lot of mail servers do not honor requests for DSNs. So, in general it is better to simply assume successful delivery as long as you haven't received a bounce.
Upvotes: 2