使用现有的InputStream作为附件内容使用javax.mail发送电子邮件 [英] Send email with javax.mail using an existing InputStream as attachment content
问题描述
是否可以使用javax.mail
并使用现有" InputStream
作为电子邮件附件内容来发送电子邮件?
Is it possible to send an email using javax.mail
and using an "existing" InputStream
for the email message attachment content?
目前,我正在按照以下方式构建电子邮件:
Currently I am building the email message as follows:
final MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Subject line");
final Multipart multipartContent = new MimeMultipart();
final MimeBodyPart textPart = new MimeBodyPart();
textPart.setText("Message body");
multipartContent.addBodyPart(textPart);
final MimeBodyPart attachmentPart = new MimeBodyPart();
final DataSource source = new InputStreamDataSource("text/plain", "test.txt", new ByteArrayInputStream("CONTENT INPUT STREAM".getBytes()));
attachmentPart.setDataHandler(new DataHandler(source));
attachmentPart.setFileName("text.txt");
multipartContent.addBodyPart(attachmentPart);
message.setContent(multipartContent);
InputStreamDataSource
的实现方式如下:
public class InputStreamDataSource implements DataSource
{
private final String contentType;
private final String name;
private final InputStream inputStream;
public InputStreamDataSource(String contentType, String name, InputStream inputStream)
{
this.contentType = contentType;
this.name = name;
this.inputStream = inputStream;
}
public String getContentType()
{
return contentType;
}
public String getName()
{
return name;
}
public InputStream getInputStream() throws IOException
{
System.out.println("CALLED TWICE: InputStreamDataSource.getInputStream()");
return new BufferedInputStream(inputStream);
//return new ByteArrayInputStream("THIS 'NEW' INPUT STREAM WORKS BUT 'EXISTING' INPUT STREAM RESULTS IN ZERO-BYTE ATTACHMENT".getBytes());
}
public OutputStream getOutputStream() throws IOException
{
throw new UnsupportedOperationException("Not implemented");
}
}
DataSource
提供方法getInputStream()
来获取电子邮件附件内容的InputStream
.
The DataSource
provides method getInputStream()
to get the InputStream
for the email message attachment content.
如果我返回一个不依赖于现有" InputStream
的新" InputStream
,则它可以正常工作.但是,如果我返回现有" InputStream,则电子邮件将以零字节附件发送.
If I return a "new" InputStream
which does not depend on an "existing" InputStream
then it works fine. But if I return an "existing" InputStream then the email message is delivered with a zero-byte attachment.
是否可以使用javax.mail
发送电子邮件,并使用现有" InputStream
作为电子邮件附件内容?
Is it possible to send an email using javax.mail
, and use an "existing" InputStream
for the email message attachment content?
推荐答案
我重写了您的InputStreamDataSource类,它对我有用.
I rewrited your InputStreamDataSource class, and it works for me.
class InputStreamDataSource implements DataSource {
String contentType;
String name;
byte[] fileData;
public InputStreamDataSource(String contentType, String name, InputStream inputStream) throws IOException {
this.contentType = contentType;
this.name = name;
/**
* It seems DataSource will close inputStream and reopen it.
* I converted inputStream to a byte array, so it won't be closed again.
*/
fileData = IOUtils.toByteArray(inputStream);
}
public String getContentType() {
return contentType;
}
public String getName() {
return name;
}
public InputStream getInputStream() throws IOException {
/**
* Convert byte array back to inputStream.
*/
return new ByteArrayInputStream(fileData);
}
public OutputStream getOutputStream() throws IOException {
throw new UnsupportedOperationException("Not implemented");
}
}
这篇关于使用现有的InputStream作为附件内容使用javax.mail发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!