SpringBoot ~ 邮件发送

  1. 云栖社区>
  2. 博客>
  3. 正文

SpringBoot ~ 邮件发送

wsyjlly 2019-08-12 13:54:16 浏览123
展开阅读全文

SpringBoot邮件发送

  1. 添加pom依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
  2. application.properties配置

    spring.mail.host=smtp.qq.com
    spring.mail.port=465
    spring.mail.username=wsyjlly@qq.com
    spring.mail.password=khhvydlbhiqjcjaj
    spring.mail.default-encoding=UTF-8
    spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
    spring.mail.properties.mail.debug=true
  3. 编写邮件发送服务

    /**
     * @author wsyjlly
     * @create 2019.07.16 - 17:32
     **/
    @Component
    public class MailService {
        @Autowired
        JavaMailSender javaMailSender;
        
        //简单邮件发送
        public void sendSimpleSender(String from,String to,String cc,String subject,String content){
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom(from);
            simpleMailMessage.setTo(to);
            simpleMailMessage.setCc(cc);
            simpleMailMessage.setSubject(subject);
            simpleMailMessage.setText(content);
            javaMailSender.send(simpleMailMessage);
        }
        
        //带附件的邮件发送
        public void sendAttachFileMail(String from, String to, String subject, String content, File file) throws MessagingException {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content);
            helper.addAttachment(file.getName(),file);
            javaMailSender.send(mimeMessage);
    
        }
        
        public void sendMailWithImg(String from, String to, String subject, String content, String[] srcPath,String[] resIDs) throws MessagingException {
            if (srcPath.length != resIDs.length){
                System.out.println("发送失败!");
                return;
            }
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content,true);
            for (int i = 0; i < srcPath.length; i++) {
                FileSystemResource resource = new FileSystemResource(new File(srcPath[i]));
                helper.addInline(resIDs[i],resource);
            }
            javaMailSender.send(mimeMessage);
        }
    }
  4. 发送邮件

    /**
     * @author wsyjlly
     * @create 2019.06.10 - 19:00
     **/
    @RestController
    public class ControllerDemo1 {
        @Autowired
        MailService mailService;
    
        @RequestMapping("/mail1")
        public void mail1(){
            mailService.sendSimpleSender("wsyjlly@qq.com",
            "895864393@qq.com",
            "211874876@qq.com",
            "你爱我吗",
            "你这个坏孩子!");
        }
        
        @RequestMapping("/mail2")
        public void mail2() throws MessagingException {
            mailService.sendAttachFileMail("wsyjlly@qq.com","wsyjlly@qq.com","你信命吗?","你这个坏孩子!",new File("./uploadFiles/abc.gif"));
        }
        
        @RequestMapping("/mail3")
        public void mail3() throws MessagingException {
            mailService.sendMailWithImg("wsyjlly@qq.com","wsyjlly@qq.com","你信神吗?","<div>hello,坏孩子!"
                    +"<div><img src='cid:p01'/></div>"
                    +"<div><img src='cid:p02'/></div>"
                    +"</div>",
                    new String[]{"./mail/aaa.jpg","./mail/abc.gif"},
                    new String[]{"p01","p02"});
        }
    }
  5. 测试链接

  6. 发送成功

网友评论

登录后评论
0/500
评论
wsyjlly
+ 关注