SpringBoot ~ 邮件发送

简介: SpringBoot邮件发送添加pom依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId></dependency>application.

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. 发送成功
目录
相关文章
|
2月前
|
前端开发 JavaScript Java
【实操】SpringBoot监听Iphone15邮件提醒,Selenium+Python自动化抢购脚本
本文介绍了一个结合SpringBoot和Python的实用功能,旨在监控iPhone 15的库存状态并通过邮件提醒用户。系统采用SpringBoot监听苹果官网API,解析JSON数据判断是否有货,并展示最近的库存记录。此外,还能自动触发Selenium+Python脚本实现自动化购买。文中详细介绍了技术栈、接口分析、邮件配置及自动化脚本的设置方法。该项目不仅适用于熟悉后端开发的人员,也适合回顾Layui和Jquery等前端技术。
48 0
【实操】SpringBoot监听Iphone15邮件提醒,Selenium+Python自动化抢购脚本
消息中间件 缓存 监控
93 0
|
3月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的邮件过滤系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的邮件过滤系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
4月前
|
安全 Java Maven
在 Spring Boot 中实现邮件发送功能可以通过集成 Spring Boot 提供的邮件发送支持来完成
在 Spring Boot 中实现邮件发送功能可以通过集成 Spring Boot 提供的邮件发送支持来完成
53 2
|
4月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的邮件过滤系统的详细设计和实现
基于SpringBoot+Vue的邮件过滤系统的详细设计和实现
50 1
|
3月前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的邮件过滤系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的邮件过滤系统附带文章源码部署视频讲解等
29 0
|
3月前
|
Java API Spring
Spring Boot中如何实现邮件发送功能
Spring Boot中如何实现邮件发送功能
|
4月前
|
Java API Spring
Spring Boot中如何实现邮件发送功能
Spring Boot中如何实现邮件发送功能
|
4月前
|
Java
springboot使用邮件服务
springboot使用邮件服务
|
4月前
|
Java 数据安全/隐私保护
Springboot拓展之整合邮件 JavaMail的使用与实操
Springboot拓展之整合邮件 JavaMail的使用与实操
30 0