当前位置: 首页 > article >正文

阿里邮箱发送邮件 Java

实体对象 分为授权实体跟测试实体
授权实体
@Data
public class EmailAuthorization {
//网易163邮箱的SMTP服务器地址 smtp.qiye.aliyun.com
    private String host;
//网易163邮箱的SMTP服务器端口
    private String post;
//邮箱用户名
    private String username;
//邮箱密码
    private String password;
//可不填非必要参数
    private Properties properties;
}

参数实体

@Data
public class EmailMsgData {
//接收人
    private String to;
//标题
    private String title;
//内容
    private String content;

}

调用方法类

import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

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

@Component
@Slf4j
public class EmailSendMsgHandle {


    public void sendMsgOne(EmailAuthorization emailAuthorization, EmailMsgData emailMsgData){
        ArrayList<EmailMsgData> emailMsgDatas = new ArrayList<>();
        emailMsgDatas.add(emailMsgData);
        sendMsgBatch(emailAuthorization, emailMsgDatas);
    }


    public void sendMsgBatch(EmailAuthorization emailAuthorization, List<EmailMsgData> emailMsgDatas){
        log.info("发邮件");
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        if (null == emailAuthorization.getProperties()) {
            Properties properties = System.getProperties();
            properties.setProperty("mail.smtp.auth", "true");
            properties.setProperty("mail.smtp.host", emailAuthorization.getHost());//smtp服务器地址
            properties.setProperty("mail.smtp.from", emailAuthorization.getUsername());//mailfrom 参数
            properties.setProperty("mail.user",emailAuthorization.getUsername());//发件人的账号
            properties.setProperty("mail.password",emailAuthorization.getPassword());// 发件人的账号的密码,如果开启三方客户端安全密码请使用新生产的密码
            properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            properties.setProperty("mail.smtp.socketFactory.fallback", "false");
            properties.setProperty("mail.smtp.socketFactory.port", emailAuthorization.getPost());
            emailAuthorization.setProperties(properties);
        }
        mailSender.setDefaultEncoding("utf-8");
        mailSender.setJavaMailProperties(emailAuthorization.getProperties());
        mailSender.setHost(emailAuthorization.getHost());
        mailSender.setPassword(emailAuthorization.getPassword());
        mailSender.setUsername(emailAuthorization.getUsername());
        mailSender.setPort(Integer.parseInt(emailAuthorization.getPost()));
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message, true);
            // 设置发送方邮箱地址
            helper.setFrom(emailAuthorization.getUsername());

            for (EmailMsgData emailMsgData : emailMsgDatas) {
                helper.setSubject(emailMsgData.getTitle());
                helper.setText(emailMsgData.getContent(), true);
                helper.setTo(emailMsgData.getTo());
                mailSender.send(message);
            }
        } catch (MessagingException e) {
            e.printStackTrace();
         
        }
    }
}

调用

@Autowired private EmailSendMsgHandle emailSendMsgHandle;

//授权参数装配

EmailAuthorization emailAuthorization = new EmailAuthorization();
//发送参数装配
EmailMsgData emailMsgData = new EmailMsgData();
//发送
emailSendMsgHandle.sendMsgOne(emailAuthorization,emailMsgData);

示例

 


                

http://www.kler.cn/a/3077.html

相关文章:

  • 爬虫基础(三)Session和Cookie讲解
  • https数字签名手动验签
  • 安卓(android)读取手机通讯录【Android移动开发基础案例教程(第2版)黑马程序员】
  • 低代码产品插件功能一览
  • 零基础Vue入门4——Vue3基础核心
  • Windows11无法打开Windows安全中心主界面
  • 【C语言进阶】 12. 假期测评①
  • go语言gin框架学习
  • Android开发-Android UI与布局
  • vue里面的 Object.defineProperty 和 Proxy使用优势
  • 【AR技术】AR教学机器人
  • 【Python语言基础】——Python 字典方法
  • 用Node.js实现一个HTTP服务器程序(文件服务器)
  • 【K哥爬虫普法】大众点评VS百度地图,论“数据权属”对爬虫开发的罪与罚!
  • JQuery
  • ESP32设备驱动-HDC1080温度湿度传感器驱动
  • 【Pytorch】利用PyTorch实现图像识别
  • 标准I/O
  • 用Pytorch搭建一个房价预测模型
  • 关于学习的一些网站(自用)
  • 【C++】内联函数inline
  • 数据结构与算法——堆的基本存储
  • 电路设计的一些概念
  • 华为OD机试题,用 Java 解【卡片组成的最大数字】问题 | 含解题说明
  • 8个你一看就觉得很棒的Vue开发技巧
  • Liunx下的进程程序替换