我向无效的电子邮件地址发送邮件,但我没有任何异常。它的工作就像一切都很好,并且在调试器中它显示一切正常。
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();
}
我尝试了不同的电子邮件地址,但并没有抛出异常。我错了什么?
答案 0 :(得分:6)
TransportListener不会告诉您任何超出异常的信息。您问题的真正答案在JavaMail FAQ。
答案 1 :(得分:0)
您需要的是TransportListener,它附加到您的传输实例:
添加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);
}
}
我不会尝试这个(但是),但我认为这正是你所需要的......
的修改
我错了,这对你的问题没有帮助