1. 程式人生 > >Spring Boot -- Email

Spring Boot -- Email

Spring Boot -- Email

1. pom.xml

傳送模板郵件需要用到spring-boot-starter-thymeleaf,其他其他郵件內容不需要該依賴

<!-- mail -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-mail</
artifactId
>
</dependency> <!-- thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

2. application.properties

spring.mail.host=郵箱伺服器
spring.mail.username=郵箱使用者名稱
spring.mail.password=郵箱授權碼
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

3.MailUtil

@Component
public class MailUtil {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private JavaMailSender javaMailSender;

    @Resource
    private TemplateEngine templateEngine;

    /**
     * 傳送簡單郵件
     * @param sender 傳送人
     * @param receiver 接收人
     * @param carbonCopy 抄送人
     * @param subject 主題
     * @param text 內容
     */
public void sendSimpleEmail(String sender, String[] receiver, String[] carbonCopy, String subject, String text){ long startTimestamp = System.currentTimeMillis(); logger.info("{} Start send mail ... ", startTimestamp); try { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(sender); message.setTo(receiver); message.setCc(carbonCopy); message.setSubject(subject); message.setText(text); javaMailSender.send(message); logger.info("{} Send mail success", System.currentTimeMillis()); } catch (Exception e) { e.printStackTrace(); logger.error("Send mail failed, error message is : {} \n", e.getMessage()); } } /** * 傳送html郵件 * @param sender * @param receiver * @param carbonCopy * @param subject * @param text * @param isHtml */ public void sendHtmlEmail(String sender, String[] receiver, String[] carbonCopy, String subject, String text, boolean isHtml){ long startTimestamp = System.currentTimeMillis(); logger.info("{} Start send mail ... ", startTimestamp); try { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message); mimeMessageHelper.setFrom(sender); mimeMessageHelper.setTo(receiver); mimeMessageHelper.setCc(carbonCopy); mimeMessageHelper.setSubject(subject); mimeMessageHelper.setText(text, isHtml); javaMailSender.send(message); logger.info("{} Send mail success", System.currentTimeMillis()); } catch (Exception e) { e.printStackTrace(); logger.error("Send mail failed, error message is : {} \n", e.getMessage()); } } /** * 傳送帶附件的郵件 * @param sender * @param receiver * @param carbonCopy * @param subject * @param text * @param isHtml * @param fileName * @param file */ public void sendAttachmentFileEmail(String sender, String[] receiver, String[] carbonCopy, String subject, String text, boolean isHtml, String fileName, File file){ long startTimestamp = System.currentTimeMillis(); logger.info("{} Start send mail ... ", startTimestamp); try { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message); mimeMessageHelper.setFrom(sender); mimeMessageHelper.setTo(receiver); mimeMessageHelper.setCc(carbonCopy); mimeMessageHelper.setSubject(subject); mimeMessageHelper.setText(text, isHtml); mimeMessageHelper.addAttachment(fileName, file); javaMailSender.send(message); logger.info("{} Send mail success", System.currentTimeMillis()); } catch (Exception e) { e.printStackTrace(); logger.error("Send mail failed, error message is : {} \n", e.getMessage()); } } /** * 傳送模板郵件 * @param sender * @param receiver * @param carbonCopy * @param subject * @param template * @param context */ public void sendTemplateEmail(String sender, String[] receiver, String[] carbonCopy, String subject, String template, Context context){ long startTimestamp = System.currentTimeMillis(); logger.info("{} Start send mail ... ", startTimestamp); try { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message); String text = templateEngine.process(template, context); mimeMessageHelper.setFrom(sender); mimeMessageHelper.setTo(receiver); mimeMessageHelper.setCc(carbonCopy); mimeMessageHelper.setSubject(subject); mimeMessageHelper.setText(text, true); javaMailSender.send(message); logger.info("{} Send mail success", System.currentTimeMillis()); } catch (Exception e){ e.printStackTrace(); logger.error("Send mail failed, error message is : {} \n", e.getMessage()); } } }

轉載
Spring Boot入門教程(二十七): Mail