Java Mail

package com.hsp.service;
//該程序是javamail,用於發送郵件的
import java.util.*;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMail {

	private MimeMessage mimeMsg;      //MIME郵件對象
	
	private Session session;          //郵件會話對象
	private Properties props;         //系統屬性
	private boolean needAuth=false;   //smtp是否須要認證
	
	private String username="";       //smtp認證用戶名和密碼
	private String password="";
	
	private Multipart mp;             //Multipart對象,郵件內容、標題、附件等內容
	
	public SendMail(){
		//setSmtpHost(getConfig.mailHost); //若是沒有指定有加你服務
		createMimeMessage();
	}
	
	public SendMail(String smtp){
		setSmtpHost(smtp);
		createMimeMessage(); 
	}
	
	public void setSmtpHost(String hostName){
		System.out.println("設置系統屬性:mail.stmp.host="+hostName);
		if(props==null) props=System.getProperties();                 //獲取系統屬性
		props.put("mail.smtp.host",hostName);                         //設置SMTP主機
	}
	
	public boolean createMimeMessage(){
		try{
			System.out.println("準備獲取郵件會話對象!");
			session=Session.getDefaultInstance(props,null);
		}catch(Exception e){
			System.err.println("獲取郵件會話對象時發生錯誤!"+e);
			return false;
		}
		System.out.println("準備建立MIME郵件對象");
		try{
			mimeMsg =new MimeMessage(session);//建立MIME郵件對象
			mp=new MimeMultipart();
			
			return true;
		}catch(Exception e){
			System.err.println("建立MIME郵件對象失敗"+e);
			return false;
		}
	}
	
	public void setNeedAnth(boolean need){
		System.out.println("設置smtp身份認證:mail.smtp.auth="+need);
		if(props==null) props=System.getProperties();
		
		if(need){
			props.put("mail.smtp.auth", "true");
		}else{
			props.put("mail.smtp.auth", false);
		}
	}
	
	public void setNamePass(String name,String pass){
		username=name;
		password=pass;
	}
	
	public boolean setSubject(String mailSubject){
		System.out.print("設置郵件主題");
		try{
			mimeMsg.setSubject(mailSubject);
			return true;
		}catch(Exception e){
			System.err.println("設置郵件主題發生錯誤");
			return false;
		}
	}
	
	public boolean setBody(String mailBody){
		try{
			BodyPart bp=new MimeBodyPart();
			bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312/>"+mailBody,"text/html;charset=gb2312");
			mp.addBodyPart(bp);
			
			return true;
		}catch(Exception e){
			System.err.println("設置郵件正文是發生錯誤!"+e);
			return false;
		}
	}
	
	public boolean addFileAffix(String filename){
		System.out.println("增長郵件附件:"+filename);
		try{
			
			BodyPart bp=new MimeBodyPart();
			FileDataSource fileds=new FileDataSource(filename);
			bp.setDataHandler(new DataHandler(fileds));
			bp.setFileName(fileds.getName());
			mp.addBodyPart(bp);
			return true;
		}catch(Exception e){
			System.err.println("增長郵件附件"+filename+"發生錯誤:"+e);
			return false;
		}
	}
	
	public boolean setForm(String from){
		System.out.println("設置發信人");
		try{
			mimeMsg.setFrom(new InternetAddress(from));
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	public boolean setTo(String to){
		if(to==null) return false;
		try{
			mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	public boolean setCopyTp(String copyto){
		if(copyto==null) return false;
		try{
			mimeMsg.setRecipients(Message.RecipientType.CC, (Address[])InternetAddress.parse(copyto));
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	public boolean sendout(){
		try{
			mimeMsg.setContent(mp);
			//mimeMsg.saveChanges();
			
			System.out.println("正在發送郵件....");
			
			Session mailSession=Session.getInstance(props,null);
			Transport transport=mailSession.getTransport("smtp");
			transport.connect((String)props.get("mail.smtp.host"),username,password);
			transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
			
			System.out.println("發送郵件成功!");
			transport.close();
			
			return true;
		}catch(Exception e){
			System.out.println("郵件發送失敗!"+e);
			return false;
		}
	}
	
	public static void sendToSomebody(String mailbody,String title,String toEmail){
		//發送的郵件內容
		//String mailbody=mailbody;
			
		//指明讓那個smtp轉發
		SendMail themail=new SendMail("smtp.163.com");
		
		//校驗身份
		themail.setNeedAnth(true);
		
		//郵件標題
		if(themail.setSubject(title)==false) return;
		//將要發送的內容放入郵件體
		if(themail.setBody(mailbody)==false) return;
		
		//發往哪裏
		if(themail.setTo(toEmail)==false) return;
		//誰發送的
		if(themail.setForm("mingliang88820@163.com")==false) return;
		
		//發送附件
		//if(themail.addFileAffix("xxx.xx")==false) return;
		//將在163網站上的郵件用戶名和密碼放入郵件體
		themail.setNamePass("mingliang88820", "15938968102");
		//發送
		if(themail.sendout()==false) return;
	}
}
相關文章
相關標籤/搜索