春季启动 Base64 字符串作为邮件附件

huangapple go评论85阅读模式
英文:

Spring boot Base64 String as mail attachment

问题

我将一个PPTX文件转换成了Base64字符串,现在我想将这个字符串作为电子邮件附件使用。

之前我曾经使用以下这部分代码从本地路径发送文件,它能够正常工作:

public void sendEmail(Mail email) throws MailException, MessagingException {

    MimeMessage message = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setTo(email.getEmail());
    helper.setText(email.getMessage());

    FileSystemResource file  = new FileSystemResource(email.getPath());
    helper.addAttachment("testfile.pptx", file);

    helper.setSubject(email.getSubject());
    javaMailSender.send(message);
}

请问我需要怎样修改/添加代码,才能将这个Base64字符串作为附件文件发送呢?谢谢。

英文:

I converted a PPTX file into Base64 string and now i want to use this string as an email attachmen

i used to use this part of code for sending file from local path and it works fine

   public void sendEmail(Mail email) throws MailException, MessagingException {

        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(email.getEmail());
        helper.setText(email.getMessage());

        FileSystemResource file  = new FileSystemResource(email.getPath());
        helper.addAttachment("testfile.pptx", file);

        helper.setSubject(email.getSubject());
        javaMailSender.send(message);
    }

what i must change/add to send this base64 string as an attachement file ? . thank you

答案1

得分: 3

我解决了

       
public void sendEmail(Mail email) throws MailException, MessagingException {

    MimeMessage message = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setTo(email.getEmail());
    helper.setText(email.getMessage());
    byte[] doc = Base64.getDecoder().decode(email.getBase64());

    helper.addAttachment("doc.pptx", new ByteArrayResource(doc));

    helper.setSubject(email.getSubject());
    javaMailSender.send(message);
}
英文:

I solved it

public void sendEmail(Mail email) throws MailException, MessagingException {

    MimeMessage message = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setTo(email.getEmail());
    helper.setText(email.getMessage());
    byte[] doc = Base64.getDecoder().decode(email.getBase64());

    helper.addAttachment("doc.pptx", new ByteArrayResource(doc));

    helper.setSubject(email.getSubject());
    javaMailSender.send(message);
}

huangapple
  • 本文由 发表于 2020年8月18日 16:52:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63465102.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定