java邮件开发--心得5-java编写复杂的电子邮件

     1. 前面介绍了如何编写一个简单的电子邮件,这里再介绍一个编写复杂的电子邮件的案例,该电子邮件包括附件和html文本内容两部分;邮件主要内容和功能如下,下面请看具体案例和注释分析:

java邮件开发--心得5-java编写复杂的电子邮件_第1张图片

package com.csdn.itcast;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class Demo3 {

	/**
	 * @param args
	 * 复杂邮件的编写
	 */
	public static void main(String[] args) throws Exception{
		
		Message msg = new MimeMessage(Session.getInstance(new Properties()));
		
		/*这里用到了sun公司提供的编码工具类MimeUtility的encondeText()方法,来进行中文的编码;"\""是双引号的转义字符
		 * 邮件里的中文处理编码是以 =?  开头  ?= 结尾的base64编码的,中间要用两个问号隔开编码类型和中文编过码的内容;
		 *比如: =?UTF-8?B?tKvWx7Klv80=?=  ;编译过来是”传智播客“的意思。
		 */
		msg.setFrom(new InternetAddress("\"" + MimeUtility.encodeText("杨凯专属频道") + "\" <[email protected]>"));
		msg.setSubject("杨凯专属频道报道:黑马老师都很给力!");		
		msg.setReplyTo(new Address[]{new InternetAddress("[email protected]")});
		msg.setRecipients(RecipientType.TO,InternetAddress.parse(MimeUtility.encodeText("黎活明") + " <[email protected]>," + MimeUtility.encodeText("张孝祥") + " <[email protected]>"));
	
		//最外面的即第一层框架,由附件和内容组成
		//这里传入的值是subtype即子类型mixed,不能是multipart/mixed;因为你new出的就是MimeMultipart
		MimeMultipart msgMultipart = new MimeMultipart("mixed");
		msg.setContent(msgMultipart);
		
		//attach1和attach2代表附件1和2,content代表内容
		MimeBodyPart attach1 = new MimeBodyPart();
		MimeBodyPart attach2 = new MimeBodyPart();
		MimeBodyPart content = new MimeBodyPart();
		
		msgMultipart.addBodyPart(attach1);
		msgMultipart.addBodyPart(attach2);
		msgMultipart.addBodyPart(content);
		
		//设置附件的值
		attach1.setDataHandler(new DataHandler(new FileDataSource("resource\\Java培训.txt")));
		attach1.setFileName(MimeUtility.encodeText("java培训.txt"));
		attach2.setDataHandler(new DataHandler(new FileDataSource("resource\\people.gif")));
		attach2.setFileName("people.gif");
		
		
		//设置内容的值,第二层框架,有html文本和gif图片组成
		MimeMultipart bodyMultipart = new MimeMultipart("related");
		//这里一定要注意,是设置内容content的内容而不是msg卫星的内容,否则会把上面设置的覆盖掉
		content.setContent(bodyMultipart);
		
		MimeBodyPart htmlPart = new MimeBodyPart();
		MimeBodyPart gifPart = new MimeBodyPart();
		
		bodyMultipart.addBodyPart(htmlPart);
		bodyMultipart.addBodyPart(gifPart);
		
		//设置图片的值
		gifPart.setDataHandler(new DataHandler(new FileDataSource("resource\\welcome.gif")));
		gifPart.setHeader("Content-Location", "http://www.itcast.cn/logo.gif");
		//设置html文本的值
		htmlPart.setContent("欢迎来到杨凯专属频道,这是我自己用程序生成和发送的邮件哦!<img src='resource\\welcome.gif'>"
				,"text/html;charset=UTF-8");
		
		msg.saveChanges();
		
		//此处不使用方法链的原因是:如果发送的邮件特别多,用一个for循环实现,那么如果不关闭资源的话会造成内存泄露
		OutputStream ops = new FileOutputStream("resource\\yk.eml");
		msg.writeTo(ops);
		ops.close();
		
	}

}

2. base64编码转换工具

package cn.itcast.javamail2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import sun.misc.BASE64Encoder;

public class Base64Util {

	/**
	 * @param args add by zxx ,Dec 30, 2008
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
	
		BASE64Encoder encoder = new BASE64Encoder();
		System.out.println("please input user name:");
		String username = new BufferedReader(
					new InputStreamReader(System.in))
					.readLine();
		System.out.println(encoder.encode(username.getBytes()));
		System.out.println("please input password:");
		String password = new BufferedReader(
				new InputStreamReader(System.in))
				.readLine();		
		System.out.println(encoder.encode(password.getBytes()));
	}
	

}


 

你可能感兴趣的:(java邮件开发--心得5-java编写复杂的电子邮件)