英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论