JAVA实现QQ邮箱推送邮件

JAVA实现QQ邮箱推送邮件



准备

  1. 在QQ邮箱主界面开启POP/SMTP服务:设置–账户–开启POP/SMTP服务
    开启后会给一个POP3/SMTP服务的授权码,保存好该授权码。

  2. 我们还需要一个mail.jar ,下载参考下方连接

    JavaMail包下载


一、在项目中引入JavaMail包


在这里插入图片描述

二、开始实现

1.我们在Service类里面编写一个方法,这个方法接收两个参数:1收件人邮箱 2.要发送的内容

代码如下:

package org.jeecg.modules.EmailPush.service.impl;

import com.sun.mail.util.MailSSLSocketFactory;
import org.jeecg.modules.EmailPush.entity.Emailpush;
import org.jeecg.modules.EmailPush.mapper.EmailpushMapper;
import org.jeecg.modules.EmailPush.service.IEmailpushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

/**
 * @Description: 邮箱信息推送
 * @Author: 汪杰杰杰杰
 * @Date: 2022-04-15
 * @Version: V1.0
 */
@Service
public class EmailpushServiceImpl extends ServiceImpl<EmailpushMapper, Emailpush> implements IEmailpushService {


    @Autowired
    JavaMailSender javaMailSender;

    @Override
    public Boolean EmailPush(String email, String data) {

        Boolean bool = false;   //用于判定是否发送成功

        //设置发送邮件的主机  smtp.qq.com

        String host = "smtp.qq.com";

        //1.创建连接对象,连接到邮箱服务器

        Properties props = System.getProperties();

        //Properties 用来设置服务器地址,主机名 。。 可以省略

        //设置邮件服务器

        props.setProperty("mail.smtp.host", host);

        props.put("mail.smtp.auth", "true");

        //SSL加密

        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        }

        sf.setTrustAllHosts(true);

        props.put("mail.smtp.ssl.enable", "true");

        props.put("mail.smtp.ssl.socketFactory", sf);

        //props:用来设置服务器地址,主机名;Authenticator:认证信息

        Session session = Session.getDefaultInstance(props, new Authenticator() {

            //通过密码认证信息

            protected PasswordAuthentication getPasswordAuthentication() {

                //new PasswordAuthentication(用户名, password);

                //这个用户名密码就可以登录到邮箱服务器了,用它给别人发送邮件

                return new PasswordAuthentication("1526668275@qq.com", "OP3/SMTP服务的授权码");

            }
        });

        try {

            Message message = new MimeMessage(session);

            //2.1设置发件人:

            message.setFrom(new InternetAddress("填写发件人的邮箱"));

            //2.2设置收件人 email变量存的是收件人邮箱

            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email));

            //2.3邮件的主题

            message.setSubject("QQ邮箱信息推送测试");

            //2.4设置邮件的正文 第一个参数是邮件的正文内容 第二个参数是:是文本还是html的连接

            message.setContent("<h1>来自汪杰杰杰的一条消息:</h1><h3>" + data + "</h3>", "text/html;charset=UTF-8");

            //3.发送一封激活邮件

            Transport.send(message);

        } catch (MessagingException mex) {

            mex.printStackTrace();

        }

        bool = true;
        return bool;
    }

}

PostMan调用测试

在这里插入图片描述

效果展示

在这里插入图片描述


实现代码

我将该功能实现的源码放进阿里云盘,感兴趣的同学可以自行下载学习

JAVA实现QQ邮箱推送邮件功能源码

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

汪杰杰杰杰

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

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

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

打赏作者

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

抵扣说明:

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

余额充值