首页 > 编程语言 >Java 发送邮件(2024-03)

Java 发送邮件(2024-03)

时间:2024-03-27 18:35:11浏览次数:28  
标签:qq 03 Java String new 2024 import mail com

1\

2\

package org.jeecg.common.util.io;

import com.sun.mail.util.MailSSLSocketFactory;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;

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.util.*;

@Slf4j
public class MailUtil {
    public static void main(String[] args) throws Exception {
        sendQQEmail("[email protected]", "主题", "自测内容" + DateUtils.getNowStr());
		//sendQQEmailHasFile("[email protected]", "主题", "自测内容" + DateUtils.getNowStr(), Arrays.asList(new File("C:\\Users\\xxxxx\\Desktop\\微信图片_xxxxx192242.jpg")));
    }

    private static Boolean sendQQEmail(String receiveEmail, String subject, String content) throws Exception {
        Session session = getQQSession("smtp.qq.com", "[email protected]", "密码");
        return send(session, "[email protected]", receiveEmail, subject, content, null);
    }

    private static Boolean sendQQEmailHasFile(String receiveEmail, String subject, String content, List<File> fileList) throws Exception {
        Session session = getQQSession("smtp.qq.com", "[email protected]", "密码");
        return send(session, "[email protected]", receiveEmail, subject, content, fileList);
    }

    private static List<BodyPart> getBodyPartList(List<File> fileList) throws Exception {
        List<BodyPart> partList = new ArrayList<>();
        if (fileList == null || fileList.size() <= 0) {
            return partList;
        }
        for (File file : fileList) {
            BodyPart bodyPart_curr = new MimeBodyPart();
            DataSource dataSource = new FileDataSource(file.getPath());
            bodyPart_curr.setDataHandler(new DataHandler(dataSource));
            bodyPart_curr.setFileName(file.getName());
            partList.add(bodyPart_curr);
        }
        return partList;
    }

    private static Session getQQSession(String host, String userName, String password) throws Exception {
        Properties properties = System.getProperties();
        MailSSLSocketFactory sslSocketFactory = new MailSSLSocketFactory();
        sslSocketFactory.setTrustAllHosts(true);
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.ssl.socketFactory", sslSocketFactory);
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.auth", "true");
        //properties.put("mail.smtp.starttls.enable", "true");

        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }
        });
        return session;
    }

    private static Boolean send(Session session, String sendEmail, String receiveEmail, String subject, String content, List<File> fileList) throws Exception {
        try {
            MimeMessage message = new MimeMessage(session);
            //防止邮件被当然垃圾邮件处理,披上Outlook的马甲
            message.addHeader("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
            message.setFrom(new InternetAddress(sendEmail));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(receiveEmail));
            message.setSubject(subject);
            //message.setText(content);

            BodyPart bodyPart_text = new MimeBodyPart();
            bodyPart_text.setText(content);

            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(bodyPart_text);

            List<BodyPart> bodyPartList = getBodyPartList(fileList);
            bodyPartList.forEach(a -> {
                try {
                    multipart.addBodyPart(a);
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            });

            message.setContent(multipart);

            Transport.send(message);

            String msg = DateUtils.getNowStrForLogPre()
                    + String.format("邮件发送成功,发送者:%s,接收者:%s,主题:%s,内容:%s", sendEmail, receiveEmail, subject, content);
            log.info(msg);

            return true;
        } catch (Exception e) {
            String msg = DateUtils.getNowStrForLogPre()
                    + String.format("邮件发送失败:%s,发送者:%s,接收者:%s,主题:%s,内容:%s", e.getMessage(), sendEmail, receiveEmail, subject, content);
            log.error(msg, e);
            return false;
        }
    }

    private static Session getCapSession(String smtpServer, int smtpPort, String password) {
        Properties properties = new Properties();
        properties.put("mail.smtp.host", smtpServer);
        properties.put("mail.smtp.port", smtpPort);
        properties.put("mail.smtp.auth", "false");
        properties.put("mail.smtp.starttls.enable", "false");
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("from", password);
            }
        });
        return session;
    }
}

标签:qq,03,Java,String,new,2024,import,mail,com
From: https://www.cnblogs.com/kikyoqiang/p/18099960

相关文章

  • Java内存马2-Spring内存马
    Spring内存马目录Spring内存马1、Spring&SpringMVC简介2、环境搭建3、Controller内存马4、踩坑日记5、Interceptor内存马1、Spring&SpringMVC简介Spring框架是一个开源的Java应用框架,它提供了一个综合的基础设施,用于构建Java应用程序。Spring框架的主要技术包括:依赖注入(Dep......
  • AJAX(Asynchronous JavaScript and XML)是一种用于创建交互式网页应用程序的技术
    AJAX(AsynchronousJavaScriptandXML)是一种用于创建交互式网页应用程序的技术。通过在后台与服务器进行异步通信,实现在不重新加载整个页面的情况下更新部分页面内容。而Spring是一个开源的Java框架,它提供了一种简化Java开发的方式,包括Web应用程序开发。下面是一个使用AJAX......
  • 基于JAVA的超市管理系统设计与实现
    摘 要现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本超市管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达......
  • 2024MathorCup数学建模思路A题B题C题D题思路汇总 妈妈杯建模思路分享
    文章目录1赛题思路2比赛日期和时间3组织机构4建模常见问题类型4.1分类问题4.2优化问题4.3预测问题4.4评价问题5建模资料1赛题思路(赛题出来以后第一时间在CSDN分享)https://blog.csdn.net/dc_sinor?type=blog2比赛日期和时间报名截止时间:2024年4月11......
  • 2024妈妈杯数学建模思路ABCD题思路汇总分析 MathorCup建模思路分享
    文章目录1赛题思路2比赛日期和时间3组织机构4建模常见问题类型4.1分类问题4.2优化问题4.3预测问题4.4评价问题5建模资料1赛题思路(赛题出来以后第一时间在CSDN分享)https://blog.csdn.net/dc_sinor?type=blog2比赛日期和时间报名截止时间:2024年4月11......
  • java智慧工地源码 大型建筑公司应用的智慧工地系统源码 智慧工地建筑管理系统源码
    java智慧工地源码大型建筑公司应用的智慧工地系统源码智慧工地建筑管理系统源码智慧工地是智慧地球理念在工程领域的具体体现,它代表了一种全新的工程全生命周期管理理念。通过运用信息化手段,智慧工地能够精确设计和模拟工程项目,实现互联协同、智能生产、科学管理的施工项目......
  • tomcat 启动报错javax.naming.NameNotFoundException: 名称[xxx.LoginFilter/xxxServi
    本地测试没问题,部署到服务器上的tomcat,启动报错javax.naming.NameNotFoundException:名称[xxx.LoginFilter/xxxService]未在此上下文中绑定可能是由于在Tomcat的配置文件中,资源名称[xxxx]没有正确配置或者引用。为了解决这个问题,你可以尝试以下步骤:1、检查你的Tomcat配置文......
  • Java进程假死排查 《二》
    在使用docker部署的项目可以参考第一篇文章:https://www.cnblogs.com/heavenTang/p/18027006如果是非docker部署的,那么往下看:步骤1.top输入top命令,找到占用CPU最高的进程。按Shift+P键排序:可以看到CPU占用最高的pid是92129。步骤2.top-Hppid查看指定进程......
  • 2024年三款企业级Java报表工具测评,总有一个适合你!
    报表是企业管理不可或缺的工具,通过将庞大的数据整理成易懂的图表和图形,其为决策者提供了直观的洞察力。准确的报表能够揭示业务趋势、关键指标和潜在机会,助力企业实时监控绩效、制定战略,并作出迅速而明智的决策。无论规模大小,企业都能从报表中获得精准、可视的数据,帮助其更高效、......
  • C. Theofanis' Nightmare
    原题链接题解太巧妙了!!层加式?code#include<bits/stdc++.h>#definelllonglongusingnamespacestd;lla[100005]={0};intmain(){llt;cin>>t;while(t--){lln;cin>>n;for(inti=1;i<=n;i++)cin>&g......