import java.util.Date;
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class EmailUtil {
public static void sendEmail(Object object){
Properties props = new Properties();
//配置发邮件主机及SMTP
props.setProperty("mail.host", "mail.china-ops.com");
props.setProperty("mail.smtp.auth", "true");
Authenticator authenticator = new Authenticator(){
@Override
public PasswordAuthentication getPasswordAuthentication() {
//发件人邮箱用户名和密码
return new PasswordAuthentication("用户名","密码");
}
};
Session session = Session.getDefaultInstance(props, authenticator);
session.setDebug(true);
Message message = new MimeMessage(session);
try {
// 发件人
message.setFrom(new InternetAddress("发件人邮箱"));
//收件人
message.setRecipients(RecipientType.TO,InternetAddress.parse("接收者邮箱1,接收者邮箱2"));
//标题
message.setSubject(ticket.getTicketType());
//内容
String content = "<html><body>"
+ "<h3>您好:</h3>"
+ "<p style='text-indent:2em'></p>"
+ "<p>简要说明:</p>"
+ "<br/><br/><br/>"
+ "<p style='text-align:right;padding-right:200px;padding-bottom:100px;'>"+ dateToString(new Date(), "yyyy-MM-dd HH:mm:ss") + "</p>"
+ "</body></html>";
message.setContent(content,"text/html;charset=UTF-8");
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public Date stringToDate(String strTime, String formatType)
throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat(formatType);
Date date = null;
date = formatter.parse(strTime);
return date;
}
}