定义发送邮件API
public interface SendMail {
/**
* @param subject
* @param message
* @param recipients
* @throws MessagingException
*/
void send(String subject, String message, String... recipients) throws MessagingException, UnsupportedEncodingException;
/**
* 向一个人发送邮件(带多个附件)
*
* @param subject
* @param message
* @param attachments
* @param recipients
* @throws MessagingException
* @throws UnsupportedEncodingException
*/
void send(String subject, String message, List attachments, String... recipients) throws MessagingException, UnsupportedEncodingException;
}
发送邮件具体实现
public class SendMailImpl extends Authenticator implements SendMail {
private String username;
private String password;
//发送邮件的props文件
private Properties props = System.getProperties();
//邮箱发送会话
private Session session;
/**
* 带参数的构造器,初始化邮箱登录的用户名和密码
*
* @param username
* @param password
*/
public SendMailImpl(String username, String password) {
this.username = username;
this.password = password;
//根据发送邮箱的账号解析出smtp服务器
String smtpHostName = "smtp." + username.split("@")[1];
//初始化props
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", smtpHostName);
props.put("mail.smtp.ssl.enable", true);
//发送验证
//创建邮箱收发会话的session
session = Session.getInstance(props, this);
}
//重写该方法,获得 PasswordAuthentication 对象
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
@Override
public void send(String subject, String message, String... recipients) throws MessagingException, UnsupportedEncodingException {
this.send(subject,message,null,recipients);
}
@Override
public void send(String subject, String message, List attachments, String... recipients) throws MessagingException, UnsupportedEncodingException {
//根据session创建MimeMessage
MimeMessage mimeMessage = new MimeMessage(this.session);
//发件人
mimeMessage.setFrom(new InternetAddress(this.username,"趁我们还未老","UTF-8"));
//收件人们
InternetAddress[] addresses = new InternetAddress[recipients.length];
for (int i = 0; i < addresses.length; i++) {
addresses[i] = new InternetAddress(recipients[i]);
}
mimeMessage.setRecipients(MimeMessage.RecipientType.TO, addresses);
//主题
mimeMessage.setSubject(subject);
// msg.setFrom (new InternetAddress ("[email protected]", "这里是需要的昵称", "UTF-8"));
//邮件附件
if (attachments != null) {
//邮件正文
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(message, "text/html;charset=utf-8");
//附件
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(contentPart);
for (File attachment : attachments) {
BodyPart attachmentPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachment);
attachmentPart.setDataHandler(new DataHandler(source));
//避免中文乱码的处理
attachmentPart.setFileName(MimeUtility.encodeWord(attachment.getName()));
multipart.addBodyPart(attachmentPart);
}
mimeMessage.setContent(multipart);
//保存邮件
mimeMessage.saveChanges();
}else {
//内容
mimeMessage.setContent(message, "text/html;charset=utf-8");
}
//发送
Transport.send(mimeMessage);
}
}