java - Java 发送电子邮件时出现异常

标签 java email exception

我在发送电子邮件时遇到异常问题。下面是我的代码

public static void sendEmail(String email, String subjectBody, String srcAndFile) throws Exception {
    System.out.println(srcAndFile);

    try {
        logger.debug("sending email to: " + email + "with attached file: " + srcAndFile);

        Properties props = System.getProperties();
        props.put("mail.smtp.host", address);
        props.put("mail.smtp.port", "25");
        props.put("mail.smtp.auth", "false");

        Session session_m = Session.getDefaultInstance(props, null);
        MimeMessage message = new MimeMessage(session_m);
        message.setFrom (new InternetAddress(sender, sender));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
        message.setSubject(subjectBody);
        message.setText("Hi");
        message.setHeader("Content-Type","text/plain;charset=windows-1251");

        Multipart multiPart = new MimeMultipart();
        MimeBodyPart messageText = new MimeBodyPart();
        messageText.setContent(subjectBody, "text/plain");
        multiPart.addBodyPart(messageText);

        MimeBodyPart rarAttachment = new MimeBodyPart();
        File f = new File(srcAndFile);
        FileDataSource rarFile = new FileDataSource(f);
        rarAttachment.setDataHandler(new DataHandler(rarFile));
        rarAttachment.setFileName(rarFile.getName());
        multiPart.addBodyPart(rarAttachment);

        message.setContent(multiPart);

        SMTPTransport t = (SMTPTransport)session_m.getTransport("smtp");
        t.connect(addrress, sender, null);
        t.sendMessage(message, message.getAllRecipients());
        success = true;

        } catch (AddressException e) {
            logger.error(e.getMessage());
            throw new AddressException("[sendEmail]: Incorrect email address");

        } catch (MessagingException e) {
            logger.error(e.getMessage());
            throw new MessagingException("[sendEmail]: Unable to send email");

        } catch (IOException e) {
            logger.error(e.getMessage());
            throw new IOException("[sendEmail]: Unable to find file to attach");

        } catch (Exception e) {
            logger.error(e.getMessage());
            DBWrapper.processStatusDB("[sendEmail]","failed",e.getMessage());
            throw  new Exception("[sendEmail]: Error in method " + e.getMessage());
        }
        DBWrapper.processStatusDB("[sendEmail]","finished","process to send an email with " + FileManager.getFile(srcAndFile) + " has finished properly");


}

现在,当我想捕获一些错误时,问题就出现了:

  1. 地址无效

  2. 无法连接到服务器

这两种情况都会被捕获 (MessagingException e)。有没有办法将它们分成不同的异常(exception)。

问题是,如果收件人的电子邮件地址无效,我的程序应该继续与其他收件人联系。但如果程序无法连接到邮件服务器,则程序应该终止。但就我而言,即使电子邮件地址无效,它也会终止。由于 (MessagingException e) 抛出错误,如代码所示。

还有其他异常可以捕获无效电子邮件地址吗? (AddressException e) 未捕获无效电子邮件的错误。

谢谢。

最佳答案

您可以使用SendFailedException 。文档摘录

This exception is thrown when the message cannot be sent.

The exception includes those addresses to which the message could not be sent as well as the valid addresses to which the message was sent and valid addresses to which the message was not sent.

关于java - Java 发送电子邮件时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19091923/

相关文章:

保存对象/Hibernate 时出现 java.lang.NullPointerException

C++ 从电子邮件 vector 中删除

java - 为什么我的异常(exception)不起作用

java - 了解 Spring AOP 拦截器开销

java - java中的spark流与mongodb

java - CellUtil : Key type in createCell method

html - Outlook 2013 中的电子邮件通讯第一个空表格单元格高度问题

使用 Intent 的 Android 多个电子邮件附件

java - (Java) 如何定义允许用户输入哪些字母/数字/符号?

未捕获 C++ 异常(Qt 项目)