javamail实践

时间:2023-02-09 21:46:55
	public static void main(String[] args) throws Exception, Exception {
MimeMessage message=new MimeMessage(Session.getDefaultInstance(new Properties()));
message.setFrom(new InternetAddress("aa@aa.com"));
message.setRecipient(RecipientType.TO, new InternetAddress("bb@bb.com"));
message.setSubject("test+image+append");
//文本
MimeBodyPart text=new MimeBodyPart();
text.setContent("this id a<br/><img src='cid:imageID'/><br/>image<br/>","text/html;charset=ISO8859-1");
//图片 MimeBodyPart image=new MimeBodyPart();
DataHandler dh=new DataHandler(new FileDataSource("d:\\cc.jpg"));
image.setDataHandler(dh);
image.setContentID("imageID"); //设置图片和文本的关系
MimeMultipart mm1=new MimeMultipart();
mm1.addBodyPart(text);
mm1.addBodyPart(image); mm1.setSubType("related"); //附件
MimeBodyPart append=new MimeBodyPart();
dh=new DataHandler(new FileDataSource("d:\\qq.zip"));
append.setDataHandler(dh);
append.setFileName(dh.getName()); //设置(文本图片)和附件的关系 MimeBodyPart temp=new MimeBodyPart();
//将文本和图片的关系封装成一个邮件
temp.setContent(mm1);
MimeMultipart mm=new MimeMultipart();
mm.addBodyPart(temp);
mm.addBodyPart(append);
mm.setSubType("mixed"); //设置全邮件的关系 message.setContent(mm);
message.writeTo(new FileOutputStream("d:\\1.eml")); }