A TransportListener won't tell you anything more than an exception will tell you. The real answer to your question is in the JavaMail FAQ.
Transport sendMessage doesn't throw exception
-
09-03-2022 - |
Question
I send message to invalid email address, but I doesn't get any exception. It works like everything is good, and in debugger it shows as everything is normal.
private MailService() {
Properties props = new Properties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", HOST);
props.put("mail.smtps.auth", "true");
props.put("mail.smtp.from", FROM);
props.put("mail.smtps.quitwait", "false");
mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
}
public static void sendMessage(String recipient, String subject,
String message) throws MessagingException {
if (mailService == null) {
mailService = new MailService();
}
MimeMessage mimeMessage = new MimeMessage(mailSession);
mimeMessage.setFrom(new InternetAddress(FROM));
mimeMessage.setSender(new InternetAddress(FROM));
mimeMessage.setSubject(subject);
mimeMessage.setContent(message, "text/plain");
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
Transport transport = mailSession.getTransport("smtps");
transport.connect(HOST, PORT, USER, PASSWORD);
transport.sendMessage(mimeMessage,
mimeMessage.getRecipients(Message.RecipientType.TO));
transport.close();
}
I tried for different email addresses, and for all it didn't throw exception. What do I wrong ?
Solution
OTHER TIPS
What you need is an TransportListener, which is attached to your transport-instance:
adding TransportListener
transport.addTransportListener(new MyTransportListener());
MyTransportListener.java
public class MyTransportListener implements TransportListener{
@Override
public void messageDelivered(TransportEvent te) {
System.out.println("Everything went fine!");
}
@Override
public void messageNotDelivered(TransportEvent te) {
System.out.println("ERROR: " + te.getMessage());
Address[] invalidAddresses = te.getInvalidAddresses();
Address[] validUnsentAddresses = te.getValidUnsentAddresses();
//make something happen
}
@Override
public void messagePartiallyDelivered(TransportEvent te) {
messageNotDelivered(te);
}
}
I din't try this (yet), but I think this is exactly what you need...
EDIT
I was wrong, this won't help with your problem
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow