package com.puckasoft.video327.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
public class EmailUtil {
private static Properties properties;//配置信息
private static String hostName;//郵件服務器
private static String userName;//登錄郵件服務器的用戶名
private static String password;//登錄郵件服務器的密碼
private static String from;//郵件發送者
static {
properties=new Properties();
InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("email.properties");
try {
properties.load(in);
hostName = properties.getProperty("hostName");
userName = properties.getProperty("userName");
password = properties.getProperty("password");
from = properties.getProperty("from");
// System.out.println(hostName);
// System.out.println(userName);
// System.out.println(password);
// System.out.println(from);
} catch (IOException e) {
throw new ExceptionInInitializerError();
}
}
private static EmailUtil emailManager = new EmailUtil();
private EmailUtil() {
}
public static EmailUtil getInstance() {
return emailManager;
}
/**
* @author: new
* @data: Aug 27, 2009
* @param to 接收人的email
* @param subject 發送的郵件的主題
* @param msg 發送郵件的內容
* @return
* @throws EmailException
* @function:
*/
public boolean sendHtmlEmail(String to, String subject, String msg)
throws EmailException {
boolean flag = true;
HtmlEmail email = new HtmlEmail();
// 設置郵件字符集編碼
email.setCharset("utf-8");
// 設置郵件服務器
email.setHostName(hostName);
email.setAuthentication(userName, password);
// 設置郵件主題
email.setSubject(subject);
// 設置郵件消息
email.setHtmlMsg(msg);
// 設置郵件發送者
email.setFrom(from);
// 設置郵件接收者
email.addTo(to);
email.send();
return flag;
}
public boolean sendResetPasswordEmail(String to, Map<String, String> params) {
String subject = properties.getProperty("resetPasswordSubject");
String msg = properties.getProperty("resetPasswordMsg");
try {
subject = new String(subject.getBytes("iso-8859-1"), "utf-8");
msg = new String(msg.getBytes("iso-8859-1"), "utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
// 用map裏的值代替模板裏的對應參數
//("path","url"),("keyId","fahfao097fankfhaoi") ${path}
for (Map.Entry<String, String> paramsEntry : params.entrySet()) {
//${path}-->http://192.168.1.245:8080/video325/user/resetPwd.jsp
//${keyId}-->fafafag2r535365732gsdsgs
msg = msg.replace("${" + paramsEntry.getKey() + "}", paramsEntry
.getValue());
}
System.out.println(subject);
System.out.println(msg);
try {
sendHtmlEmail(to, subject, msg);
} catch (EmailException e) {
e.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args) {
Map<String, String> params = new HashMap<String, String>();
params.put("path", "http://192.168.1.245:8080/video325/resetPwd.jsp");
// System.out.println(UUID.randomUUID().toString());
params.put("keyId", UUID.randomUUID().toString().replace("-", ""));
EmailUtil.getInstance().sendResetPasswordEmail("[email protected]",
params);
}
}