簡體   English   中英

發送電子郵件Java時的異常

[英]Exceptions while sending email Java

我在發送電子郵件時遇到異常問題。 這是我的下面的代碼

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)。 有沒有一種方法可以將它們拆分為不同的異常。

問題是,如果收件人的電子郵件地址無效,則我的程序應與其他收件人一起繼續。 但是,如果該程序無法連接到郵件服務器,則該程序應終止。 但就我而言,即使電子郵件地址無效,它也會終止。 由於(MessagingException e)會引發錯誤,如代碼所示。

是否還有其他例外可以捕獲無效的電子郵件地址? (AddressException e)無法使用無效的電子郵件捕獲錯誤。

謝謝。

我認為,最好在嘗試發送郵件之前先驗證郵件地址。 這是您所知道的,即使在異常發生之前也可以糾正它。 郵件服務器不可訪問是您可以預料到的異常,因此有必要進行捕獲並進行后期處理。

您可以使用SendFailedException 文檔摘錄

無法發送消息時,拋出此異常。

例外包括無法將消息發送到的那些地址,以及消息已發送到的有效地址和消息未發送到的有效地址。

幾天前我遇到了同樣的問題。 嘗試使用

Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });

代替

Session session_m = Session.getDefaultInstance(props, null);

還要重新檢查您的端口號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM