gpt4 book ai didi

spring - JavaMail 需要一个 InputStreamSource 来为每次调用创建一个新的流

转载 作者:行者123 更新时间:2023-12-04 01:50:20 29 4
gpt4 key购买 nike

我正在尝试通过 Spring 2.5 的 MailSender 使用 JavaMail 发送附件,但我不断收到此错误:

Passed-in Resource contains an open stream: invalid argument.
JavaMail requires an InputStreamSource that creates a fresh stream for every call.

我正在使用 InputStreamResource :
InputStream crofileInputStream = emailDraft.getAttachmentCroFile().getInputStream();
InputStream nacfileInputStream = emailDraft.getAttachmentNacFile().getInputStream();
InputStream sourcefileInputStream = emailDraft.getAttachmentSourceFile().getInputStream();

InputStreamSource[] attachments = {new InputStreamResource(crofileInputStream),new InputStreamResource(nacfileInputStream),new InputStreamResource(sourcefileInputStream)};

sentEmailLog = mailSenderService.sendMIMEMessage(emailDraft, attachmentFileNames, attachments);

最后一条指令调用
每个附件的 MimeMessageHelper.addAttachment(fileName,attachments[i])

请问我怎么解决这个问题?

谢谢你的帮助。

最佳答案

尝试使用 javax.mail.util.ByteArrayDataSource

String mailTo = message.getHeaders().get("emailAddress", String.class); 
MimeMessage mimeMessage = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
ByteArrayDataSource attachment = new ByteArrayDataSource(message.getPayload(), "application/octet-stream");
helper.addAttachment("document.zip", attachment);
helper.setText("text content of the email");

您还可以查看此示例:
https://www.javatips.net/api/javax.mail.util.bytearraydatasource

关于spring - JavaMail 需要一个 InputStreamSource 来为每次调用创建一个新的流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40590696/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com