简体   繁体   English

Spring 启动 API - 发送 email 和来自 URL 的附件

[英]Spring Boot API - Send email with attachment from URL

This is what I have so far.这是我到目前为止所拥有的。 I think I need a line like: helper.addAttachment(attachment);我想我需要这样一行: helper.addAttachment(attachment); in there, but that doesn't compile.在那里,但那不编译。 I just can't figure out the syntax of how to add this attachment.我只是不知道如何添加此附件的语法。 Anyone have any tips?有人有任何提示吗? Thanks!谢谢!

@RequestMapping(value = "/api/sendemailsubmitted")
public void sendEmailSubmitted(@RequestBody VerifyInfo newVerifyInfo) throws MessagingException, MalformedURLException {
    MimeMessage message = sender.createMimeMessage();
    String username = newVerifyInfo.getUsername();

    // Enable the multipart flag!
    MimeMessageHelper helper = new MimeMessageHelper(message, true);

    Part attachment = new MimeBodyPart();
    URL url = new URL("https://www.url.com/applicationsubmitted.pdf");
    attachment.setDataHandler(new DataHandler(url));
    attachment.setDisposition(Part.ATTACHMENT);
    attachment.setFileName(url.getFile());

    helper.setTo(username);
    helper.setFrom("AdmissionsApplication@gmail.com");
    helper.setText("<html><body>" +
            "Please see attachment" +
            "</body></html>", true);
    helper.setSubject("Begin Your Journey!");

    sender.send(message);

}

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/mail/javamail/MimeMessageHelper.html https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/mail/javamail/MimeMessageHelper.html

As you can see here you can just do a message.addAttachment("myDocument.pdf", new ClassPathResource("doc/myDocument.pdf"));正如你在这里看到的,你可以只做一个message.addAttachment("myDocument.pdf", new ClassPathResource("doc/myDocument.pdf"));

No need to create MimeBodyPart MimeBodyPart is used to add it to javax.mail.inte.net.MimeMultipart and then add it to javax.mail.inte.net.MimeMessage, however, you are using Spring with org.springframework.mail.javamail.MimeMessageHelper无需创建 MimeBodyPart MimeBodyPart 用于将其添加到 javax.mail.inte.net.MimeMultipart,然后将其添加到 javax.mail.inte.net.MimeMessage,但是,您使用的是 Spring 和 org.springframework.mail.javamail .MimeMessageHelper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM