从 bytearrayoutputstream 添加附件到邮件
Add attachment to mail from bytearrayoutputstream
我正在尝试发送带有附件的电子邮件:
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, false, CharEncoding.UTF_8);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
message.addAttachment("facture.pdf", new ByteArrayResource(IOUtils.toByteArray(is)));
我收到一个错误:
java.lang.IllegalStateException: Not in multipart mode - create an
appropriate MimeMessageHelper via a constructor that takes a
'multipart' flag if you need to set alternative texts or add inline
elements or attachments.
有没有办法让它继续使用 addAttachment 方法?
从 MimeMessageHelper 的文档看来,您只需传递一个 true
标志。
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, CharEncoding.UTF_8);
您必须将多部分模式指定为构造函数的第二个参数。
multipart模式有多种选择:
MULTIPART_MODE_NO
MULTIPART_MODE_MIXED
MULTIPART_MODE_RELATED
MULTIPART_MODE_MIXED_RELATED
通过传递 false,您将设置 ** MULTIPART_MODE_NO**,这不允许您插入附件。
通过传递 true,您将设置 ** MULTIPART_MODE_MIXED_RELATED**,这在文档中是这样描述的:
This is arguably the most correct MIME structure, according to the MIME spec: It is known to work properly on Outlook, Outlook Express, Yahoo Mail, and Lotus Notes. Does not work properly on Mac Mail. If you target Mac Mail or experience issues with specific mails on Outlook, consider using MULTIPART_MODE_RELATED instead.
一般来说,您可以使用此替代构造函数选择最适合您的构造函数:
public MimeMessageHelper(MimeMessage mimeMessage,
int multipartMode,
String encoding)
throws MessagingException
不同之处在于要求整数约束(以上之一)而不是布尔值。
我正在尝试发送带有附件的电子邮件:
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, false, CharEncoding.UTF_8);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
message.addAttachment("facture.pdf", new ByteArrayResource(IOUtils.toByteArray(is)));
我收到一个错误:
java.lang.IllegalStateException: Not in multipart mode - create an appropriate MimeMessageHelper via a constructor that takes a 'multipart' flag if you need to set alternative texts or add inline elements or attachments.
有没有办法让它继续使用 addAttachment 方法?
从 MimeMessageHelper 的文档看来,您只需传递一个 true
标志。
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, CharEncoding.UTF_8);
您必须将多部分模式指定为构造函数的第二个参数。 multipart模式有多种选择:
MULTIPART_MODE_NO
MULTIPART_MODE_MIXED
MULTIPART_MODE_RELATED
MULTIPART_MODE_MIXED_RELATED
通过传递 false,您将设置 ** MULTIPART_MODE_NO**,这不允许您插入附件。
通过传递 true,您将设置 ** MULTIPART_MODE_MIXED_RELATED**,这在文档中是这样描述的:
This is arguably the most correct MIME structure, according to the MIME spec: It is known to work properly on Outlook, Outlook Express, Yahoo Mail, and Lotus Notes. Does not work properly on Mac Mail. If you target Mac Mail or experience issues with specific mails on Outlook, consider using MULTIPART_MODE_RELATED instead.
一般来说,您可以使用此替代构造函数选择最适合您的构造函数:
public MimeMessageHelper(MimeMessage mimeMessage,
int multipartMode,
String encoding)
throws MessagingException
不同之处在于要求整数约束(以上之一)而不是布尔值。