0

我向无效的电子邮件地址发送消息,但我没有收到任何异常。它就像一切都很好,在调试器中它显示一切正常。

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();

}

我尝试了不同的电子邮件地址,但它并没有抛出异常。我错了什么?

4

2 回答 2

6

TransportListener 不会告诉您任何信息,而只会告诉您异常。您的问题的真正答案在JavaMail FAQ中。

于 2013-02-11T19:21:42.627 回答
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);
    }

}

我还没有尝试这个(还),但我认为这正是你需要的......
编辑
我错了,这对你的问题没有帮助

于 2013-02-11T19:07:08.747 回答