1. 功能
- 发送普通邮件
- 发送htm邮件
- 发送带附件的邮件
- 发送带静态资源的邮件
2. 实现
类结构图
3. 实现
接口
package com.jihite.service; public interface MailService { void sendSimpleMail(String to, String subject, String content); void sendHtmlMail(String to, String subject, String content); void sendAttachMail(String to, String subject, String content, String filePath); void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId); }
实现
package com.jihite.service.impl; import com.jihite.service.MailService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @Component public class MailServiceImpl implements MailService{ private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private JavaMailSender mailSender; @Value("${mail.fromMail.addr}") private String from; @Override public void sendSimpleMail(String to, String subject, String content) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setText(content); try { mailSender.send(message); logger.info("简易邮件发送成功"); } catch (Exception e) { logger.info("简易邮件发送失败", e); } } @Override public void sendHtmlMail(String to, String subject, String content) { MimeMessage message = mailSender.createMimeMessage(); try { MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true); mimeMessageHelper.setFrom(from); mimeMessageHelper.setTo(to); mimeMessageHelper.setSubject(subject); mimeMessageHelper.setText(content, true); mailSender.send(message); } catch (MessagingException e) { logger.error("发送html邮件时发生异常", e); } } @Override public void sendAttachMail(String to, String subject, String content, String filePath) { MimeMessage mimeMessage = mailSender.createMimeMessage(); try { MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); mimeMessageHelper.setFrom(from); mimeMessageHelper.setTo(to); mimeMessageHelper.setSubject(subject); mimeMessageHelper.setText(content, true); FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath)); String fileName = "附件"; mimeMessageHelper.addAttachment(fileName, fileSystemResource); mailSender.send(mimeMessage); logger.info("带附件的邮件发送成功!"); } catch (MessagingException e) { logger.error("发送带附件失败", e); } } @Override public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) { MimeMessage mimeMessage = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); FileSystemResource res = new FileSystemResource(new File(rscPath)); helper.addInline(rscId, res); mailSender.send(mimeMessage); logger.info("发送嵌入静态资源的邮件成功"); } catch (MessagingException e) { logger.error("发送嵌入静态资源的邮件失败", e); } } }
resources/application.properties
spring.application.name=spirng-boot-mail spring.mail.host=smtp.163.com spring.mail.username=xxoo@xxoo.com spring.mail.password=xxoo spring.mail.default-encoding=UTF-8 mail.fromMail.addr=xxoo@xxoo.com
测试
package com.jihite.service; 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 MailServiceTest { @Autowired private MailService mailService; private String to = "jihite@126.com"; @Test public void testSimpleMail() { String subject = "测试普通邮件"; String content = "Hello, This is a simple mail"; mailService.sendSimpleMail(to, subject, content); } @Test public void testHtmlMail() { String subject = "测试待Html邮件"; String content="<html>\n" + "<body>\n" + " <h3>hello world ! 这是一封html邮件!</h3>\n" + "</body>\n" + "</html>"; mailService.sendHtmlMail(to, subject, content); } @Test public void testAttachMail() { String subject = "测试带附件邮件"; String content = "请注意查收附件哦"; String filePath = "/Users/jihite/Del/testMail"; mailService.sendAttachMail(to, subject, content, filePath); } @Test public void testInlineResourceMail() { String subject = "带图片的邮件"; String rscId = "neo006"; String content="<html><body>这是有图片的邮件:<img src=\'cid:" + rscId + "\' ></body></html>"; String imgPath = "/Users/jihite/Downloads/abc.png"; mailService.sendInlineResourceMail(to, subject, content, imgPath, rscId); } }