JavaMail邮件发送(一)

2014-11-24 08:22:29 · 作者: · 浏览: 0
001
/*
002
* 版权所有(C) 上海沃尚信息技术有限公司2011-2020
003
* Copyright 2009-2020 VOKE TECHNOLOGY CO.,LTD.
004
*
005
* This software is the confidential and proprietary information of
006
* Voke Corporation ("Confidential Information"). You
007
* shall not disclose such Confidential Information and shall use
008
* it only in accordance with the terms of the license agreement
009
* you entered into with Voke.
010
*/
011
package com.wokejia.space.core.util;
012

013
import java.io.UnsupportedEncodingException;
014
import java.util.ArrayList;
015
import java.util.Date;
016
import java.util.List;
017
import java.util.Properties;
018

019
import javax.mail.Authenticator;
020
import javax.mail.Message;
021
import javax.mail.MessagingException;
022
import javax.mail.PasswordAuthentication;
023
import javax.mail.Session;
024
import javax.mail.Transport;
025
import javax.mail.internet.InternetAddress;
026
import javax.mail.internet.MimeBodyPart;
027
import javax.mail.internet.MimeMessage;
028
import javax.mail.internet.MimeMultipart;
029

030
/**
031
*

邮件工具


032
*
033
* @ClassName: EmailUtil
034
* @version V1.0 @date 2012-5-22 下午03:57:16
035
* http://my.oschina.net/arthor" class="referer" target="_blank">@author mailto:luoweijun@wokejia.com ">罗伟俊
036
*
037
*/
038
public class EmailUtil {
039
private static final int PORT = 25;
040
//发送者信息
041
private static final String SERVER = "smtp.ym.163.com";//邮件服务器mail.cpip.net.cn
042
private static final String FROM = "name";//发送者,显示的发件人名字
043
private static final String USER = " aaaaaa@qq.com";//发送者邮箱地址
044
private static final String PASSWORD = "xxxx";//密码
045
private static final String ENCODING = "GBK";//邮件编码
046

047

048
private String title;//邮件标题
049
private String html;//邮件html内容
050
private List emailEvents = new ArrayList();//邮件数据
051
private static EmailUtil emailUtil = new EmailUtil();
052
private Session session;
053

054
public EmailUtil() {
055
Properties props = new Properties();
056
props.put("mail.smtp.host", SERVER);//SMTP服务器地址
057
props.put("mail.smtp.port", String.valueOf(PORT));//端口
058
props.put("mail.smtp.auth", "true");//SMTP服务器是否需要用户认证,默认为false
059
props.put("mail.stmp.timeout", "2000");
060
session = Session.getDefaultInstance(props, new Authenticator() {
061
// 验账账户
062
public PasswordAuthentication getPasswordAuthentication() {
063
return new PasswordAuthentication(USER,PASSWORD);
064
}
065
});
066
}
067

068
public static EmailUtil getInstance() {
069
return emailUtil;
070
}
071
public EmailUtil setTitle(String title){
072
this.title = title;
073
return this;
074
}
075
public EmailUtil setHTML(String html){
076
this.html = html;
077
return this;
078
}
079

080
private Email getEmail(){
081
return new Email();
082
}
083

084
public EmailUtil addEmailGiveUser(String addresseeEmail, String addresseeName) {
085
EmailEvent emailEvent = new EmailEvent();
086
emailEvent.setAddresseeEmail(addresseeEmail);
087
emailEvent.setAddresseeName(addresseeName);
088
emailEvents.add(