如何从 JavaMailSender 异常中检索附件名称?
How can attachment names be retrieved from a JavaMailSender exception?
我正在使用 org.springframework.mail.javamail.JavaMailSender(Spring Framework 4.1.6)。我正在通过以下方式发送多封电子邮件:
mailSender.send(mimeMessagePreparators);
其中 mimeMessagePreparators 是一个 MimeMessagePreparator 数组。每个 MimeMessagePreparator 构建如下:
MimeMessagePreparator mimeMessagePreparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws MessagingException {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
// get the subscribers of the attachment and put them as the recipients
// of this email
mimeMessageHelper.setTo(subscribers);
// all email have the same from, bcc, reply to, subject, and body
String fromEmailAddress = emailTemplate.getFromEmailAddress();
mimeMessageHelper.setFrom(fromEmailAddress);
// note: bcc the sender so that they get the email too
mimeMessageHelper.setBcc(fromEmailAddress);
// this will help on auto replies and bounce messages
// also it should help on deliverability
mimeMessageHelper.setReplyTo(fromEmailAddress);
String subject = emailTemplate.getSubject();
mimeMessageHelper.setSubject(subject);
String emailBody = emailTemplate.getBody();
mimeMessageHelper.setText(OPEN_EMAIL_TAGS + emailBody + CLOSE_EMAIL_TAGS, true);
// get the physical file and add as an email attachment
FileSystemResource file = new FileSystemResource(new File(directory, attachment.getName()));
mimeMessageHelper.addAttachment(attachment.getName(), file);
}
};
我需要知道哪些电子邮件发送失败(即出现 MailException)并最终告诉用户与失败电子邮件关联的附件名称。如何从异常中检索附件名称?到目前为止,我有
try {
mailSender.send(mimeMessagePreparators);
} catch (MailSendException mailSendException) {
Map<Object, Exception> map = mailSendException.getFailedMessages();
for (Map.Entry<Object, Exception> entry : map.entrySet()) {
MimeMessage mimeMessage = (MimeMessage) entry.getKey();
// get attachment names from mimeMessage? or preferably
// get in a more simplistic way using a helper such as MimeMessageHelper
} catch (MailException mailException) {
// how do I get attachment names here?
}
如果您有一堆 MimeMessage objects,请参阅从这里开始的 JavaMail FAQ 条目:
本质上,您需要遍历邮件中的各个部分,确定哪些部分代表附件,然后访问您认为代表附件的部分中的任何元数据或 headers "name"。
我正在使用 org.springframework.mail.javamail.JavaMailSender(Spring Framework 4.1.6)。我正在通过以下方式发送多封电子邮件:
mailSender.send(mimeMessagePreparators);
其中 mimeMessagePreparators 是一个 MimeMessagePreparator 数组。每个 MimeMessagePreparator 构建如下:
MimeMessagePreparator mimeMessagePreparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws MessagingException {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
// get the subscribers of the attachment and put them as the recipients
// of this email
mimeMessageHelper.setTo(subscribers);
// all email have the same from, bcc, reply to, subject, and body
String fromEmailAddress = emailTemplate.getFromEmailAddress();
mimeMessageHelper.setFrom(fromEmailAddress);
// note: bcc the sender so that they get the email too
mimeMessageHelper.setBcc(fromEmailAddress);
// this will help on auto replies and bounce messages
// also it should help on deliverability
mimeMessageHelper.setReplyTo(fromEmailAddress);
String subject = emailTemplate.getSubject();
mimeMessageHelper.setSubject(subject);
String emailBody = emailTemplate.getBody();
mimeMessageHelper.setText(OPEN_EMAIL_TAGS + emailBody + CLOSE_EMAIL_TAGS, true);
// get the physical file and add as an email attachment
FileSystemResource file = new FileSystemResource(new File(directory, attachment.getName()));
mimeMessageHelper.addAttachment(attachment.getName(), file);
}
};
我需要知道哪些电子邮件发送失败(即出现 MailException)并最终告诉用户与失败电子邮件关联的附件名称。如何从异常中检索附件名称?到目前为止,我有
try {
mailSender.send(mimeMessagePreparators);
} catch (MailSendException mailSendException) {
Map<Object, Exception> map = mailSendException.getFailedMessages();
for (Map.Entry<Object, Exception> entry : map.entrySet()) {
MimeMessage mimeMessage = (MimeMessage) entry.getKey();
// get attachment names from mimeMessage? or preferably
// get in a more simplistic way using a helper such as MimeMessageHelper
} catch (MailException mailException) {
// how do I get attachment names here?
}
如果您有一堆 MimeMessage objects,请参阅从这里开始的 JavaMail FAQ 条目:
本质上,您需要遍历邮件中的各个部分,确定哪些部分代表附件,然后访问您认为代表附件的部分中的任何元数据或 headers "name"。