通过SpringFramework发送带有JavaMailSender附件的电子邮件

这是一篇简短的“如何”文章,介绍如何使用SpringFramework发送电子邮件。 我将提供一些示例,并说明一个受欢迎的问题。

可以像这样实现一个简单的任务:

public void send(String subject, String from, String to, File file) throws MessagingException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setSubject(subject); helper.setFrom(from); helper.setTo(to); helper.setReplyTo(from); helper.setText("stub", false); helper.addAttachment("document.txt", file); javaMailSender.send(message); } 

但是,当Web应用程序直接与文件系统一起工作时,这不是典型的情况。 让我们考虑在内存中创建文档并将其附加到电子邮件中的示例。

MimeMessageHelper类中有几种方法可以帮助我们:

 public class MimeMessageHelper { ... public void addAttachment(String attachmentFilename, InputStreamSource inputStreamSource) { ... } public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException { ... } } 

让我们看一些例子。

1.附件是一个InputStreamSource接口

这是一个棘手的问题,因为在这种情况下,开发人员可能会收到带有消息的IllegalArgumentException

传入的资源包含一个开放的流:无效的参数。JavaMail需要一个InputStreamSource,它为每个调用创建一个新鲜的流。

发生这种情况是因为在MimeMessageHelper#addAttachment()方法中进行了特殊检查:

 if (inputStreamSource instanceof Resource && ((Resource) inputStreamSource).isOpen()) { throw new IllegalArgumentException( "Passed-in Resource contains an open stream: invalid argument. " + "JavaMail requires an InputStreamSource that creates a fresh stream for every call."); } 

例如,实现InputStreamResource总是从方法isOpen()返回true ,这使得不可能将此实现用作附件:

  public class InputStreamResource extends AbstractResource { //... @Override public boolean isOpen() { return true; } //... } 

工作示例是:

 public void send() { try { final ByteArrayOutputStream stream = createInMemoryDocument("test document text"); final InputStreamSource attachment = new ByteArrayResource(stream.toByteArray()); sendMimeMessageWithAttachments( "subject", "random@random.com", "random@random.com", attachment); } catch (IOException | MailException | MessagingException e) { logger.warn(e.getMessage(), e); } } private void sendMimeMessageWithAttachments(String subject, String from, String to, InputStreamSource source) throws MessagingException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setSubject(subject); helper.setFrom(from); helper.setTo(to); helper.setReplyTo(from); helper.setText("stub", false); helper.addAttachment("document.txt", source); javaMailSender.send(message); } private ByteArrayOutputStream createInMemoryDocument(String documentBody) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); outputStream.write(documentBody.getBytes()); return outputStream; } 

2.附件是一个数据源接口

这个例子不包含陷阱,很清楚:

 public void send() { try { final ByteArrayOutputStream document = createInMemoryDocument("test document text"); final InputStream inputStream = new ByteArrayInputStream(document.toByteArray()); final DataSource attachment = new ByteArrayDataSource(inputStream, "application/octet-stream"); sendMimeMessageWithAttachments( "subject", "anonymous@xyz-mail.com", "anonymous@xyz-mail.com", attachment); } catch (IOException | MailException | MessagingException e) { logger.warn(e.getMessage(), e); } } private void sendMimeMessageWithAttachments(String subject, String from, String to, DataSource dataSource) throws MessagingException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setSubject(subject); helper.setFrom(from); helper.setTo(to); helper.setReplyTo(from); helper.setText("stub", false); helper.addAttachment("document.txt", dataSource); javaMailSender.send(message); } 

看看弹簧参考章节会有所帮助。

Source: https://habr.com/ru/post/zh-CN439176/


All Articles