springboot2入门到实战-整合QQ邮箱

springboot整合QQ邮箱

配置邮箱

登录邮箱服务器: 登录QQ邮箱

image-20230622182119608

image-20230622182251409

image-20230622182859546

image-20230622182939884

image-20230622183148220

image-20230622183307585image-20230622183328768

image-20230622183450114

image-20230622183615512

springboot整合email

导入依赖

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

配置

image-20230622185113812

emai业务类

  • 接口
public interface IMailService {
    void sendMail(String from , String to, String subject, String content);
}
  • 实现类
package com.wnhz.mq.tools.service.impl;

import com.wnhz.mq.tools.service.IMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class MailServiceImpl implements IMailService {

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void sendMail(String to, String subject, String content) {

        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setFrom(from);
        mail.setTo(to);
        mail.setSubject(subject);
        mail.setSentDate(new Date());
        mail.setText(content);
        javaMailSender.send(mail);
    }
}

单元测试

image-20240227152743586

image-20240227152825147

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot整合QQ邮箱,首先需要导入邮件依赖包,并在配置文件中设置相关信息。具体步骤如下: 1. 在项目的pom.xml文件中添加spring-boot-starter-mail依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 2. 在application.yaml(或application.properties)配置文件中添加QQ邮箱的相关信息,包括用户名(即QQ邮箱账号)、密码(即QQ邮箱的授权码)、以及SMTP服务器地址和端口号: ``` spring[email protected] spring.mail.password=your-password spring.mail.host=smtp.qq.com spring.mail.properties.mail.smtp.ssl.enable=true ``` 请注意替换上述代码中的"[email protected]"和"your-password"为你自己的邮箱账号和授权码。 3. 编写发送邮件的方法,可以使用JavaMailSender来发送简单文字邮件或复杂类型邮件。示例代码如下: ``` // 导入JavaMailSender相关的包和类 import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; // 在需要发送邮件的地方注入JavaMailSender @Autowired private JavaMailSender mailSender; // 发送简单文字邮件的方法 public void sendSimpleEmail() { SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setSubject("邮件主题"); mailMessage.setText("邮件内容"); mailMessage.setTo("[email protected]"); mailMessage.setFrom("[email protected]"); mailSender.send(mailMessage); } // 发送复杂类型邮件的方法 public void sendComplexEmail() throws MessagingException { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject("邮件主题"); helper.setText("<p style='color:red'>邮件内容</p>", true); helper.addAttachment("附件名称", new File("附件路径")); helper.setTo("[email protected]"); helper.setFrom("[email protected]"); mailSender.send(mimeMessage); } ``` 请注意将上述示例代码中的"[email protected]"替换为你自己的邮箱地址。 通过以上步骤,你就可以在Spring Boot中成功整合QQ邮箱并发送邮件了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [SpringBoot整合邮件任务(qq为例)](https://blog.csdn.net/qq2844509367/article/details/127500442)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值