Springbootメールボックスの簡単な使用


pom:
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

構成:
#   QQ  
#     25465   ,      :
#  spring.mail.port=465
#  spring.mail.properties.mail.smtp.ssl.enable=true
spring:
  mail:
    host: smtp.cntaiping.com  #
    port: 25
    username: yusy02@xx.com #     
#    password: eyhtbgdrujetbgfh #    
    password: XXXXX #   
    properties:
      mail:
        smtp:
          ssl: false
          auth: true
          starttls:
            enable: true
            required: false
    default-encoding: UTF-8

#     
mail:
  fromMail:
    addr: yusy02@xxx.com  #    
  receptionMail:
    addr: yusy02@xxx.com  #    
  toCCMail:
    addr: yusy02@xxx.com  #   
   ...    

コード:Services:
package com.XXXX.XXX.XXX.email;

/**
 * yusy
 */
public interface MailService {

    /**
     *          
     * @param toAddr     
     * @param ccAddr           
     * @param title   
     * @param content   
     */
    public void sendTextMail(String toAddr[],String ccAddr[], String title, String content);


    /**
     *    html   
     * @param toAddr
     * @param ccAddr     
     * @param title
     * @param content   (HTML)
     */
    public void sendHtmlMail(String toAddr[],String ccAddr[], String title, String content);



    /**
     *          -  
     * @param toAddr
     * @param ccAddr     
     * @param title
     * @param content
     * @param filePaths     
     */
    public void sendAttachmentsMail(String toAddr[],String ccAddr[], String title, String content, String filePaths[]);


    /**
     *            (  )   -  
     * @param toAddr
     * @param ccAddr     
     * @param title
     * @param content
     * @param rscPath     
     * @param rscId   id (       )
     */
    public void sendInlineResourceMail(String toAddr[], String ccAddr[],String title, String content, String rscPath, String rscId);

}

impl:
package com.XXXX.email;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class MailServiceImpl implements MailService {

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

    @Resource
    private JavaMailSender mailSender;

    //     
    @Value("${mail.fromMail.addr}")
    private String from;

    /**
     *       
     *
     * @param toAddr
     * @param title
     * @param content
     */
    public void sendTextMail(String toAddr[], String title, String content) {
        sendTextMail(toAddr, null, title, content);
    }

    /**
     *       
     *
     * @param toAddr      
     * @param title     
     * @param content   
     */
    @Override
    public void sendTextMail(String toAddr[], String ccAddr[], String title, String content) {
        //        
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(toAddr);
        message.setSubject(title);
        message.setText(content);
        //  
        if (ccAddr != null && ccAddr.length > 0) {
            message.setCc(ccAddr);
        }

        try {
            mailSender.send(message);
            logger.info("Text      。");
        } catch (Exception e) {
            logger.error("  Text       !", e);
        }

    }


    /**
     *   html  
     *
     * @param toAddr
     * @param title
     * @param content
     */
    public void sendHtmlMail(String toAddr[], String title, String content) {
        sendHtmlMail(toAddr, null, title, content);
    }

    /**
     *   html  
     *
     * @param toAddr
     * @param title
     * @param content
     */
    @Override
    public void sendHtmlMail(String toAddr[], String ccAddr[], String title, String content) {
        // html     
        MimeMessage message = mailSender.createMimeMessage();
        try {
            //true         multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(toAddr);
            helper.setSubject(title);
            helper.setText(content, true);
            //  
            if (ccAddr != null && ccAddr.length > 0) {
                helper.setCc(ccAddr);
            }

            mailSender.send(message);
            logger.info("html      ");
        } catch (MessagingException e) {
            logger.error("  html       !", e);
        }
    }


    /**
     *         
     *
     * @param toAddr
     * @param title
     * @param content
     * @param filePath
     */
    public void sendAttachmentsMail(String toAddr[], String title, String content, String filePath[]) {
        sendAttachmentsMail(toAddr, null, title, content, filePath);
    }

    /**
     *         
     *
     * @param toAddr
     * @param title
     * @param content
     * @param filePaths
     */
    @Override
    public void sendAttachmentsMail(String toAddr[], String ccAddr[], String title, String content, String filePaths[]) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(toAddr);
            helper.setSubject(title);
            helper.setText(content, true);
            //  
            if (ccAddr != null && ccAddr.length > 0) {
                helper.setCc(ccAddr);
            }
            //  
            if (filePaths != null) {
                for (String filePath : filePaths) {
                    FileSystemResource file = new FileSystemResource(new File(filePath));
                    helper.addAttachment(file.getFilename(), file);
                }
            }
            mailSender.send(message);
            logger.info("          。");
        } catch (MessagingException e) {
            logger.error("             !", e);
        }
    }


    public void sendInlineResourceMail(String toAddr[], String title, String content, String rscPath, String rscId) {
        sendInlineResourceMail(toAddr, null, title, content, rscPath, rscId);
    }

    /**
     *           (  )   
     *
     * @param toAddr
     * @param title
     * @param content
     * @param rscPath
     * @param rscId
     */
    @Override
    public void sendInlineResourceMail(String toAddr[], String ccAddr[], String title, String content, String rscPath, String rscId) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(toAddr);
            helper.setSubject(title);
            helper.setText(content, true);
            //  
            if (ccAddr != null && ccAddr.length > 0) {
                helper.setCc(ccAddr);
            }
            FileSystemResource res = new FileSystemResource(new File(rscPath));
            helper.addInline(rscId, res);
            mailSender.send(message);
            logger.info("             。");
        } catch (MessagingException e) {
            logger.error("                !", e);
        }
    }
}