java从腾讯邮箱获得文件

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容Redis),内存型 2GB
简介: java从腾讯邮箱获得文件
package com.jack.platformweb.email.service;
import com.sun.mail.imap.IMAPStore;
import com.jack.common.redis.service.RedisService;
import com.jack.common.utils.DateUtils;
import com.jack.common.utils.JsonUtils;
import com.jack.common.utils.ZipUtils;
import com.jack.db.urgerobot.robot.model.RobotInfo;
import com.jack.platformweb.partner.service.GzOutUrgeFileService;
import com.jack.platformweb.robot.service.RobotInfoService;
import net.lingala.zip4j.exception.ZipException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;
import javax.print.DocFlavor;
import java.io.*;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
 * @Description
 * @Author zhenghao
 * @Date 2021/8/17 22:07
 **/
@Service
public class RemoteEmailService {
    private static Logger log = LoggerFactory.getLogger(RemoteEmailService.class);
    @Value("${gz.local.path}")
    private String attachFilePath;
    @Autowired
    private GzOutUrgeFileService gzOutUrgeFileService;
    @Autowired
    private RobotInfoService robotInfoService;
    @Autowired
    private RedisService redisService;
    //从哪个邮箱地址获取内容
    private static String username = "test@qq.com";
    private static String password = "123456";
    private static Integer robotId = 161;
    //过滤发件人
    private static List<String> monitoringEmail = Stream.of("tt@qq.com").collect(Collectors.toList());
    private IMAPStore getConnect() {
        log.info("开始获得邮件链接");
        // 准备连接服务器的会话信息
        IMAPStore store = null;
        try {
            Properties props = new Properties();
//            props.setProperty("mail.store.protocol", "imap");
//            props.setProperty("mail.imap.host", "imap.exmail.qq.com");
//            props.setProperty("mail.imap.port", "993");
//            props.setProperty("mail.imap.ssl.enable", "true");
//
//            props.setProperty("mail.imap.auth.plain.disable", "true");
//
//            Session session = Session.getInstance(props, new javax.mail.Authenticator() {
//                @Override
//
//                protected PasswordAuthentication getPasswordAuthentication() {
//                    return new PasswordAuthentication(username, password
//
//                    );
//
//                }
//
//            });
            props.put("mail.imap.host", "imap.exmail.qq.com");//QQ邮箱为:imap.qq.com
            props.put("mail.imap.auth", "true");
            props.setProperty("mail.store.protocol", "imap");
            props.put("mail.imap.starttls.enable", "true");
            Session session = Session.getInstance(props);
//            // 创建Session实例对象
//            Session session = Session.getInstance(props);
            // 创建IMAP协议的Store对象
            store = (IMAPStore) session.getStore("imap");
            // 连接邮件服务器
            store.connect("imap.exmail.qq.com", username, password);
        } catch (Exception e) {
            log.info("成功获得邮件链接失败");
            e.printStackTrace();
        }
        log.info("成功获得邮件链接");
        return store;
    }
    public boolean receiveEmail() {
        log.info("开始执行远程获得邮件任务");
        try {
            String key = "remoteEmailkey" + DateUtils.getToDayStartYYMMMDD();
            String s = redisService.get(key);
            if (StringUtils.isNotEmpty(s)) {
                log.info("远程获得邮件已经成功执行");
                return false;
            }
            /**
             * 1. 取得链接
             */
            IMAPStore imapStore = getConnect();
            if (null == imapStore) {
                log.error("获取邮箱imap连接失败。方法执行失败。退出当前方法!!!!!!!");
                return false;
            }
            Folder defaultFolder = imapStore.getDefaultFolder();
            if (defaultFolder == null) {
                log.info("服务器不可用");
                return false;
            }
            Folder folder = imapStore.getFolder("INBOX");
            folder.open(Folder.READ_WRITE);
            //获得所有近30天的邮件,近30天是在客户端配置的
            Message[] messages = folder.getMessages();
//        Message[] messages = folder.getMessages(folder.getMessageCount() - folder.getUnreadMessageCount() + 1, folder.getMessageCount());
            log.info("未读邮件数量: {}", messages.length);
            for (Message msg : messages) {
                try {
                    //邮件发送日期超过两天 退出遍历方法
                    if (!outDays(msg)) {
                        log.info("邮件{}已过期,退出循环", msg.getSubject());
                        continue;
                    }
                    //不是最新邮件,跳过
                    //                if (isNew(msg)) {
                    //                    log.info("邮件不是新邮件,跳过");
                    //                    continue;
                    //                }
                    //是否属于需要处理的邮件
                    if (needProcessEmail(msg)) {
                        redisService.setex(key, 20 * 60 * 60, "1");
                        break;
                    } else {
                        log.info("邮件不需要处理,跳过");
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
            //关闭资源
            folder.close(true);
            imapStore.close();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return false;
    }
    /**
     * @param
     * @param msg
     * @return boolean
     * @author FeianLing
     * @date 2019/8/20
     * @desc 检查当前邮件是否已超过1天, 接收时间大于1天以前 返回true
     */
    private boolean outDays(Message msg) {
        String s = "";
        Date receiveDate = null;
        try {
            receiveDate = msg.getReceivedDate();
            s = DateUtils.formatYYYYMMDD(receiveDate);
            log.info("邮件接收时间:{}", DateUtils.formatYYYYMMDD(receiveDate));
            return DateUtils.getToDayYYYYMMDD().equals(s);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return true;
    }
    /**
     * @param
     * @param msg
     * @return boolean
     * @author FeianLing
     * @date 2019/8/20
     * @desc 判断邮件是否是新的邮件
     */
    private boolean isNew(Message msg) throws MessagingException {
        boolean isNewFlag = false;
        Flags flags = msg.getFlags();
        Flags.Flag[] flagsArr = flags.getSystemFlags();
        log.info("邮件状态:" + JsonUtils.writeValue(flagsArr));
        for (Flags.Flag flag : flagsArr) {
            if (flag == Flags.Flag.SEEN) {
                isNewFlag = true;
                log.info("当前邮件为未读状态!");
                break;
            }
        }
        return isNewFlag;
    }
    /**
     * @param
     * @param msg
     * @return boolean
     * @author FeianLing
     * @date 2019/8/20
     * @desc 检查邮件内容是否需要我们处理
     * 1. 检查发件人是否满足要求
     * 2. 检查是否包含附件
     * 3. 检查是否有满足条件的附件
     */
    private boolean needProcessEmail(Message msg) throws Exception {
        log.info("needProcessEmail  > 当前邮件的标题:{}", msg.getSubject());
        // 1. 检查发件人邮箱是否包含在我们监控的邮箱列表里面
        String from = getFrom(msg);
        if (!monitoringEmail.contains(from)) {
            log.info("当前邮件的发件人[{}]不是我们要监控的对象", from);
            return false;
        }
        if (!isContainAttach((Part) msg)) {
            log.info("发件人满足要求但是附件为空,不满足我们监控的需求!");
            return false;
        }
        Map<String, InputStream> fileMap = new HashMap<>();
        getFileInputStream(msg, fileMap);
        if (fileMap.isEmpty()) {
            log.info("尽管邮件中有附件但是邮件中的附件却无一个满足要求!");
            return false;
        } else {
            //写入本地文件
            for (String s : fileMap.keySet()) {
                InputStream inputStream = fileMap.get(s);
                this.getFile(inputStream, attachFilePath + "/" + s);
                this.dealFileData(attachFilePath + "/" + s);
                break;
            }
        }
        return true;
    }
    /**
     * @param
     * @param part
     * @return java.io.InputStream
     * @author FeianLing
     * @date 2019/8/20
     * @desc 获取文件输入流
     */
    private void getFileInputStream(Part part, Map<String, InputStream> inputStreamMap) throws Exception {
        String fileName;
        if (part.isMimeType("multipart/*")) {
            Multipart mp = (Multipart) part.getContent();
            for (int i = 0; i < mp.getCount(); i++) {
                BodyPart mPart = mp.getBodyPart(i);
                String disposition = mPart.getDisposition();
                if ((disposition != null)
                        && ((disposition.equals(Part.ATTACHMENT)) || (disposition
                        .equals(Part.INLINE)))) {
                    fileName = mPart.getFileName();
                    fileName = MimeUtility.decodeText(fileName);
                    if (checkFileName(fileName)) {
                        inputStreamMap.put(fileName, mPart.getInputStream());
                    }
                } else if (mPart.isMimeType("multipart/*")) {
                    log.info("子邮件里面的附件");
                    getFileInputStream(mPart, inputStreamMap);
                } else {
                    fileName = mPart.getFileName();
                    if ((fileName != null)
                            && (fileName.toLowerCase().indexOf("GB2312") != -1)) {
                        fileName = MimeUtility.decodeText(fileName);
                        if (checkFileName(fileName)) {
                            inputStreamMap.put(fileName, mPart.getInputStream());
                        }
                    }
                }
            }
        } else if (part.isMimeType("message/rfc822")) {
            getFileInputStream((Part) part.getContent(), inputStreamMap);
        }
    }
    /**
     * @param
     * @param fileName
     * @return boolean
     * @author FeianLing
     * @date 2019/8/20
     * @desc 检查文件名称是否符合要求 AI智能语音催收名单——智清厂商-0803.xls
     */
    private boolean checkFileName(String fileName) {
        return (fileName.contains("AI外呼剔除名单") || fileName.contains("AI外呼提出名单"));
    }
    /**
     * @param
     * @param part
     * @return boolean
     * @author FeianLing
     * @date 2019/8/20
     * @desc 判断邮件是否包含附件,如果没有包含附件,返回false 反之返回true
     */
    private boolean isContainAttach(Part part) throws Exception {
        boolean attachFlag = false;
        // String contentType = part.getContentType();
        if (part.isMimeType("multipart/*")) {
            Multipart mp = (Multipart) part.getContent();
            for (int i = 0; i < mp.getCount(); i++) {
                BodyPart mPart = mp.getBodyPart(i);
                String disposition = mPart.getDisposition();
                if ((disposition != null)
                        && ((disposition.equals(Part.ATTACHMENT)) || (disposition
                        .equals(Part.INLINE)))) {
                    attachFlag = true;
                } else if (mPart.isMimeType("multipart/*")) {
                    attachFlag = isContainAttach((Part) mPart);
                } else {
                    String conType = mPart.getContentType();
                    if (conType.toLowerCase().indexOf("application") != -1) {
                        attachFlag = true;
                    }
                    if (conType.toLowerCase().indexOf("name") != -1) {
                        attachFlag = true;
                    }
                }
            }
        } else if (part.isMimeType("message/rfc822")) {
            attachFlag = isContainAttach((Part) part.getContent());
        }
        return attachFlag;
    }
    /**
     * @param
     * @param msg
     * @return java.lang.String
     * @author FeianLing
     * @date 2019/8/20
     * @desc 获取发送地址
     */
    private String getFrom(Message msg) throws MessagingException {
        String from = "";
        InternetAddress[] addresses = (InternetAddress[]) msg.getFrom();
        if (null == addresses || addresses.length == 0) {
            log.error("无法获取发送人地址信息!");
            return from;
        }
        Address address = addresses[0];
        log.info("发件人地址json:" + JsonUtils.writeValue(address));
        String form = ((InternetAddress) address).getAddress();
        return form;
    }
    public void getFile(InputStream is, String fileName) {
        try {
            BufferedInputStream in = null;
            BufferedOutputStream out = null;
            in = new BufferedInputStream(is);
            out = new BufferedOutputStream(new FileOutputStream(fileName));
            int len = -1;
            byte[] b = new byte[1024];
            while ((len = in.read(b)) != -1) {
                out.write(b, 0, len);
            }
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //业务处理方法
    public void dealFileData(String filePath) {
        try {
            log.info("开始处理文件:" + filePath);
            File file = new File(filePath);
            String name = file.getName();
            String fileName = name.replace(".zip", ".xls");
            ZipUtils.unZip(file, attachFilePath, "111111");
            RobotInfo robotInfoById = robotInfoService.getRobotInfoById(robotId);
            String tempFilePath = attachFilePath + "/" + fileName;
            log.info("处理文件路径:" + tempFilePath);
            gzOutUrgeFileService.autoOutUrge(robotInfoById, tempFilePath);
            log.info("出催文件处理完毕");
        } catch (ZipException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
    }
}
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
目录
相关文章
|
2月前
|
监控 Java API
Java语言按文件创建日期排序及获取最新文件的技术
这段代码实现了文件创建时间的读取、文件列表的获取与排序以及获取最新文件的需求。它具备良好的效率和可读性,对于绝大多数处理文件属性相关的需求来说足够健壮。在实际应用中,根据具体情况,可能还需要进一步处理如访问权限不足、文件系统不支持某些属性等边界情况。
163 14
|
2月前
|
存储 Java 编译器
深入理解Java虚拟机--类文件结构
本内容介绍了Java虚拟机与Class文件的关系及其内部结构。Class文件是一种与语言无关的二进制格式,包含JVM指令集、符号表等信息。无论使用何种语言,只要能生成符合规范的Class文件,即可在JVM上运行。文章详细解析了Class文件的组成,包括魔数、版本号、常量池、访问标志、类索引、字段表、方法表和属性表等,并说明其在Java编译与运行过程中的作用。
|
2月前
|
存储 人工智能 Java
java之通过Http下载文件
本文介绍了使用Java实现通过文件链接下载文件到本地的方法,主要涉及URL、HttpURLConnection及输入输出流的操作。
128 0
|
3月前
|
存储 Java 数据安全/隐私保护
Java技术栈揭秘:Base64加密和解密文件的实战案例
以上就是我们今天关于Java实现Base64编码和解码的实战案例介绍。希望能对你有所帮助。还有更多知识等待你去探索和学习,让我们一同努力,继续前行!
294 5
|
10月前
|
Java
java小工具util系列5:java文件相关操作工具,包括读取服务器路径下文件,删除文件及子文件,删除文件夹等方法
java小工具util系列5:java文件相关操作工具,包括读取服务器路径下文件,删除文件及子文件,删除文件夹等方法
204 9
|
3月前
|
网络协议 安全 Java
实现Java语言的文件断点续传功能的技术方案。
像这样,我们就完成了一项看似高科技、实则亲民的小工程。这样的技术实现不仅具备实用性,也能在面对网络不稳定的挑战时,稳稳地、不失乐趣地完成工作。
209 0
|
10月前
|
监控 Java 应用服务中间件
高级java面试---spring.factories文件的解析源码API机制
【11月更文挑战第20天】Spring Boot是一个用于快速构建基于Spring框架的应用程序的开源框架。它通过自动配置、起步依赖和内嵌服务器等特性,极大地简化了Spring应用的开发和部署过程。本文将深入探讨Spring Boot的背景历史、业务场景、功能点以及底层原理,并通过Java代码手写模拟Spring Boot的启动过程,特别是spring.factories文件的解析源码API机制。
255 2
|
9月前
|
人工智能 自然语言处理 Java
FastExcel:开源的 JAVA 解析 Excel 工具,集成 AI 通过自然语言处理 Excel 文件,完全兼容 EasyExcel
FastExcel 是一款基于 Java 的高性能 Excel 处理工具,专注于优化大规模数据处理,提供简洁易用的 API 和流式操作能力,支持从 EasyExcel 无缝迁移。
1858 65
FastExcel:开源的 JAVA 解析 Excel 工具,集成 AI 通过自然语言处理 Excel 文件,完全兼容 EasyExcel
|
6月前
|
前端开发 Cloud Native Java
Java||Springboot读取本地目录的文件和文件结构,读取服务器文档目录数据供前端渲染的API实现
博客不应该只有代码和解决方案,重点应该在于给出解决方案的同时分享思维模式,只有思维才能可持续地解决问题,只有思维才是真正值得学习和分享的核心要素。如果这篇博客能给您带来一点帮助,麻烦您点个赞支持一下,还可以收藏起来以备不时之需,有疑问和错误欢迎在评论区指出~
Java||Springboot读取本地目录的文件和文件结构,读取服务器文档目录数据供前端渲染的API实现
|
7月前
|
存储 算法 Java
解锁“分享文件”高效密码:探秘 Java 二叉搜索树算法
在信息爆炸的时代,文件分享至关重要。二叉搜索树(BST)以其高效的查找性能,为文件分享优化提供了新路径。本文聚焦Java环境下BST的应用,介绍其基础结构、实现示例及进阶优化。BST通过有序节点快速定位文件,结合自平衡树、多线程和权限管理,大幅提升文件分享效率与安全性。代码示例展示了文件插入与查找的基本操作,适用于大规模并发场景,确保分享过程流畅高效。掌握BST算法,助力文件分享创新发展。

热门文章

最新文章