JavaMail 邮件发送

http://www.oracle.com/technetwork/java/javamail/index-138643.html
/*
 * 版权所有(C) 上海沃尚信息技术有限公司2011-2020
 * Copyright 2009-2020 VOKE TECHNOLOGY CO.,LTD.
 *  
 * This software is the confidential and proprietary information of
 * Voke Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Voke.
 */
package com.wokejia.space.core.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
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;

/**
 * <p class="detail">邮件工具</p>
 *
 * @ClassName: EmailUtil 
 * @version V1.0  @date 2012-5-22 下午03:57:16 
 * @author <a href="mailto:[email protected] ">罗伟俊</a>                             
 *      
 */
public class EmailUtil {
	private static final int PORT = 25;
	//发送者信息
	private static final String SERVER = "smtp.ym.163.com";//邮件服务器mail.cpip.net.cn
	private static final String FROM = "name";//发送者,显示的发件人名字
	private static final String USER = "[email protected]";//发送者邮箱地址
	private static final String PASSWORD = "xxxx";//密码
	private static final String ENCODING = "GBK";//邮件编码
   
	
	private String title;//邮件标题
	private String html;//邮件html内容
	private List<EmailEvent> emailEvents = new ArrayList<EmailEvent>();//邮件数据
	private static EmailUtil emailUtil = new EmailUtil();
	private Session session;
	
	public EmailUtil() {
		 Properties props = new Properties();
         props.put("mail.smtp.host", SERVER);//SMTP服务器地址
         props.put("mail.smtp.port", String.valueOf(PORT));//端口
         props.put("mail.smtp.auth", "true");//SMTP服务器是否需要用户认证,默认为false
         props.put("mail.stmp.timeout", "2000");
         session = Session.getDefaultInstance(props, new Authenticator() { 
         	 // 验账账户
             public PasswordAuthentication getPasswordAuthentication() {
                 return new PasswordAuthentication(USER,PASSWORD);
             }
         });
	}
	
	public static EmailUtil getInstance() {
		return emailUtil;
	}
	public EmailUtil setTitle(String title){
		this.title = title;
		return this;
	}
	public EmailUtil setHTML(String html){
		this.html = html;
		return this;
	}
	
	private Email getEmail(){
		return new Email();
	}
	
	public EmailUtil addEmailGiveUser(String addresseeEmail, String addresseeName) {
		EmailEvent emailEvent = new EmailEvent();
		emailEvent.setAddresseeEmail(addresseeEmail);
		emailEvent.setAddresseeName(addresseeName);
		emailEvents.add(emailEvent);
		return this;
	}

	public void send() {
		Email email = emailUtil.getEmail();
		try {
			email.sendEmail();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		clear();
	}
	
	
	private void clear(){
		emailEvents = new ArrayList<EmailEvent>();
		html = null;
		title = null;
	}
	
    public class Email{
    	public void sendEmail() throws UnsupportedEncodingException, MessagingException{
            MimeMessage msg = new MimeMessage(session);
            msg.setSentDate(new Date());
	        msg.setFrom(new InternetAddress(USER,FROM,ENCODING));
            
            //这里可以添加多个目的用户
            for (EmailEvent e : emailEvents) {
                msg.addRecipient(Message.RecipientType.TO,new InternetAddress(e.addresseeEmail,e.addresseeName,ENCODING));
			}
            
            msg.setSubject(title, ENCODING);   
            
            //设置邮件内容格式为混合内容  
            MimeMultipart msgMultipart = new MimeMultipart("mixed");  
            MimeBodyPart content = new MimeBodyPart();  
            //设置html内容  
            content.setContent(html,"text/html;charset=gbk");
            msgMultipart.addBodyPart(content); 
            msg.setContent(msgMultipart);
            Transport.send(msg);
    	}
    }
    
    public class EmailEvent{
    	private String addresseeName;//收件人姓名
    	private String addresseeEmail;//收件人邮箱
		public String getAddresseeName() {
			return addresseeName;
		}
		public void setAddresseeName(String addresseeName) {
			this.addresseeName = addresseeName;
		}
		public String getAddresseeEmail() {
			return addresseeEmail;
		}
		public void setAddresseeEmail(String addresseeEmail) {
			this.addresseeEmail = addresseeEmail;
		}
    }
    
    public static void main(String args[]) throws UnsupportedEncodingException{
    	String ss = "<STYLE type=\"text/css\">BODY { font-size: 14px; line-height: 1.5  } </STYLE><html><head><meta http-equiv=Content-Type content=\"text/html; charset=gbk\"/></head><body><h1>这个是一个测试邮件:<a href=\"http://www.baidu.com\">百度的连接</h1></body></html>";
    	String ss2 = "<STYLE type=\"text/css\">BODY { font-size: 14px; line-height: 1.5  } </STYLE><html><head><meta http-equiv=Content-Type content=\"text/html; charset=gbk\"/></head><body><h1>这是第二封邮件:<a href=\"http://www.baidu.com\">百度的连接</h1></body></html>";
    	
    	//收件人
        EmailUtil.getInstance()
        .setTitle("这是第一sss封邮件")
        .setHTML(ss)
        .addEmailGiveUser("[email protected]","徐鑫")
        .addEmailGiveUser("[email protected]","罗伟俊")
        .send();
        
        
        EmailUtil.getInstance()
        .setTitle("这是第二sss封邮件")
        .setHTML(ss2)
        .addEmailGiveUser("[email protected]","徐鑫")
        .addEmailGiveUser("[email protected]","罗伟俊")
        .send();
        
        System.out.println("ok");
    }
}

下载:javamail1_4_5.zip

你可能感兴趣的:(邮件,邮件发送)