使用email发送exec

该代码片段展示了如何在Java项目中使用EasyExcel库处理数据并将之转换为Excel文件,然后通过自定义的sendFileEmail方法利用ApachePOI和邮件服务发送带有附件的电子邮件。
摘要由CSDN通过智能技术生成

使用到的依赖:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.3.4</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>

代码

    public void importVehicleInfoExec() throws Exception {
        List<VehicleDailyData> vehicleDailyDataList = vehicleService.getVehicleDailyDateList();
        ByteArrayOutputStream out = null;
        try {
            // 返回excel文件流
            out = ExcelUtil.generateExcel(vehicleDailyDataList, VehicleDailyData.class);
            LOG.debug("开始发送邮件===========================");
            LOG.debug("开始发送邮件===========================");
            LOG.debug("开始发送邮件===========================");
            SendMailUtil.sendFileEmail("车辆日常运行报表",out,"2039701916@qq.com",mailProperties);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (MailException e) {
            throw new RuntimeException(e);
        } finally {
            if(out != null){
                out.close();
            }
        }
    }

public class ExcelUtil {

    public static <T> ByteArrayOutputStream generateExcel(List<T> dataList, Class<T> clazz) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        // excel写入
        EasyExcel.write(out,clazz).sheet(0).doWrite(dataList);
        return out;
    }

}
    public static void sendFileEmail(String subject, ByteArrayOutputStream outputStream, String to, MailProperties mailProperties) throws Exception {
        Properties prop = new Properties();
        prop.setProperty("mail.host", mailProperties.getHost());
        prop.setProperty("mail.transport.protocol", mailProperties.getProtocol());
        prop.setProperty("mail.smtp.auth", "true");
        prop.setProperty("mail.smtp.ssl.enable", "true");
        Session session = Session.getInstance(prop);
        Transport ts = session.getTransport();
        ts.connect(mailProperties.getHost(), mailProperties.getUsername(), mailProperties.getPassword());
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(mailProperties.getUsername()));
        message.setRecipients(RecipientType.TO, StringUtils.isNotBlank(to) ? to : "chkai.good@163.com");
        message.setSubject(subject);
        MimeBodyPart mbp = new MimeBodyPart();
        DataSource source = new ByteArrayDataSource(outputStream.toByteArray(), "application/msexcel");
        mbp.setDataHandler(new DataHandler(source));
        mbp.setFileName(MimeUtility.encodeText(subject + ".xls"));
        MimeMultipart multipart = new MimeMultipart();
        multipart.addBodyPart(mbp);
        message.setContent(multipart);
        ts.sendMessage(message, message.getAllRecipients());
        ts.close();
    }
mailProperties对象 是我们自己写的一个配置类,里面有自己的邮箱,密码等信息。
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
发送匿名邮件,您可以在 QT 中使用 SMTP 协议来发送邮件。以下是一个简单的示例代码,可以帮助您了解如何使用 QT 发送匿名邮件: ```c++ #include <QCoreApplication> #include <QtNetwork/QSslSocket> #include <QtNetwork/QSslConfiguration> #include <QtNetwork/QHostInfo> #include <QtNetwork/QHostAddress> #include <QtNetwork/QNetworkInterface> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkReply> #include <QByteArray> #include <QTextStream> #include <QDateTime> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString smtp_server = "smtp.gmail.com"; // SMTP 服务器地址 int smtp_port = 465; // SMTP 服务器端口号(SSL) QString user_email = "your_email@gmail.com"; // 发送方邮箱地址 QString user_name = "Anonymous"; // 发送方姓名(可选) QString user_password = "your_password"; // 发送方邮箱密码 QString receiver_email = "receiver_email@gmail.com"; // 接收方邮箱地址 QString subject = "Test Email"; // 邮件主题 QString body = "This is a test email from QT!"; // 邮件正文 QString boundary = "ChitGPT-2021"; // 邮件分隔符 QString message; // 构造邮件头部信息 message += "From: " + user_name + " <" + user_email + ">\n"; message += "To: " + receiver_email + "\n"; message += "Subject: " + subject + "\n"; message += "Date: " + QDateTime::currentDateTime().toString("ddd, dd MMM yyyy hh:mm:ss +0800") + "\n"; message += "MIME-Version: 1.0\n"; message += "Content-Type: multipart/alternative; boundary=" + boundary + "\n\n"; // 构造邮件正文 message += "--" + boundary + "\n"; message += "Content-Type: text/plain; charset=UTF-8\n\n"; message += body + "\n\n"; message += "--" + boundary + "--\n"; // 连接 SMTP 服务器 QSslSocket *socket = new QSslSocket(); socket->connectToHostEncrypted(smtp_server, smtp_port); if (!socket->waitForConnected()) { qDebug() << "Error: " << socket->errorString(); return a.exec(); } // SSL 配置 QSslConfiguration config = socket->sslConfiguration(); config.setProtocol(QSsl::TlsV1_2); socket->setSslConfiguration(config); // 接收服务器返回的信息 if (!socket->waitForReadyRead()) { qDebug() << "Error: " << socket->errorString(); return a.exec(); } // 发送邮件认证信息 QTextStream textStream(socket); textStream << "EHLO " + QHostInfo::localHostName() + "\r\n"; textStream << "AUTH LOGIN\r\n"; textStream << QByteArray().append(user_email.toUtf8()).toBase64() + "\r\n"; textStream << QByteArray().append(user_password.toUtf8()).toBase64() + "\r\n"; textStream << "MAIL FROM: <" + user_email + ">\r\n"; textStream << "RCPT TO: <" + receiver_email + ">\r\n"; textStream << "DATA\r\n"; // 发送邮件内容 textStream << message + "\r\n.\r\n"; textStream << "QUIT\r\n"; // 接收服务器返回的信息 if (!socket->waitForReadyRead()) { qDebug() << "Error: " << socket->errorString(); return a.exec(); } // 关闭连接 socket->close(); delete socket; qDebug() << "Email has been sent successfully!"; return a.exec(); } ``` 请替换示例代码中的 SMTP 服务器地址、端口号、发送方邮箱地址、密码、接收方邮箱地址、邮件主题、邮件正文等信息,以便发送您自己的匿名邮件。注意,由于涉及到邮箱密码和敏感信息,请妥善保管好您的代码和密码,以免被他人恶意利用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值