packagecom.iot.common.email;importjava.io.File;importjavax.activation.DataHandler;importjavax.activation.DataSource;importjavax.activation.FileDataSource;importjavax.mail.Multipart;importjavax.mail.internet.MimeMultipart;importjavax.mail.internet.MimeBodyPart;importjavax.mail.BodyPart;importjavax.mail.PasswordAuthentication;importjavax.mail.Authenticator;importjava.util.ArrayList;importjavax.mail.MessagingException;importjavax.mail.Transport;importjavax.mail.Message;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeMessage;importjavax.mail.Session;importjava.util.Properties;importjava.util.List;/*** 邮件发送类,用来发送邮件给用户.
*
*@authorSam
**/
public abstract class EmailSender extendsAuthenticator {privateString host;privateString port;privateString username;privateString password;privateString from;publicEmailSender() {this.host =PropertiesUtil.getProperty(Constant.EMAIL_HOST);this.port =PropertiesUtil.getProperty(Constant.EMAIL_HOST_PORT);this.username =PropertiesUtil.getProperty(Constant.EMAIL_USER_NAME);this.password =PropertiesUtil.getProperty(Constant.EMAIL_PASSWORD);this.from =PropertiesUtil.getProperty(Constant.EMAIL_SENDER);
}/*** EmailSender构造函数,需要用户提供host,port,username,password,from等信息。
*
*@paramhost
* ,smtp服务器地址
*@paramport
* ,smtp服务器端口
*@paramusername
* ,邮箱用户名
*@parampassword
* ,邮箱密码
*@paramfrom
* ,邮箱发送人*/
publicEmailSender(String host, String port, String username,
String password, String from) {this.host =host;this.port =port;this.username =username;this.password =password;this.from =from;
}/*** 发送邮件到指定用户
*
*@paramto
* , 邮件发送对象
*@returnture 发送成功, false发送失败*/
publicBoolean send(String to) {
List toList = new ArrayList<>();
toList.add(to);returnsend(toList);
}/*** 群发邮件
*
*@paramtoList
* ,需要接受邮件的用户
*@returnture 发送成功, false发送失败*/
public Boolean send(ListtoList) {//Get system properties
Properties properties =System.getProperties();//Setup mail server
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");//create email authenticator.
Authenticator authenticator = newAuthenticator() {protectedPasswordAuthentication getPasswordAuthentication() {return newPasswordAuthentication(username, password);
}
};//Get the default Session object.
Session session =Session.getDefaultInstance(properties, authenticator);try{//Create a default MimeMessage object.
MimeMessage message = newMimeMessage(session);//Set From: header field of the header.
message.setFrom(newInternetAddress(from));//Set To: header field of the header.
for(String to : toList) {
message.addRecipient(Message.RecipientType.TO,newInternetAddress(to));
}//Set Subject: header field
message.setSubject(getSubject(), "utf-8");//Create the message part
BodyPart messageBodyPart = newMimeBodyPart();//Fill the message
messageBodyPart.setContent(getContent(), "text/html;charset=utf-8");//Create a multipar message
Multipart multipart = newMimeMultipart();//Set text message part
multipart.addBodyPart(messageBodyPart);//add attachment
List attchments =getAttachments();if (attchments != null && attchments.size() > 0) {for(File attachment : attchments) {
messageBodyPart= newMimeBodyPart();
DataSource source= newFileDataSource(attachment);
messageBodyPart.setDataHandler(newDataHandler(source));
messageBodyPart.setFileName(attachment.getName());
multipart.addBodyPart(messageBodyPart);
}
}//Send the complete message parts
message.setContent(multipart);//Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch(MessagingException mex) {
mex.printStackTrace();return false;
}return true;
}/*** 获取邮件主题,支持html格式。
*
*@return
*/
protected abstractString getSubject();/*** 获取邮件内容,支持html格式。
*
*@return
*/
protected abstractString getContent();/*** 获取附件列表,若不需要发送附件,请返回null或长度为0的List列表.
*
*@return
*/
protected abstract ListgetAttachments();
}