阿里邮箱发送带excel附件邮件

导包

java 复制代码
<dependency>
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
      <version>1.4.7</version>
</dependency>

内容

java 复制代码
调用
EmilUtil.sendEmail("xxxx@163.com",
                             host,
                             username,
                             password,
                             port,
                             excelFile,
                             "主题",
                             content);

工具类

java 复制代码
package com.ruoyi.common.utils.emil;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import java.security.Security;
import java.util.Map;
import java.util.Properties;

public class EmilUtil {


public static void sendEmail(String toEmail, String host, String username, String password,String port, File file, String bt, String contentXML) {
        try {
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
            Properties props = System.getProperties();
            props.setProperty("mail.smtp.host", host);
            props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.setProperty("mail.smtp.socketFactory.fallback", "false");
            //设置端口
            props.setProperty("mail.smtp.port", port);
            //启用调试
            props.setProperty("mail.debug", "true");
            props.setProperty("mail.smtp.socketFactory.port", port);
            props.setProperty("mail.smtp.auth", "true");
            //建立邮件会话
            Session session = Session.getDefaultInstance(props, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
            //建立邮件对象
            MimeMessage message = new MimeMessage(session);
            //设置邮件的发件人、收件人、主题
            //发件人账号
            message.setFrom(new InternetAddress(username));
            //收件人账号
            message.setRecipients(Message.RecipientType.TO, toEmail);
            //邮件标题
            message.setSubject(bt);
            //内容
            Multipart multipart = new MimeMultipart();
            BodyPart contentPart = new MimeBodyPart();
            //邮件内容
            contentPart.setContent(contentXML, "text/html;charset=utf-8");
            multipart.addBodyPart(contentPart);
            message.setContent(multipart);

            //附件
            // 创建消息部分
            BodyPart messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(file);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(file.getName());
            multipart.addBodyPart(messageBodyPart);
            message.saveChanges();

            Transport.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

配置

java 复制代码
//注意阿里邮箱设置设置第三方权限验证则无法验证通过    
mail:
        host: smtp.qiye.aliyun.com
        port: 465
        username: xxx@aliyun.com
        password: xxxx@#123465
相关推荐
马剑威(威哥爱编程)3 分钟前
鸿蒙6开发视频播放器的屏幕方向适配问题
java·音视频·harmonyos
@游子5 分钟前
内网渗透笔记-Day5
运维·服务器
JIngJaneIL7 分钟前
社区互助|社区交易|基于springboot+vue的社区互助交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·社区互助
Yawesh_best34 分钟前
告别系统壁垒!WSL+cpolar 让跨平台开发效率翻倍
运维·服务器·数据库·笔记·web安全
V***u45336 分钟前
MS SQL Server partition by 函数实战二 编排考场人员
java·服务器·开发语言
ttod_qzstudio1 小时前
深入理解 Vue 3 的 h 函数:构建动态 UI 的利器
前端·vue.js
这是程序猿1 小时前
基于java的ssm框架旅游在线平台
java·开发语言·spring boot·spring·旅游·旅游在线平台
_大龄1 小时前
前端解析excel
前端·excel
i***t9191 小时前
基于SpringBoot和PostGIS的云南与缅甸的千里边境线实战
java·spring boot·spring
k***08291 小时前
【监控】spring actuator源码速读
java·spring boot·spring