Springboot发送邮件操作(简单邮件、复杂邮件)

一:导入pom依赖

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

二:配置application.properties中邮件相关属性

在这里插入图片描述
这里的密码是加密的,需要登录发送邮件账户,在设置中开启pop3/smtp服务(我用的是qq邮箱),即可自动生成加密的密码
在这里插入图片描述

三:在测试类中实现发送邮件功能

@SpringBootTest
class SpringbootAsyncApplicationTests {

    @Autowired
    JavaMailSenderImpl javaMailSender;  //用于发送邮件
    
    @Test
    void contextLoads() {

        //一个简单邮件
        SimpleMailMessage simpleMail = new SimpleMailMessage();
        simpleMail.setSubject("zym你好");              //邮件主题
        simpleMail.setText("are you OK?");            //邮件内容
        simpleMail.setTo("691639910@qq.com");         //接收者
        simpleMail.setFrom("691639910@qq.com");        //发送者
        javaMailSender.send(simpleMail);
    }

    @Test
    void contextLoads2() throws MessagingException {

        //一个复杂邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //用MimeMessageHelper组装复杂邮件,第二个参数为true,可以发送附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        //正文,可以发送html文本,需要第二个参数为true
        helper.setSubject("zym你好");
        helper.setText("<p style='color:red'>春暖花开</p>",true);
        //附件
        helper.addAttachment("1.png", new File("C:\\Users\\LENOVO\\Pictures\\1.png"));

        helper.setTo("691639910@qq.com");           //接收者
        helper.setFrom("691639910@qq.com");         //发送者

        javaMailSender.send(mimeMessage);
    }
}
以下是使用Spring Boot发送复杂邮件的步骤: 1.在pom.xml文件中添加spring-boot-starter-mail依赖项。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> <version>2.2.6.RELEASE</version> </dependency> ``` 2.在application.properties文件中配置邮件服务器的相关信息。 ```properties spring.mail.host=smtp.example.com spring.mail.port=587 spring.mail.username=yourusername spring.mail.password=yourpassword spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 3.创建一个邮件服务类,该类将使用JavaMailSender发送邮件。 ```java @Service public class EmailService { @Autowired private JavaMailSender mailSender; public void sendEmailWithAttachment(String to, String subject, String text, String pathToAttachment) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(to); helper.setSubject(subject); helper.setText(text); FileSystemResource file = new FileSystemResource(new File(pathToAttachment)); helper.addAttachment("Attachment", file); mailSender.send(message); } } ``` 4.在控制器中使用邮件服务类发送邮件。 ```java @RestController public class EmailController { @Autowired private EmailService emailService; @PostMapping("/sendEmail") public ResponseEntity<String> sendEmail(@RequestParam String to, @RequestParam String subject, @RequestParam String text, @RequestParam String pathToAttachment) { try { emailService.sendEmailWithAttachment(to, subject, text, pathToAttachment); return ResponseEntity.ok("Email sent successfully."); } catch (MessagingException e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error sending email: " + e.getMessage()); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mitsuha三葉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值