1.Pom中引入相关包
<!-- start java 提供的支持邮件发送相关业务的类 -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- end java 提供的支持邮件发送相关业务的类 -->
2.配置文件中配好发件,收件邮箱地址
3.发送邮件工具类
package cn.com.common.email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
* @Date: 2019/3/4 14:47
*/
@Component
public class EmailUtil {
@Value("${email_from_address}")
private String email_from_address;
@Value("${email_from_password}")
private String email_from_password;
@Value("${email_recipient_to}")
private String email_recipient_to;
@Value("${email_recipient_cc}")
private String email_recipient_cc;
public void send(String subject,String content) throws MessagingException {
//都不为空时发送邮件
if (!("").equals(email_from_address) && !("").equals(email_from_password) && !("").equals(email_recipient_to)) {
//1.确定连接位置
Properties props = new Properties();
//获取163邮箱smtp服务器的地址
props.setProperty("mail.transport.protocol", "smtp");
/**!不是企业邮箱可以直接将mail.smtp.host配置为smtp.163.com,我这里用的企业邮箱,所
*以配置为smtp.qiye.163.com!!!切记哟~我之前就是用企业邮箱作为发件人,这里配置了
*smtp.163.com,结果就一直验证失败,报javax.mail.AuthenticationFailedException:
*535 Error: authentication failed*/
props.setProperty("mail.smtp.host", "smtp.qiye.163.com");
//是否进行权限验证。
props.setProperty("mail.smtp.auth", "true");
//2.确定权限(账号和密码)
Authenticator authenticator = new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
//邮箱的登录帐号和授权密码。
return new PasswordAuthentication(email_from_address, email_from_password);
}
};
//3.获得连接
/**
* props:包含配置信息的对象,Properties类型。配置邮箱服务器地址、配置是否进行权限验证(帐号密码验证)等
* authenticator:确定权限(帐号和密码)
* 所以就要在上面构建这两个对象。
*/
Session session = Session.getDefaultInstance(props, authenticator);
//4.创建消息
Message message = createMessage(session, subject, content);
//5.发送消息
Transport.send(message);
System.out.println("邮件发送成功,发件人:" + email_from_address + ";收件人:" + email_recipient_to + ";抄送:" + email_recipient_cc);
}
}
private Message createMessage(Session session, String subject,String content) throws MessagingException {
//2 创建消息
Message message = new MimeMessage(session);
//2.1 发件人
message.setFrom(new InternetAddress(email_from_address));
/**
* 2.2 收件人
* 第一个参数:
* RecipientType.TO 代表收件人
* RecipientType.CC 抄送
* RecipientType.BCC 暗送
* 比如A要给B发邮件,但是A觉得有必要给要让C也看看其内容,就在给B发邮件时,
* 将邮件内容抄送给C,那么C也能看到其内容了,但是B也能知道A给C抄送过该封邮件
* 而如果是暗送(密送)给C的话,那么B就不知道A给C发送过该封邮件。
* 第二个参数
* 收件人的地址,或者是一个Address[],用来装抄送或者暗送人的名单。或者用来群发。可以是相同邮箱服务器的,也可以是不同的
*多个收件人:
* InternetAddress[] internetAddresses =
* new InternetAddress[user.getReceptEmails().size() - 1];
* for(int i = 0 ; i < user.getReceptEmails().size() ; i++ ){
* internetAddresses[i] =
* new InternetAddress(user.getReceptEmails().get(i));
* }
* message.setRecipients(Message.RecipientType.TO,internetAddresses);
*/
message.setRecipient(Message.RecipientType.TO, new InternetAddress(email_recipient_to));
if(email_recipient_cc != null && !("").equals(email_recipient_cc)){
message.setRecipient(Message.RecipientType.CC, new InternetAddress(email_recipient_cc));
}
//2.3 主题(标题),避免中文乱码
message.setSubject(MimeUtility.encodeText(subject,
MimeUtility.mimeCharset("UTF-8"), null));
//2.4 正文
//设置编码,防止发送的内容中文乱码。
message.setContent(content, "text/html;charset=UTF-8");
return message;
}
}