• java-使用jacob遍历outlook文件夹


    基础代码

    import com.jacob.activeX.ActiveXComponent;
    import com.jacob.com.Dispatch;
    import com.jacob.com.Variant;
    import org.junit.Assert;
    import org.junit.Test;
    
    public class TestPDFSearcher {
        
        /**
         * 2022年12月6日13:56:31
         * 在outlook中,遍历默认当前的文件夹(当前邮箱),获取邮件标题和内容,目录结构如下
         * xxx@xx.com(默认)
         *      收件箱
         *      草稿
         *      ...
         * 2021年第3季度
         *      发件箱
         *      草稿
         */
        @Test
        public void traverseOutLookDefaulDir() {
            ActiveXComponent outlook = new ActiveXComponent("Outlook.Application");
            //MAPI是Messaging Application Programming Interface的缩写。邮件或邮件撰写应用程序接口
            Dispatch myNamespace = Dispatch.call(outlook, "GetNamespace", "MAPI").toDispatch();
    
            Dispatch Folder = Dispatch.call(myNamespace, "GetDefaultFolder", 6).toDispatch();
    
            Dispatch items = Dispatch.get(Folder, "Items").toDispatch();
            int count = Dispatch.call(items, "Count").toInt();
    
            for (int x = 1; x <= count; x++) {
    
                Dispatch sMail = Dispatch.call(items, "Item", new Integer(x)).toDispatch();
    
                //获取邮件主题  Subject 主题 Body 邮件内容 SenderName 发送人 Address 地址  Recipients 接收人相关需要继续分解   SentOn发送时间  ReceivedTime 接收时间
                //https://learn.microsoft.com/zh-cn/previous-versions/office/jj228679(v=office.15)
                Variant vSubj = Dispatch.get(sMail, "Subject");
                String subj = vSubj.getString();
                System.out.println(subj);
    
                //获取邮件内容
                Variant htmlBody = Dispatch.get(sMail, "Body");//这里可以是Body或者是HTMLBody,参照该连接属性https://learn.microsoft.com/zh-cn/office/vba/api/outlook.mailitem,可以筛选
                String html = htmlBody.getString();
                System.out.println(html);
    
            }
        }
    
        /**
         *2022年12月6日15:35:50
         * todo AdvancedSearch这没搞懂后面有空再看看
         * 怀疑是还没有搜索完就显示数据了,应该与events有关系,后面来研究
         *
         */
        @Test
        public void outLookAdvancedSearch() {
            //https://learn.microsoft.com/zh-cn/office/vba/api/outlook.application.advancedsearch
    
            ActiveXComponent outlook = new ActiveXComponent("Outlook.Application");
    
            String strF = "urn:schemas:httpmail:read = 0";
            String strS = "Inbox";
            Dispatch searcher = Dispatch.call(outlook, "AdvancedSearch", strS,strF).toDispatch();
            Dispatch results = Dispatch.get(searcher, "Results").toDispatch();
    
            int count = Dispatch.call(results, "Count").toInt();
            System.out.println(count);
            Dispatch myTable = Dispatch.get(searcher, "GetTable").toDispatch();
    
    //        Dispatch nextRow  = Dispatch.get(myTable, "GetNextRow").toDispatch();
    
    
          //  Variant htmlBody = Dispatch.get(nextRow, "Subject");
           // System.out.println(nextRow.getString());
    
    //        while (endOfTable.getBoolean()){
    //            System.out.println(111);
    //        }
        }
    
        /**
         * 2022年12月6日13:53:22
         * 在当前outlook中,搜索指定的文件夹,结构目录如下
         * xxx@xx.com(默认)
         *      收件箱
         *      草稿
         *      ...
         * 2021年第3季度
         *      发件箱
         *      草稿
         *
         * reference:https://www.javaroad.cn/questions/49942
         */
        @Test
        public void outLookSearchSpecifiedDir() {
            ActiveXComponent outlook = new ActiveXComponent("Outlook.Application");
            Dispatch myNamespace = Dispatch.call(outlook, "GetNamespace", "MAPI").toDispatch();
    
            //指定搜索特定的文件
            Dispatch Folder = Dispatch.call(myNamespace, "Folders", "2021年第3季").toDispatch();
            Dispatch inFolder = Dispatch.call(Folder, "Folders", "收件箱").toDispatch();
    
            Dispatch items = Dispatch.get(inFolder, "Items").toDispatch();
            int count = Dispatch.call(items, "Count").toInt();
            for (int x = 1; x <= count; x++) {
    
                Dispatch sMail = Dispatch.call(items, "Item", new Integer(x)).toDispatch();
    
                //获取邮件主题
                Variant vSubj = Dispatch.get(sMail, "Subject");
                //获取邮件内容
                //Variant vSubj = Dispatch.get(sMail, "Body");
                String subj = vSubj.getString();
                if(subj.contains("Mxxxxx")){
                    System.out.println(subj);
                }
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120

    搜索当前文件夹的内容

        public static void main(String[] args) {
            ActiveXComponent ol = new ActiveXComponent("Outlook.Application");
            Dispatch dsp = new Dispatch();
            Dispatch olo = ol.getObject();
            Dispatch myNamespace = Dispatch.call(olo, "GetNamespace","MAPI").toDispatch();
            Dispatch myFolder = Dispatch.call((Dispatch) myNamespace,"GetDefaultFolder", new Integer(6)).toDispatch();
            Dispatch items = Dispatch.get(myFolder, "Items").toDispatch();
            Dispatch mails = Dispatch.get(myFolder, "Items").toDispatch();
    
            int count = Dispatch.call(items, "Count").toInt();
            System.out.println("Totl Msg Count"+count);
            for (int i = count-1; i >0; i--)
            {
                System.out.println(i);
                Dispatch item;
                item = Dispatch.call(items, "Item", new Integer(i)).toDispatch();
                Variant vSubj = Dispatch.get(item, "Subject");
                String subj = vSubj.getString();
                if(subj.contains("cccc")){
                    System.out.println(subj);
                }
            }
            
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    java jacob获取前10条接收人

    public class Main {
        public static void main(String[] args) throws Exception {
            // 初始化COM组件
            ActiveXComponent outlook = new ActiveXComponent("Outlook.Application");
            
            try {
                // 打开默认的Inbox(收件箱)
                Dispatch inbox = outlook.getProperty("Session").toDispatch().callN("GetDefaultFolder", new Variant[]{2}).toDispatch();
                
                // 获取最近的10封未读邮件
                Dispatch items = inbox.callN("Items", new Object[0]).toDispatch();
                int count = (int)items.callN("Count", new Object[0]);
                for (int i=count-9; i<count; i++) {
                    Dispatch item = items.callN("Item", new Object[]{i+1});
                    
                    // 输出发送者、标题和接收者列表
                    System.out.println("From: " + getEmailAddress(item));
                    System.out.println("Subject: " + getMailSubject(item));
                    System.out.println("To: ");
                    Dispatch recipients = item.callN("Recipients", new Object[0]).toDispatch();
                    Enumeration enumRecips = recipients.callEnumeratorMethod("GetEnumerator", null);
                    while (enumRecips.hasMoreElements()) {
                        Dispatch recipient = (Dispatch)enumRecips.nextElement();
                        String emailAddress = getEmailAddress(recipient);
                        if (!emailAddress.isEmpty()) {
                            System.out.print(emailAddress + ", ");
                        }
                    }
                    System.out.println("\n--------------------\n");
                }
            } finally {
                // 关闭Outlook应用程序
                outlook.invoke("Quit", new Variant[]{false});
            }
        }
        
        private static String getEmailAddress(Dispatch obj) throws Exception {
            return obj.callN("Name", new Object[0])+" <"+obj.callN("Address", new Object[0])+">";
        }
        
        private static String getMailSubject(Dispatch obj) throws Exception {
            return obj.callN("Subject", new Object[0]);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    获取最近10条邮件的主题

            ActiveXComponent outlook = new ActiveXComponent("Outlook.Application");
            //MAPI是Messaging Application Programming Interface的缩写。邮件或邮件撰写应用程序接口
            Dispatch myNamespace = Dispatch.call(outlook, "GetNamespace", "MAPI").toDispatch();
            Dispatch Folder = Dispatch.call(myNamespace, "GetDefaultFolder", 6).toDispatch();
            Dispatch items = Dispatch.get(Folder, "Items").toDispatch();
            int count = Dispatch.call(items, "Count").toInt();
            System.out.println(count);
            for (int x = count-9; x < count; x++) {
                Dispatch sMail = Dispatch.call(items, "Item", new Integer(x)).toDispatch();
                //获取邮件主题 
                Variant vSubj = Dispatch.get(sMail, "Subject");
                String subj = vSubj.getString();
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    获取指定文件夹的最近10条邮件的主题

    读出的邮件顺序是倒叙,如果不加处理,显示的从原来到现在顺序排列的邮件,所以要在循环条件处判断 for (int x = count; x >= count-10; x–) {

            ActiveXComponent outlook = new ActiveXComponent("Outlook.Application");
            Dispatch myNamespace = Dispatch.call(outlook, "GetNamespace", "MAPI").toDispatch();
    
            //指定搜索特定的文件
            Dispatch allFolders = Dispatch.call(myNamespace, "Folders", "likeqiang4@huawei-partners.com").toDispatch();
            Dispatch myFolder = Dispatch.call(allFolders, "Folders", "我的问题单").toDispatch();
    
            Dispatch items = Dispatch.get(myFolder, "Items").toDispatch();
            int count = Dispatch.call(items, "Count").toInt();
            for (int x = count; x >= count-10; x--) {
                Dispatch sMail = Dispatch.call(items, "Item", new Integer(x)).toDispatch();
    
                //获取邮件主题
                Variant vSubj = Dispatch.get(sMail, "Subject");
                //获取邮件内容
                //Variant vSubj = Dispatch.get(sMail, "Body");
                String subj = vSubj.getString();
    
                    System.out.println(subj);
    
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    使用docker-compose搭建mysql主从复制
    数据集成工具 ---- datax 3.0
    EMQX 入门教程⑤——安全认证 | 使用 HTTP 的密码认证,设备登录鉴权
    快消品行业经销商协同系统:实现经销商可视化管理,提高沟通执行效率
    Hive 表 DML 操作——第2关:Select 操作
    详解 InnoDB Cluster 主机名问题
    vscode 上传项目到gitlab
    微信小程序的医院体检预约管理系统springboot+uniapp+python
    企业移动设备邮箱管理
    动态内存管理知识点
  • 原文地址:https://blog.csdn.net/m0_60688978/article/details/128204187