如何在Spring的电子邮件中发送多重附件?

时间:2015-06-12 03:58:53

标签: spring

我有一些代码可以通过电子邮件发送附件,但我想发送多个附件。这是我目前的代码:

MimeMessage message = mailSender.createMimeMessage();
try{
  MimeMessageHelper helper = new MimeMessageHelper(message, true);
  helper.setFrom(fromEmailId);
  helper.setTo(emailId);
  helper.setSubject(emailSubject);
  String userName = "";

  helper.setText("HI", true);

  File file = new File(System.getenv("CATALINA_HOME") + File.separator + attachmentFolder + File.separator + fileName);


  String filePath = file.getAbsolutePath();
  FileSystemResource fileSystemResource = new FileSystemResource(filePath.trim());
  helper.addAttachment(fileSystemResource.getFilename(), fileSystemResource);
  message.send();

1 个答案:

答案 0 :(得分:2)

以下代码适用于我。

   MimeMessage message = mailSender.createMimeMessage();
try{
   MimeMessageHelper helper = new MimeMessageHelper(message, true);
  helper.setFrom(fromEmailId);
   helper.setTo(emailId);
   helper.setSubject(emailSubject);
   String userName = "";

  helper.setText("HI", true);

    File file = new File(System.getenv("CATALINA_HOME") + File.separator +        attachmentFolder + File.separator + fileName);


  String filePath1 = file.getAbsolutePath();
  FileSystemResource fileSystemResource1 = new      FileSystemResource1(filePath1.trim());
   helper.addAttachment(fileSystemResource1.getFilename(), fileSystemResource1);

   String filePath = file.getAbsolutePath();
  FileSystemResource fileSystemResource = new  FileSystemResource(filePath.trim());
     helper.addAttachment(fileSystemResource.getFilename(), fileSystemResource);

  message.send();