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);
}
}