gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:19:31 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com