java mail api_使用JavaMail API发送邮件

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值