其他分享
首页 > 其他分享> > 邮箱发送部分相关代码

邮箱发送部分相关代码

作者:互联网

springboot 邮件发送相关的maven依赖

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

实现类

package cn.eangaie.appcloud.service.impl;

import cn.eangaie.appcloud.entity.Extra;
import cn.eangaie.appcloud.service.ExtraService;
import cn.eangaie.appcloud.service.MailService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @Author Ean
 * @Description
 * @Date 2021/10/28
 */
@Service
@Slf4j
public class MailServiceImpl implements MailService {
    @Autowired
    ExtraService extraService;
    static JavaMailSenderImpl javaMailSender;

    /**
     * @Author Ean
     * @Description 初始化发送方邮箱信息
     * @Date 2021/10/28
     */
    @Override
    public void iniMailProperties() {
        try {
            //自定义字符串,返回值为 { "host":"smtp.qq.com", "userName":"xxx@qq.com", "userPass":"xxx", "port":465, "protocol":"smtp" }
            String info = extraService.selectOne(new EntityWrapper<Extra>().eq("extra_type", 80)).getExtraCon();
            JSONObject json = JSONObject.parseObject(info);
            String host = json.getString("host");
            String userName = json.getString("userName");
            String pass = json.getString("userPass");
            int port = json.getInteger("port");
            String protocol = json.getString("protocol");
            javaMailSender = new JavaMailSenderImpl();
            javaMailSender.setHost(host);
            javaMailSender.setUsername(userName);
            javaMailSender.setPassword(pass);
            javaMailSender.setPort(port);
            javaMailSender.setProtocol(protocol);
            javaMailSender.setDefaultEncoding("UTF-8");
            Properties pro = new Properties();
            pro.put("mail.smtp.ssl.enable", "true");
            pro.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            pro.put("mail.smtp.auth", "true");
            pro.put("mail.smtp.port", port);
            pro.put("mail.smtp.socketFactory.port", port);
            pro.put("mail.smtp.socketFactory.fallback", "false");
            javaMailSender.setJavaMailProperties(pro);
            log.info("初始化邮箱发件方信息host{} userName{} userPass{}", host, userName, pass);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("初始化失败");
        }
    }

    /**
     * @param msg
     * @param toEmail
     * @Author Ean
     * @Description 邮箱发送,可以发送富文本
     * @Date 2021/10/28
     */
    @Override
    public String sendMail(String msg, String toEmail) {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = null;
        try {
            messageHelper = new MimeMessageHelper(message, true);
            messageHelper.setFrom(javaMailSender.getUsername());
            messageHelper.setTo(toEmail);
            messageHelper.setSubject("订单状态通知");
            messageHelper.setText(msg, true);
            javaMailSender.send(message);
            return "发送成功";
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return "发送失败";
    }
}

标签:javaMailSender,String,import,代码,pro,发送,mail,邮箱,port
来源: https://www.cnblogs.com/eangaie/p/15476788.html