使用 byte[] 和 Java-Mail 发送电子邮件附件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1804901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send Email Attachement using byte[] with Java-Mail
提问by Nuno Furtado
I have a byte array wich i wish to add as an attachement to an email i am sending.
我有一个字节数组,我希望将其作为附件添加到我发送的电子邮件中。
Unfortunally i can't find how to attach it as a byte array, the solution i have uses disk files (which i dont want since i dont want to write the byte array just so i can attach it).
不幸的是,我找不到如何将它附加为字节数组,我的解决方案使用磁盘文件(我不想要,因为我不想编写字节数组只是为了附加它)。
I've found one solution that involves creating an object that extends DataSource and use this as a wrapper for the byte array and then feed that to the MimeBodyPart.
我找到了一个解决方案,该解决方案涉及创建一个扩展 DataSource 的对象并将其用作字节数组的包装器,然后将其提供给 MimeBodyPart。
Anyone know of a better solution?
有人知道更好的解决方案吗?
回答by erickson
Creating a DataSourceis the right approach. You don't have to write your own, though. Just use the ByteArrayDataSourcefrom JavaMail.
创建一个DataSource是正确的方法。不过,您不必自己编写。只需使用ByteArrayDataSource来自 JavaMail 的。
回答by Ashok Patel
Here is the code for your requirement...store attachment file as BLOB in DB and fetch that for sending it as a attachment in mail...............
这是您的要求的代码...将附件文件作为 BLOB 存储在 DB 中并获取它以将其作为附件发送到邮件中......................
import java.io.*;
import java.util.*;
import javax.activation.*;
public class BufferedDataSource implements DataSource {
private byte[] _data;
private java.lang.String _name;
public BufferedDataSource(byte[] data, String name) {
_data = data;
_name = name;
}
public String getContentType() { return "application/octet-stream";}
public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(_data);}
public String getName() { return _name;}
/**
* Returns an OutputStream from the DataSource
* @returns OutputStream Array of bytes converted into an OutputStream
*/
public OutputStream getOutputStream() throws IOException {
OutputStream out = new ByteArrayOutputStream();
out.write(_data);
return out;
}
}
===========================================================
//Getting ByteArray From BLOB
byte[] bytearray;
BLOB blob = ((OracleResultSet) rs).getBLOB("IMAGE_GIF");
if (blob != null) {
BufferedInputStream bis = new BufferedInputStream(blob.getBinaryStream());
ByteArrayOutputStream bao = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int length = 0;
while ((length = bis.read(buffer)) != -1) {
bao.write(buffer, 0, length);
}
bao.close();
bis.close();
bytearray = bao.toByteArray();
}
===============================================================
//Attach File for mail
MimeBodyPart att = new MimeBodyPart();
BufferedDataSource bds = new BufferedDataSource(bytearray, "AttName");
att.setDataHandler(new DataHandler(bds));
att.setFileName(bds.getName());

