Spring boot 集成邮件服务

添加依赖


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-mailartifactId>
dependency>

添加配置文件


stmp.host=smtp.163.com
stmp.account=xxxxxxx@163.com
stmp.password=*********
mail.smtp.auth=true
mail.smtp.timeout=25000

创建实体


public class MyEmail {

    private String receiver;
    private String subject;
    private String text;
    private String content;

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

注册Bean


@Component
public class EmailConfig {

    @Value("${stmp.host}")
    private String host;
    @Value("${stmp.account}")
    private String account;
    @Value("${stmp.password}")
    private String password;

    @Value("${mail.smtp.auth}")
    private String isAuth;
    @Value("${mail.smtp.timeout}")
    private String outTime;


    @Bean(name = "JavaMailSenderImpl")
    public JavaMailSenderImpl getMailSender(){
        JavaMailSenderImpl javaMailSender= new JavaMailSenderImpl();
        javaMailSender.setHost(host);
        javaMailSender.setUsername(account);
        javaMailSender.setPassword(password);
        Properties properties=new Properties();
        properties.put("mail.smtp.auth", isAuth);
        properties.put("mail.smtp.timeout", outTime);
        javaMailSender.setJavaMailProperties(properties);
        return javaMailSender;
    }

    public void sendSimpleMail(MyEmail email){
        SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
        simpleMailMessage.setFrom(account);

        String receiver=email.getReceiver();
        String receivers[]=receiver.split(";");
        simpleMailMessage.setTo(receivers);
        simpleMailMessage.setSubject(email.getSubject());
        simpleMailMessage.setText(email.getContent());
        getMailSender().send(simpleMailMessage);
    }
}

测试


@Autowired
EmailConfig mc;
@Test
public void testEmailConfig(){
    MyEmail email = new MyEmail();
    email.setReceiver("[email protected]");
    email.setContent("welcome ");
    email.setSubject("test");
    mc.sendSimpleMail(email);
    System.out.println("successful to send message");
}

这样就可以发送简单的邮件消息。如果想要发送比较复杂的邮件,比如说有附件,有图片等。需要使用MimeMailMessage类来实现。这里面只需要修改的就是注册Bean的时候添加这个功能即可,附代码如下:


@Component
public class EmailConfig {

    @Value("${GaH.stmp.host}")
    private String host;
    @Value("${GaH.stmp.account}")
    private String account;
    @Value("${GaH.stmp.password}")
    private String password;

    @Value("${mail.smtp.auth}")
    private String isAuth;
    @Value("${mail.smtp.timeout}")
    private String outTime;

    @Value("${mail.attach.img}")
    private String imgPath;


    @Bean(name = "JavaMailSenderImpl")
    public JavaMailSenderImpl getMailSender(){
        JavaMailSenderImpl javaMailSender= new JavaMailSenderImpl();
        javaMailSender.setHost(host);
        javaMailSender.setUsername(account);
        javaMailSender.setPassword(password);
        Properties properties=new Properties();
        properties.put("mail.smtp.auth", isAuth);
        properties.put("mail.smtp.timeout", outTime);
        javaMailSender.setJavaMailProperties(properties);
        return javaMailSender;
    }

    public void sendSimpleMail(MyEmail email){
        SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
        simpleMailMessage.setFrom(account);
        String receiver=email.getReceiver();
        String receivers[]=receiver.split(";");
        simpleMailMessage.setTo(receivers);
        simpleMailMessage.setSubject(email.getSubject());
        simpleMailMessage.setText(email.getContent());
        getMailSender().send(simpleMailMessage);
    }

    public void sendMimeMail(MyEmail email){
        MimeMessage mimMessage= getMailSender().createMimeMessage();
        try {
            MimeMessageHelper messageHelper=new MimeMessageHelper(mimMessage,true,"utf-8");
            messageHelper.setFrom(account);
            String receiver=email.getReceiver();
            String receivers[]=receiver.split(";");
            messageHelper.setTo(receivers);

            messageHelper.setSubject(email.getSubject());
            messageHelper.setText(email.getContent(),true);//Set the second Param to True
            FileSystemResource res= new FileSystemResource(new File(imgPath));
            messageHelper.addInline("attachFile",res);
            getMailSender().send(mimMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }


}

配置文件都在application.properties中,如果想要自定义配置文件的位置,可以参考:
spring boot 自定义文件配置

你可能感兴趣的:(spring)