您的位置:首页 > 编程语言 > Java开发

关于java mail发送图片为二进…

2015-11-12 15:54 435 查看
      
初次接触java
mail只能发发纯文本内容的邮件,由于项目需要,需要发送图片作为附件。项目需要将相机拍的照片,不保存到相册中,那么就只能利用拍照时onPictureTaken(byte[]
data, Camera camera)这个方法中保存照片的byte[] 数组来想解决办法。但是java mail中的

MimeBodyPart mdp = new MimeBodyPart(); //新建一个存放附件的BodyPart

DataHandler dh = new DataHandler(...);

mdp.setDataHandler(dh); //给BodyPart对象设置内容为dh

其中当DataHandler初始化时需要DataSource的子类

 

很遗憾的是DataSource是个Interface,DataSource仅MimePartDataSource,URLDataSource,FileDataSource三个子类,而在这三个类中没有一个能直接用二进制流(byte[])创建实例。那没办法了,只能重写接口里的方法,来实现这个功能了。
 

我们就创建一个类ByteArrayDataSource并且实现DataSource的接口,很显然,在这里要定义两个变量即byte[] 
data(表示数据流)和 String type( 表示Content-Type),写该类的构造函数:
public ByteArrayDataSource(InputStream is, String
type) {

   this.type =
type;

   try {

    ByteArrayOutputStream
os = new ByteArrayOutputStream();

    int
ch;

    while
((ch = is.read()) != -1)

     os.write(ch);

    data
= os.toByteArray();

   } catch
(IOException ioe) {

   }

  }
public ByteArrayDataSource(byte[] data,String
type)

{

data = data;

type = type;

}

public ByteArrayDataSource(String data,String type)

{

try

{

// Assumption that the string contains only ascii

// characters ! Else just pass in a charset into this

// constructor and use it in getBytes()

data = data.getBytes("iso-8859-1");

}

catch (UnsupportedEncodingException uee)

{

}

type = type;

}

这三个构造方法,能分别实现将inputstream ,byte[]和String 转化成byte[],非常方便。
重写接口里的方法:
@Override

  public String getContentType()
{

   // TODO
Auto-generated method stub

   return  
type;

  }
  @Override

  public InputStream
getInputStream() throws IOException {

   if (data ==
null)

    return
null;

     
return new ByteArrayInputStream(data);
  }
  @Override

  public String getName() {

      return
null;

  }
  @Override

  public OutputStream
getOutputStream() throws IOException {

   return
null;

  }
 
以上就是将二进制流作为附件的方法,接下来说说,发送邮件的基本方法:
final String SMPT_HOSTNAME = "smtp.qq.com"; //
邮件发送服务器

  final String USERNAME =
"xxxxxxxx@qq.com"; //
发件服务器的验证账户和密码

  final String PASSWORD =
"xxxxxxxxxxxxxxxx";

  String to = "xxxxxxxxxxxxxxxx@qq.com"; //
收件人

  String from = "xxxxxxxxxxxxxx@qq.com"; //
发件人
  //
设置邮件发送服务器、协议和是否需要验证

  Properties props =
System.getProperties();

  props.setProperty("mail.smtp.host",
SMPT_HOSTNAME);

  props.setProperty("mail.transport.protocol",
"smtp");

  props.setProperty("mail.smtp.auth",
"true");
  //
创建一个包含用户验证的邮件发送Session

  Session session =
Session.getInstance(props, new Authenticator() {

   @Override

   protected
PasswordAuthentication getPasswordAuthentication() {

    return
new PasswordAuthentication(USERNAME, PASSWORD);

   }

  });
//最后发送的是message,所以最后正文,附件都要通过setContent()放到message

   MimeMessage
message = new MimeMessage(session);

   //
设定发件人、收件人、主题、正文

   message.setFrom(new
InternetAddress(from));

   message.addRecipient(Message.RecipientType.TO,
new InternetAddress(

     to));

   message.setSubject("From
PhoneSecurity");
//一封邮件中只能有一个 MimeMultipart
,但是可以有多个 MimeBodyPart
,前者通过addBodyPart方法
//将后者放进来

   MimeMultipart
multipart = new MimeMultipart();

   // 创建附件

   MimeBodyPart
attache = new MimeBodyPart();

   attache.setDataHandler(new
DataHandler(new ByteArrayDataSource(is,

     "application/x-jpg")));

   //
获取附件名称

   String
fileName = String.valueOf(System.currentTimeMillis())

     +
".jpg";

attache.setFileName(MimeUtility.encodeText(fileName, "GBK",

      null));
// 添加附件

   multipart.setSubType("related");

   multipart.addBodyPart(attache);
 
//正文部分
MimeBodyPart bodyPart = new MimeBodyPart();

   bodyPart.setContent(”xxxxxxxxxx“,

     "text/html;charset=GBK");

   multipart.addBodyPart(bodyPart);
   message.setContent(multipart);
最后调用静态方法发送邮件:
// 正式发送邮件

   Transport.send(message);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: