- 添加依賴
<!-- mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- 配置
spring.mail.host: smtp.exmail.qq.com
spring.mail.username:[email protected],[email protected],[email protected]
spring.mail.password:
spring.mail.properties.mail.smtp.auth: true
# 企業qq的郵箱或者是163這類,不建議使用私人qq
- 代碼實現
實現多賬號
/**
* 實現多賬號,輪詢發送
*
* @author wujing
*/
@Configuration
@EnableConfigurationProperties(MailProperties.class)
public class RoncooJavaMailSenderImpl extends JavaMailSenderImpl implements JavaMailSender {
private ArrayList<String> usernameList;
private ArrayList<String> passwordList;
private int currentMailId = 0;
private final MailProperties properties;
public RoncooJavaMailSenderImpl(MailProperties properties) {
this.properties = properties;
// 初始化賬號
if (usernameList == null)
usernameList = new ArrayList<String>();
String[] userNames = this.properties.getUsername().split(",");
if (userNames != null) {
for (String user : userNames) {
usernameList.add(user);
}
}
// 初始化密碼
if (passwordList == null)
passwordList = new ArrayList<String>();
String[] passwords = this.properties.getPassword().split(",");
if (passwords != null) {
for (String pw : passwords) {
passwordList.add(pw);
}
}
}
@Override
protected void doSend(MimeMessage[] mimeMessage, Object[] object) throws MailException {
super.setUsername(usernameList.get(currentMailId));
super.setPassword(passwordList.get(currentMailId));
// 設置編碼和各種參數
super.setHost(this.properties.getHost());
super.setDefaultEncoding(this.properties.getDefaultEncoding().name());
super.setJavaMailProperties(asProperties(this.properties.getProperties()));
super.doSend(mimeMessage, object);
// 輪詢
currentMailId = (currentMailId + 1) % usernameList.size();
}
private Properties asProperties(Map<String, String> source) {
Properties properties = new Properties();
properties.putAll(source);
return properties;
}
@Override
public String getUsername() {
return usernameList.get(currentMailId);
}
}
實現發送功能
/**
*
* @author wujing
*/
@Component
public class RoncooJavaMailComponent {
private static final String template = "mail/roncoo.ftl";
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
@Autowired
private RoncooJavaMailSenderImpl javaMailSender;
public void sendMail(String email) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("email", email);
try {
String text = getTextByTemplate(template, map);
send(email, text);
} catch (IOException | TemplateException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
private String getTextByTemplate(String template, Map<String, Object> model) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
return FreeMarkerTemplateUtils.processTemplateIntoString(freeMarkerConfigurer.getConfiguration().getTemplate(template), model);
}
private String send(String email, String text) throws MessagingException, UnsupportedEncodingException {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
InternetAddress from = new InternetAddress();
from.setAddress(javaMailSender.getUsername());
from.setPersonal("龍果學院", "UTF-8");
helper.setFrom(from);
helper.setTo(email);
helper.setSubject("測試郵件");
helper.setText(text, true);
javaMailSender.send(message);
return text;
}
}
- 測試
flt代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div style="width: 600px; text-align: left; margin: 0 auto;">
<h1 style="color: #005da7;">龍果學院</h1>
<div style="border-bottom: 5px solid #005da7; height: 2px; width: 100%;"></div>
<div style="border: 1px solid #005da7; font-size: 16px; line-height: 50px; padding: 20px;">
<div>${email},您好!</div>
<div>
這是個測試
</div>
<div style="border-bottom: 2px solid #005da7; height: 2px; width: 100%;"></div>
<div>掃一掃,關注龍果學院微信公共號,裏面更多精彩推薦</div>
<div>
<img src="http://account.roncoo.com/images/qrcode.png" alt="龍果學院公衆號二維碼" />
</div>
<div>
想了解更多信息,請訪問 <a href="http://www.roncoo.com">http://www.roncoo.com</a>
</div>
</div>
</div>
</body>
</html>
html代碼:
<input type="text" name="email" id="email" />
<button id="send">發送郵件</button>
js代碼:
$(function(){
$('#send').click(function(){
var email = $('#email').val();
$.ajax({
url:'/api/mail',
type:'post',
data:{'email':email},
success:function(msg){
alert(msg);
}
});
});
})
java代碼:
@Autowired
private RoncooJavaMailComponent component;
@RequestMapping(value = "mail")
public String mail(String email) {
component.sendMail(email);
return "success";
}