【问题标题】:Sending Email with Attachments in Spring Boot在 Spring Boot 中发送带有附件的电子邮件
【发布时间】:2019-03-28 23:47:55
【问题描述】:

我正在尝试在 Spring Boot 中发送带有文件附件的电子邮件。

这是一个基本的 gmail SMTP 服务器应用程序属性配置:

这是我的电子邮件服务:

EmailService

当我通过 mailMessageDto 对象调用此方法时,没有抛出异常。什么都没有发生,电子邮件没有发送。

我已经调试了 javaMailSender.send(messsage) 代码行,一切似乎都很好。

更新

spring.mail.properties.mail.smtp.ssl.enable=false

应该是 false 不是 true spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory

【问题讨论】:

    标签: java spring email spring-boot


    【解决方案1】:

    我建议您通过提取有关添加附件的功能来将SRP 应用于sendMessageWithAttachment() 方法:

    private void addAttachments(MailMessageDto message, MimeMessageHelper helper) {
      message.getFiles().forEach(file -> addAttachment(file, helper));
    }
    

    此方法streams 覆盖所有文件并使用addAttachment() 添加每个文件:

    private void addAttachment(File file, MimeMessageHelper helper) {
      String fileName = file.getName();
      try {
        helper.addAttachment(fileName, file);
        log.debug("Added a file atachment: {}", fileName);
      } catch (MessagingException ex) {
        log.error("Failed to add a file atachment: {}", fileName, ex);
      }
    }
    

    这将为每个失败的附件记录一个错误。你可以试试这个方法吗?

    【讨论】:

    • 另外我发送的是一个文件数据源,而不仅仅是一个文件。而且它不适用于这种方法。
    • @FrankLabry 为什么不能调用getFile() 方法?这也意味着您在 Java 11 中的方法 will not work 无需更改软件。
    • 问题是我在客户端从 RestController 检索 multipartFiles。但是,在这种情况下,您必须从服务器端将它们作为 FileSystemResource 发送,而不仅仅是作为 File。我之前的评论写错了它的名字。
    • @FrankLabry 好的,那为什么不FileSystemResource.getFile()
    【解决方案2】:

    步骤1.在porm.xml中添加依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    

    第二步,在application.properties中添加配置代码

    spring.mail.host=smtp.gmail.com
    spring.mail.port=465
    spring.mail.username=username
    spring.mail.password=password
    spring.mail.protocol=smtps
    spring.mail.properties.mail.smtp.auth=true
    spring.mail.properties.mail.smtp.connectiontimeout=5000
    spring.mail.properties.mail.smtp.timeout=5000
    spring.mail.properties.mail.smtp.writetimeout=5000
    spring.mail.properties.mail.smtp.starttls.enable=true
    

    步骤 3. 在控制器中添加代码 masterconroller.java

    @GetMapping("/sendmail")
    @ResponseBody
    String home() {
        try {
            masterServiceImpl.sendEmail("path");
            return "Email Sent!";
        } catch (Exception ex) {
            return "Error in sending email: " + ex;
        }
    }
    

    步骤 4. 在 MasterServiceImpl.java 中添加代码

    @Autowired
    private JavaMailSender javaMailSender;
    public void sendEmail(String path) throws Exception{
             MimeMessage message = javaMailSender.createMimeMessage();
             MimeMessageHelper helper = new MimeMessageHelper(message, true);
             helper.setTo("xyz@gmail.com");
             helper.setText("<html><body><h1>hello Welcome!</h1><body></html>", true);
             FileSystemResource file  = new FileSystemResource(new File(path));
             helper.addAttachment("testfile", file);
             helper.addAttachment("test.png", new ClassPathResource("test.jpeg"));
             helper.setSubject("Hi");
             javaMailSender.send(message);
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多