import org.springframework.stereotype.Service;
import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
/**
* 简单邮件(不带附件的邮件)发送器
*/
@Service
public class SimpleMailSenderService {
private static String mailServerHost = "XXXX.163.com";//可以是163邮箱的地址
private static String mailServerPort = "25";
private static String sendUserName = "***.163.com";//
private static String sendUserPassword ="123456" ;//
private static String sendMailProtocol = PropertiesUtil.sendMailProtocol ;////邮件发送协议
/**
* 文本格式发送邮件
*
* @param addArray
* 目标地址数组
* @param subject
* 主题
* @param content
* 内容
* @return
*/
public static boolean sendTextMail(Address[] addArray, String subject, String content) throws Exception {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
// 如果需要身份认证,则创建一个密码验证器
authenticator = new MyAuthenticator(sendUserName, sendUserPassword);
Properties p = new Properties();
p.put("mail.smtp.host", mailServerHost);
p.put("mail.smtp.port", mailServerPort);
p.put("mail.smtp.auth", true);
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(p, authenticator);
try {
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(sendUserName);
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
mailMessage.setRecipients(Message.RecipientType.TO, addArray);
// 设置邮件消息的主题
mailMessage.setSubject(subject);
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件消息的主要内容
mailMessage.setText(content);
// 发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
throw new Exception(ex.getMessage(), ex);
}
}
/**
* 以HTML格式发送邮件
*
* @param mailInfo
* 待发送的邮件信息
* @throws MessagingException
* @throws UnsupportedEncodingException
*/
public static void sendHtmlMail(String to, String subject, String messageText) throws MessagingException, UnsupportedEncodingException {
// Step 1: Configure the mail session
java.util.Properties props = new java.util.Properties();
props.setProperty("mail.smtp.auth", "true");// 指定是否需要SMTP验证
props.setProperty("mail.smtp.host", mailServerHost);// 指定SMTP服务器
props.put("mail.transport.protocol", sendMailProtocol);//邮件发送协议
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);// 是否在控制台显示debug信息
// Step 2: Construct the message
InternetAddress fromAddress = new InternetAddress(sendUserName);
InternetAddress toAddress = new InternetAddress(to);
MimeMessage testMessage = new MimeMessage(mailSession);
testMessage.setFrom(fromAddress);
testMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);
testMessage.setSentDate(new java.util.Date());
testMessage.setSubject(MimeUtility.encodeText(subject, "gb2312", "B"));
testMessage.setContent(messageText, "text/html;charset=gb2312");
// Step 3: Now send the message
Transport transport = mailSession.getTransport("smtp");
transport.connect(mailServerHost, sendUserName, sendUserPassword);
transport.sendMessage(testMessage, testMessage.getAllRecipients());
transport.close();
}
/**
* 以HTML格式发送多人邮件
*
* @param mailInfo
* 待发送的邮件信息
* @throws MessagingException
* @throws UnsupportedEncodingException
*/
public static void sendHtmlMailMore(List<InternetAddress> to, String subject, String messageText) throws MessagingException, UnsupportedEncodingException {
// Step 1: Configure the mail session
java.util.Properties props = new java.util.Properties();
props.setProperty("mail.smtp.auth", "true");// 指定是否需要SMTP验证
props.setProperty("mail.smtp.host", mailServerHost);// 指定SMTP服务器
props.put("mail.transport.protocol", sendMailProtocol);//邮件发送协议
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);// 是否在控制台显示debug信息
// Step 2: Construct the message
InternetAddress fromAddress = new InternetAddress(sendUserName);
// 设置多个收件人地址
InternetAddress[] internetAddressTo = (InternetAddress[]) to.toArray(new InternetAddress[to.size()]);
MimeMessage testMessage = new MimeMessage(mailSession);
testMessage.setFrom(fromAddress);
testMessage.setRecipients(javax.mail.Message.RecipientType.TO, internetAddressTo);
testMessage.setSentDate(new java.util.Date());
testMessage.setSubject(MimeUtility.encodeText(subject, "gb2312", "B"));
testMessage.setContent(messageText, "text/html;charset=gb2312");
// Step 3: Now send the message
Transport transport = mailSession.getTransport("smtp");
transport.connect(mailServerHost, sendUserName, sendUserPassword);
transport.sendMessage(testMessage, testMessage.getAllRecipients());
transport.close();
}
public static void main(String[] args) throws AddressException {
String to = "CCC@qq.com";
String subject = "html邮件测试"; //subject javamail自动转码
StringBuffer theMessage = new StringBuffer();
theMessage.append("<div style='margin-top:10px'>发送的内容</div>");
List<InternetAddress> list = new ArrayList<InternetAddress>();
list.add(new InternetAddress("AAAA@qq.com"));
list.add(new InternetAddress("BBBB@qq.com"));
try {
sendHtmlMailMore( list, subject, theMessage.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class MyAuthenticator extends Authenticator {
String userName = null;
String password = null;
public MyAuthenticator() {
}
public MyAuthenticator(String username, String password) {
this.userName = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
}