springboot入门_email
本文记录在springboot中发送邮件
创建springboot工程,并引入邮件服务需要的jar
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
在application.properties属性文件中配置邮件服务信息
# Email (MailProperties) spring.mail.default-encoding=UTF-8 spring.mail.host=smtp.qq.com spring.mail.protocol=smtp spring.mail.port=25 spring.mail.username=emial #第三方使用时 密码需用邮箱的授权码 spring.mail.password=your email authorization code spring.mail.test-connection=false
spring中向我们提供了一个接口JavaMailSender帮我们实现邮件的发送,在服务启动时会根据配置信息实例化供我们使用
定义发送邮件的接口方法
public interface EmailSenderService { /** * 发送简单文本邮件 * @param subject 邮件主题 * @param content 邮件内容 * @param to 收件人 */ void sendTextMail(String subject, String content, String... to); /** * 发送包含html标签内容的邮件 * @param subject 邮件主题 * @param htmlContent 邮件内容 * @param to 收件人 */ void sendHtmlTextMail(String subject, String htmlContent, String to); /** * 发送带附件邮件 * @param subject 主题 * @param content 邮件内容 * @param to 收件人 * @param filePath 附件路径 * @param fileName 附件名 */ void sendAttachmentMail(String subject, String content, String to, String filePath, String fileName); /** * 发送简单文本邮件 并抄送 * @param subject * @param content * @param ccList 抄送人 * @param tos 收件人,多个时用逗号隔开 */ void sendTextMail(String subject, String content, List<String> ccList, String... tos); }
接口的实现类
@Service public class EmailSenderServiceImpl implements EmailSenderService { @Autowired private JavaMailSender javaMailSender; @Value("${spring.mail.username}") private String from; @Override public void sendTextMail(String subject, String content, String... to) { SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setFrom(from); mailMessage.setTo(to); mailMessage.setSubject(subject); mailMessage.setText(content); mailMessage.setSentDate(new Date()); javaMailSender.send(mailMessage); } @Override public void sendHtmlTextMail(String subject, String htmlContent, String to) { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(htmlContent, true); helper.setSentDate(new Date()); FileSystemResource fsr = new FileSystemResource(new File("C:\\Users\\admin\\Pictures\\Camera Roll\\光头强.jpg")); helper.addInline("imgSrcContentId", fsr);//此处的contentId需要和html中的对应 javaMailSender.send(mimeMessage); } catch (MessagingException e) { e.printStackTrace(); } } @Override public void sendAttachmentMail(String subject, String content, String to, String filePath, String fileName) { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content); //附件 FileSystemResource file = new FileSystemResource(new File(filePath)); helper.addAttachment(fileName, file); javaMailSender.send(mimeMessage); } catch (MessagingException e) { e.printStackTrace(); } } @Override public void sendTextMail(String subject, String content, List<String> ccList, String... tos) { //收件人 InternetAddress[] addressesTo = new InternetAddress[tos.length]; if(tos != null && tos.length>0){ for(int i=0;i<tos.length;i++){ InternetAddress addressTo = null; try { addressTo = new InternetAddress(tos[i]); addressesTo[i] = addressTo; } catch (AddressException e) { e.printStackTrace(); } } } //抄送人 InternetAddress[] addressesCc = new InternetAddress[ccList.size()]; if(ccList != null && ccList.size() > 0){ for(int i=0;i<ccList.size();i++){ String ccAdd = ccList.get(i); InternetAddress address = null; try { address = new InternetAddress(ccAdd); addressesCc[i] = address; } catch (AddressException e) { e.printStackTrace(); } } } MimeMessagePreparator preparator = new MimeMessagePreparator() { @Override public void prepare(MimeMessage mimeMessage) throws Exception { mimeMessage.setFrom(from); mimeMessage.setSubject(subject); mimeMessage.setText(content); mimeMessage.setRecipients(Message.RecipientType.TO, addressesTo); mimeMessage.setRecipients(Message.RecipientType.CC, addressesCc); } }; javaMailSender.send(preparator); } }
单元测试,注入EmailSenderService,调用发送邮件方法
@RunWith(SpringRunner.class) @SpringBootTest public class EmailApplicationTests { @Autowired private EmailSenderService emailSenderService; @Test public void contextLoads() { String to = "receiver email"; String subject = "email subject"; //String content = "This is the test message, see when the message has been sent successfully!"; //emailSenderService.sendTextMail(to, subject, content); String content = "<html><body><span style='color:yellow'>This is the test mail containing HTML, see when the message has been sent successfully!<span><img " + "src='cid:imgSrcContentId'></body><html>"; emailSenderService.sendHtmlTextMail(subject, content, to); // String content = "This is the test with the attachment of the mail, when you see that the mail with attachment has been sent successfully!"; // String filePath = "your file path"; // String fileName = "your file name"; // emailSenderService.sendAttachmentMail(subject, content, to, filePath, fileName); // List<String> ccList = new ArrayList<String>(); // ccList.add("Cc 'er 1's mailbox"); // ccList.add("Cc 'er 2's mailbox"); // ... // ccList.add("Cc 'er n's mailbox"); // emailSenderService.sendTextMail(subject, content, ccList, "Recipient 1's mailbox", "Recipient 2's mailbox", // ..., "Recipient n's mailbox"); } }