首先加代码配置文件来一个

mail.properties



mail.smtp.host=smtp.qq.com mail.smtp.port=465 mail.smtp.SSLport=587
mail.smtp.starttlsEnable=true mail.smtp.auth=true
[email protected] mail.smtp.password=qrqzbcfslyhaai
mail.smtp.timeout=50000 mail.smtp.connectiontimeout=50000 mail.smtp.nickname=ewe
我使用的是腾讯qq邮箱
首先要去腾讯qq的设置===》账户》》开通授权码,第三方登陆需要授权码当成密码使用



然后在springboot引入这个配置文件


package com.ewe.core.utils; import java.util.Arrays; import java.util.Date;
import java.util.List; import java.util.Properties; import javax.mail.Message;
import javax.mail.Session; import javax.mail.Transport; import
javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage;
import org.apache.commons.logging.Log; import
org.apache.commons.logging.LogFactory; import
org.springframework.boot.context.properties.ConfigurationProperties; import
org.springframework.context.annotation.PropertySource; import
org.springframework.stereotype.Component; import
com.ewe.core.datasource.TransactionAspect; import
com.ewe.user.model.BatchSendMessage; @Component
@ConfigurationProperties(prefix="mail.smtp") @PropertySource("mail.properties")
public class MailUtil { //("${mail.smtp.host}") private String host;
//("${mail.smtp.port}") private String port; //("${mail.smtp.SSLport}") private
String SSLport; //("${mail.smtp.starttlsEnable}") private String
starttlsEnable; //("${mail.smtp.auth}") private String auth;
//("${mail.smtp.connectiontimeout}") private String connectiontimeout;
//("${mail.smtp.account}") private String account; //("${mail.smtp.password}")
private String password; //("${mail.smtp.timeout}") private String timeout;
private String nickname ="ewe";//发送者,显示的发件人名字 protected static Log LOGGER =
LogFactory.getLog(MailUtil.class); //带CC(抄送)的邮件发送 public boolean
sendSSLEmailWithCC(List<String> listEmails, List<String> cc, String title,
String content ,List<String> bcc) throws Exception {
LOGGER.info("开始发送邮件,email==>"+listEmails+",title==>"+title); Properties props =
System.getProperties(); props.put("mail.smtp.host", host); // 指定协议
props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.port",
SSLport); props.put("mail.smtp.starttls.enable", starttlsEnable);
props.put("mail.smtp.auth", auth); props.put("mail.smtp.connectiontimeout",
connectiontimeout); props.put("mail.smtp.timeout", timeout);
props.put("mail.smtp.debug", true); Transport transport = null; Session session
= Session.getDefaultInstance(props, null); transport =
session.getTransport("smtp"); transport.connect(host, account, password);
MimeMessage msg = new MimeMessage(session); msg.setSentDate(new Date());
InternetAddress fromAddress = new InternetAddress(account,nickname);
msg.setFrom(fromAddress); InternetAddress[] toAddress = new
InternetAddress[listEmails.size()]; for(int k = 0;k<listEmails.size();k++){
String emailAddress = listEmails.get(k); new InternetAddress(emailAddress);
toAddress[k]=new InternetAddress(emailAddress); }
LOGGER.debug("当前需要发送邮件的地址信息============>{}"+Arrays.deepToString(toAddress));
msg.addRecipients(Message.RecipientType.TO, toAddress); //增加CC功能 if(cc != null)
{ InternetAddress[] ccAddress = new InternetAddress[cc.size()]; for(int k =
0;k<cc.size();k++){ String emailAddress = cc.get(k); new
InternetAddress(emailAddress); ccAddress[k]=new InternetAddress(emailAddress);
} msg.addRecipients(Message.RecipientType.CC, ccAddress); } //增加BCC功能 if(bcc !=
null) { InternetAddress[] bccAddress = new InternetAddress[bcc.size()]; for(int
k = 0;k<bcc.size();k++){ String emailAddress = bcc.get(k); new
InternetAddress(emailAddress); bccAddress[k]=new InternetAddress(emailAddress);
} msg.addRecipients(Message.RecipientType.BCC, bccAddress); }
msg.setSubject(title, "UTF-8"); //发送文本邮件 msg.setContent(content, "text/html;
charset=utf-8"); transport.sendMessage(msg, msg.getAllRecipients());
LOGGER.info("邮件已发送"); return true; } public String getHost() { return host; }
public void setHost(String host) { this.host = host; } public String getPort()
{ return port; } public void setPort(String port) { this.port = port; } public
String getSSLport() { return SSLport; } public void setSSLport(String sSLport)
{ SSLport = sSLport; } public String getStarttlsEnable() { return
starttlsEnable; } public void setStarttlsEnable(String starttlsEnable) {
this.starttlsEnable = starttlsEnable; } public String getAuth() { return auth;
} public void setAuth(String auth) { this.auth = auth; } public String
getConnectiontimeout() { return connectiontimeout; } public void
setConnectiontimeout(String connectiontimeout) { this.connectiontimeout =
connectiontimeout; } public String getAccount() { return account; } public void
setAccount(String account) { this.account = account; } public String
getPassword() { return password; } public void setPassword(String password) {
this.password = password; } public String getTimeout() { return timeout; }
public void setTimeout(String timeout) { this.timeout = timeout; } public
static Log getLOGGER() { return LOGGER; } public static void setLOGGER(Log
lOGGER) { LOGGER = lOGGER; } public String getMailTitle() { return nickname; }
public void setMailTitle(String nickname) { this.nickname = nickname; } }测试可用
依赖maven加的是
<!-- email --> <dependency> <groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId> <version>1.5.4</version> </dependency>
里面的一些port 是不加密 SSLport是加密的