一、添加依赖
<!-- mail --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
二、配置application.properties文件
#############################mail配置############################# spring.mail.host: smtp.163.com,smtp.mxhichina.com spring.mail.username:liuy930723@163.com,liuy@dadaisen.com spring.mail.password:123456geren,ASDasd123 spring.mail.properties.mail.smtp.auth: true
三、实现发送功能
MailConfiguration.javapackage com.example.demo.config; import java.util.ArrayList; import java.util.Map; import java.util.Properties; import javax.mail.internet.MimeMessage; import org.springframework.boot.autoconfigure.mail.MailProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.mail.MailException; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; /** * 实现多账号,轮询发送 * * @Author: 我爱大金子 * @Description: 实现多账号,轮询发送 * @Date: Create in 16:07 2017/7/4 */ @Configuration @EnableConfigurationProperties(MailProperties.class) public class MailConfiguration extends JavaMailSenderImpl implements JavaMailSender { private ArrayList<String> usernameList; private ArrayList<String> passwordList; private ArrayList<String> hostList; private int currentMailId = 0; private final MailProperties properties; public MailConfiguration(MailProperties properties) { this.properties = properties; // 初始化邮件服务器 if (hostList == null) hostList = new ArrayList<String>(); String[] hosts = this.properties.getHost().split(","); if (hosts != null) { for (String host : hosts) { hostList.add(host); } } // 初始化账号 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(hostList.get(currentMailId)); // 设置编码和各种参数 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); } }MailComponent.java
package com.example.demo.utils.component; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import javax.mail.MessagingException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import com.example.demo.config.MailConfiguration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; /** * 邮件发送 * * @Author: 我爱大金子 * @Description: 实邮件发送 * @Date: Create in 16:07 2017/7/4 */ @Component public class MailComponent { private static final String template = "mail/roncoo.ftl"; @Autowired private ThymeleafAutoConfiguration thymeleafAutoConfiguration; @Autowired private MailConfiguration mailConfiguration; /** * 发送邮件 * @Author: 我爱大金子 * @Description: 发送邮件 * @Date: 16:46 2017/7/4 * @param to 接收人 * @param text 发送内容 */ public void sendMail(String to, String text) { Map<String, Object> map = new HashMap<String, Object>(); map.put("email", to); try { send(to, text); } catch (IOException e) { e.printStackTrace(); } catch (MessagingException e ) { e.printStackTrace(); } } private String send(String email, String text) throws MessagingException, UnsupportedEncodingException { MimeMessage message = mailConfiguration.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); InternetAddress from = new InternetAddress(); from.setAddress(mailConfiguration.getUsername()); from.setPersonal("我爱大金子", "UTF-8"); helper.setFrom(from); helper.setTo(email); helper.setSubject("测试邮件"); helper.setText(text, true); mailConfiguration.send(message); return text; } }
四、测试
package com.example.demo; import com.example.demo.utils.component.MailComponent; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SpringBootEmailsApplicationTests { @Autowired private MailComponent mailComponent; @Test public void contextLoads() { mailComponent.sendMail("a1754966750@qq.com", "测试第1次"); } }
效果:
本文出自 “我爱大金子” 博客,请务必保留此出处http://1754966750.blog.51cto.com/7455444/1944506
原文地址:http://1754966750.blog.51cto.com/7455444/1944506