package com.coupon.util;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Properties;
import java.util.Random;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.coupon.entity.WebUser;
import com.coupon.util.email.MailSenderInfo;
import com.coupon.util.email.SmtpAuth;
public class EmailUtils {
// private static final String SERVER_HOST = "smtp.163.com";
private static final String SERVER_HOST = "mail.onhand.cn";
private static final String SERVER_PORT = "25";
private static Log log = LogFactory.getLog(EmailUtils.class);
private static String userName = "";
private static String pwd = "";
/** *//**
* 以文本格式发送邮件 smtp.onhand.cn 25
* @param mailInfo 待发送的邮件的信息
*/
public static boolean sendTextMail(MailSenderInfo mailInfo) {
// 判断是否需要身份认证
SmtpAuth authenticator = null;
Properties pro = mailInfo.getProperties();
pro.put("mail.smtp.auth", "true");
// if (mailInfo.isValidate()) {
// System.out.println("SmtpAuth:" + mailInfo.getUserName()+","+ mailInfo.getPassword());
// // 如果需要身份认证,则创建一个密码验证器
// authenticator = new SmtpAuth(mailInfo.getUserName(), mailInfo.getPassword());
// }
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro,new SmtpAuth(){
@Override
protected PasswordAuthentication getPasswordAuthentication() {
System.out.println("SmtpAuth.getPasswordAuthentication :"+userName+","+pwd);
return new PasswordAuthentication(userName, pwd);
}
});
try {
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO,to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
// 发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
/**
* 以HTML格式发送邮件
* @param mailInfo 待发送的邮件信息
*/
public static boolean sendHtmlMail(MailSenderInfo mailInfo){
// 判断是否需要身份认证
SmtpAuth authenticator = null;
Properties pro = mailInfo.getProperties();
pro.put("mail.smtp.auth", "true");
//如果需要身份认证,则创建一个密码验证器
if (mailInfo.isValidate()) {
System.out.println("SmtpAuth:" + mailInfo.getUserName()+","+ mailInfo.getPassword());
authenticator = new SmtpAuth(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
try {
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
// Message.RecipientType.TO属性表示接收者的类型为TO
mailMessage.setRecipient(Message.RecipientType.TO,to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
// 设置HTML内容
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
// 发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
public static boolean sendRegisterEmail(WebUser webUser){
Integer emailIndex = getRandomCode();
log.info("--EmailUtils.sendRegisterEmail entered... sendEmail is "+Utils.readValue("UID_"+emailIndex));
try {
MailSenderInfo mailInfo = new MailSenderInfo();
System.out.println("SendRegisterMail :" + webUser.getUsername());
String emailUrl = Utils.readValue("emailUrl");
mailInfo.setMailServerHost(SERVER_HOST);
mailInfo.setMailServerPort(SERVER_PORT);
mailInfo.setValidate(true);
// mailInfo.setUserName(UID[emailIndex]);
mailInfo.setUserName(Utils.readValue("UID_"+emailIndex));
// userName = UID[emailIndex];
userName = Utils.readValue("UID_"+emailIndex);//获取随机系统邮件
// pwd=PASSWORD;
pwd = Utils.readValue("PASSWORD");
// mailInfo.setPassword(PASSWORD);
mailInfo.setPassword(Utils.readValue("PASSWORD"));
// mailInfo.setFromAddress(FROMADDRESS[emailIndex]);
mailInfo.setFromAddress(Utils.readValue("FROMADDRESS_"+emailIndex));
// System.out.println(UID[emailIndex]+"," +PASSWORD+","+FROMADDRESS[emailIndex]);
System.out.println(Utils.readValue("UID_"+emailIndex)+"," +Utils.readValue("PASSWORD")+","+Utils.readValue("FROMADDRESS_"+emailIndex));
mailInfo.setToAddress(webUser.getUsername());
mailInfo.setSubject("标题");
mailInfo.setContent("内容");
if(sendTextMail(mailInfo)){
return true;
}
} catch (Exception e) {
log.info("EmailUtils.sendRegisterEmail Exception:" + e);
e.printStackTrace();
}
log.info("--EmailUtils.sendRegisterEmail finished...");
return false;
}
public static boolean sendLotteryEmail(WebUser webUser, String lotteryLogCode){
Integer emailIndex = getRandomCode();
log.info("--EmailUtils.sendLotteryEmail entered... sendEmail is "+Utils.readValue("UID_"+emailIndex));
try {
MailSenderInfo mailInfo = new MailSenderInfo();
System.out.println("sendLotteryEmail :" + webUser.getUsername());
mailInfo.setMailServerHost(SERVER_HOST);
mailInfo.setMailServerPort(SERVER_PORT);
mailInfo.setValidate(true);
// mailInfo.setUserName(UID[emailIndex]);
mailInfo.setUserName(Utils.readValue("UID_"+emailIndex));
// mailInfo.setPassword(PASSWORD);
mailInfo.setPassword(Utils.readValue("PASSWORD"));
// userName = UID[emailIndex];
userName = Utils.readValue("UID_"+emailIndex);
// pwd = PASSWORD;
pwd = Utils.readValue("PASSWORD");
// mailInfo.setFromAddress(FROMADDRESS[emailIndex]);
mailInfo.setFromAddress(Utils.readValue("FROMADDRESS_"+emailIndex));
// System.out.println(UID[emailIndex]+"," +PASSWORD+","+FROMADDRESS[emailIndex]);
System.out.println(Utils.readValue("UID_"+emailIndex)+"," +Utils.readValue("PASSWORD")+","+Utils.readValue("FROMADDRESS_"+emailIndex));
mailInfo.setToAddress(webUser.getUsername());
mailInfo.setSubject("标题);
// String userName = webUser.getUsername();
// Integer index = userName.indexOf("@");
// userName = userName.substring(0, index);
mailInfo.setContent("邮件内容" );
if(sendTextMail(mailInfo)){
return true;
}
} catch (Exception e) {
log.info("EmailUtils.sendLotteryEmail Exception:" + e);
e.printStackTrace();
}
log.info("--EmailUtils.sendLotteryEmail finished...");
return false;
}
//获取随即数
public static Integer getRandomCode() {
Random rd = new Random();
int nextInt = rd.nextInt(Integer.parseInt(Utils.readValue("UIDLENGTH")));
return nextInt;
}
}