SpringBoot之发送QQ邮件

在这里已QQ邮箱发送为案例

1、首先我们需要进入QQ邮箱顶部的设置

2、点击进入以后会出现导航栏 请选中你的账户

3、开启POP3/SMTP服务 并申请获得授权码

4、开始SpringBoot整合Email操作

Maven pom导入依赖

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

5、在application.properties 或者application.yml文件中配置您的邮箱信息

spring.mail.host=smtp.qq.com
spring.mail.username=QQ号@qq.com
spring.mail.password=填写你申请的授权码
spring.mail.defaultEncodint=UTF-8

6、开始发送邮件

import com.bean.springcloudcommon.model.Email;
import com.bean.springcloudemail.mapper.EmailMapper;
import com.bean.springcloudemail.servie.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;
import java.util.UUID;





@Service
public class EmailServiceImpl implements EmailService {
    @Autowired
    private EmailMapper emailMapper;
    @Autowired
    private JavaMailSender javaMailSender;



    @Override
    public void sendEmail(Long userId, String userEmail) {
        Email email = new Email();
        email.setUserId(userId);
        email.setCreateTime(new Date());
        email.setEmailState(0);
        email.setEmailKey(UUID.randomUUID().toString().replaceAll("-", ""));
        emailMapper.insert(email);
        try {
            sendQQEmail(userEmail, email.getEmailKey(), email.getEmailId());
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }


    /**

     * 发送简单文本邮件
     *
     * @param userEmail
     */

    private void sendSimpleQQEmail(String userEmail) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom("2538552544@qq.com");
        simpleMailMessage.setSubject("简单邮件");
        simpleMailMessage.setTo(userEmail);
        simpleMailMessage.setText("来来来 给你发了一封简单邮件");
        javaMailSender.send(simpleMailMessage);
    }









    /**
     * 发送HTML的邮件
     *
     * @param userEmail 用户邮箱
     * @param key       激活key
     * @param emailId   数据库保存邮箱id
     * @throws MessagingException
     */
    private void sendQQEmail(String userEmail, String key, Long emailId) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        //配置地址来源
        helper.setFrom("2538552544@qq.com");
        //发送邮箱地址
        helper.setTo(userEmail);
        //邮箱标题
        helper.setSubject("注册账户");
        //邮箱内容
        helper.setText("<html><body><a href='http://localhost:9005/email/api/email/validateEmail/" + emailId + "?key=" + key + "'>请点击我完成注册</a></body></htm>", true);
        //发送
        javaMailSender.send(mimeMessage);
    }


    /**
     * 发送带附件的邮件
     *
     * @param userEmail 用户邮箱
     * @param filePath  文件地址 也可以是文件数组传递多个附件
     * @throws MessagingException
     */
    private void sendAttachmentQQEmail(String userEmail, String filePath) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //自定义发送格式工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("2538552544@qq.com");
        helper.setTo(userEmail);
        helper.setSubject("发送一封带附件的邮箱");
        helper.setText("发送带附件的邮箱文件");
        File file = new File(filePath);
        FileSystemResource fileSystemResource = new FileSystemResource(file);
        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
        //可以添加多个的附件
        helper.addAttachment(fileName, fileSystemResource);
        javaMailSender.send(mimeMessage);

    }

}

7、接下来你就可以测试你自己的发邮箱功能啦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

难搞哦!!

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

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

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

打赏作者

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

抵扣说明:

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

余额充值