java mail 发送HTML 附件,Google 邮箱Gmail作为服务端

package com.luke.mail;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;

public class MailHandler {
	private String username = "[email protected]";
	private String password = "";
	private String displayName = "PITA";
	private String host = "imap.gmail.com";
	private String port = "587";
	private Properties props;
	
	public MailHandler(String username, String password, String displayName, String host, String port) {
		this.username = username;
		this.password = password;
		this.displayName = displayName;
		this.host = host;
		this.port = port;
		initProperties();
	}

	public MailHandler(){
		initProperties();
	}
	
	private void initProperties(){
		props = System.getProperties();
		props.put("mail.smtp.starttls.enable", "true"); 
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.user", username);
		props.put("mail.smtp.password", password);
		props.put("mail.smtp.port", port);
		props.put("mail.smtp.auth", "true");
	}
	public void sendEmail(String[]receivers, String title, String htmlContent, File[]attchments) {
		Transport transport = null;
		try {
			Session session = Session.getDefaultInstance(props, null);
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(username, displayName));
	
			int len = receivers.length;
			InternetAddress[]address = new InternetAddress[len];
			for(int i=0;i<len;i++){
				address[i] = new InternetAddress(receivers[i]);
			}
			message.setRecipients(Message.RecipientType.TO,address);
	
		    message.setSubject(title);
	//	    message.setText("Hello \n welcome!");
//		    message.setDataHandler(new DataHandler(new ByteArrayDataSource(htmlContent, "text/html")));
		    Multipart multipart = new MimeMultipart();
		    MimeBodyPart htmlBodyPart = new MimeBodyPart();
		    htmlBodyPart.setContent(htmlContent, "text/html;charset=UTF-8");
		    multipart.addBodyPart(htmlBodyPart);
		    if(attchments!=null&&attchments.length>0){
		    	
		    	for(int i=0;i<attchments.length;i++){
		    		 FileDataSource dataSource = new FileDataSource(attchments[i]);
		    		 MimeBodyPart bodyPart = new MimeBodyPart();
		    		 bodyPart.setDataHandler(new DataHandler(dataSource));
		    		 bodyPart.setFileName(attchments[i].getName());
		    		 multipart.addBodyPart(bodyPart);
		    	}
		    	
		    }
		    message.setContent(multipart);
			transport = session.getTransport("smtp");
			transport.connect(host, username, password);
			transport.sendMessage(message, message.getAllRecipients());
			
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try {
				transport.close();
			} catch (MessagingException e) {
				e.printStackTrace();
			}
		}
	}
//	public void sendEmail(String receiver, String title, String htmlContent) throws AddressException, MessagingException, IOException{
//
//		Session session = Session.getDefaultInstance(props, null);
//		MimeMessage message = new MimeMessage(session);
//		message.setFrom(new InternetAddress(username));
//
//	    message.addRecipient(Message.RecipientType.TO, new InternetAddress(receiver));
//
//	    message.setSubject(title);
////	    message.setText("Hello \n welcome!");
//	    message.setDataHandler(new DataHandler(     
//                new ByteArrayDataSource(htmlContent, "text/html")));
//	    
//		Transport transport = session.getTransport("smtp");
//		transport.connect(host, username, password);
//		transport.sendMessage(message, message.getAllRecipients());
//		transport.close();
//	}
	/**
	 * @param args
	 * @throws MessagingException 
	 * @throws AddressException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws AddressException, MessagingException, IOException {
		String receiver = "[email protected]";
		String[]receivers = {receiver};
		String content = "Hello "+receiver+"<p> How are you!</p><p>Just for test!</p><p>thanks</p><p>Luke</p>";
		String title = "PITA AUTOMATION EMAIL";
		MailHandler handler = new MailHandler();
//		handler.sendEmail(receiver, title, content);
		File[]files = {new File("D:/pita/1029/output/DFLink_Summary_d3dxsapi_3.0.21.0_D3One.2012-10-15.sandbox.zip_20130131193710.html"),
				new File("D:/pita/1029/output/DFLink_Report_20130131193710.html")};
		handler.sendEmail(receivers, title, content, files);
		
	}

}

 

你可能感兴趣的:(java mail 发送HTML 附件,Google 邮箱Gmail作为服务端)