• java-使用jacob实现通过邮件发送指令执行各种功能0.2.0


    package sample.main.mailsearch;
    
    import com.jacob.activeX.ActiveXComponent;
    import com.jacob.com.Dispatch;
    import com.jacob.com.Variant;
    import com.mashape.unirest.http.HttpResponse;
    import sample.main.clockin.clockin;
    
    import java.io.IOException;
    import java.lang.management.ManagementFactory;
    import java.time.LocalDateTime;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.Enumeration;
    import java.util.List;
    
    /**
     * 2024年2月21日14:50:26
     */
    public class maiCommand {
        public  static List<String> doList=new ArrayList<>();
        public static int count=0;
        public static void main(String[] args) {
    
            new Thread(new Runnable() {
                @Override
                public void run() {
    
                    System.out.println(LocalDateTime.now()+" start clean more mail");
                    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 senderName = Dispatch.get(sMail, "SenderName");
                        if(senderName.toString().contains("more")){
                            Dispatch.call(sMail, "Delete");
                            System.out.println(LocalDateTime.now()+" delete more mail "+vSubj);
                        }
                    }
                    System.out.println(LocalDateTime.now()+" clean more mail end");
    
    
                    while (true) {
                        if(doList.size()>300){
                            doList.clear();
                        }
    
                        try {
                            Thread.sleep(3000);
                            excuteSpecifyFolder();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
                        count++;
                        if(count%100==0){
                            System.out.println(LocalDateTime.now()+" running...");
                            count=0;
                        }
                    }
                }
            }).start();
        }
    
        public static void excuteSpecifyFolder() {
            ActiveXComponent outlook = new ActiveXComponent("Outlook.Application");
            Dispatch myNamespace = Dispatch.call(outlook, "GetNamespace", "MAPI").toDispatch();
    
            //指定搜索特定的文件
            Dispatch allFolders = Dispatch.call(myNamespace, "Folders", "likexxx@ccc.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-5; x--) {
    
                Dispatch sMail = Dispatch.call(items, "Item", new Integer(x)).toDispatch();
                Variant vSubj = Dispatch.get(sMail, "Subject");
                Variant receivedTime = Dispatch.get(sMail, "ReceivedTime");
                Variant senderName = Dispatch.get(sMail, "SenderName");
                String subj = vSubj.getString();
    
                if (subj.equals("1234") && senderName.getString().equals("more")) {
                    if (!doList.contains(receivedTime.toString())) {
                        System.out.println("shutdown");
                        doList.add(receivedTime.toString());
                        shutdown();
                    } else {
                        if (count % 5 == 0) {
                            System.out.println(LocalDateTime.now() + " found 1234,but already do");
                        }
                    }
                } else if (subj.equals("5678") && senderName.getString().equals("more")) {
                    if (!doList.contains(receivedTime.toString())) {
                        System.out.println("sin");
                        sin();
                        doList.add(receivedTime.toString());
                    } else {
                        if (count % 5 == 0) {
                            System.out.println(LocalDateTime.now() + " found 5678,but already do");
                        }
                    }
                } else if (subj.equals("9101112") && senderName.getString().equals("more")) {
                    if (!doList.contains(receivedTime.toString())) {
                        System.out.println("killAllJava");
                        killAllJava();
                    } else {
                        if (count % 5 == 0) {
                            System.out.println(LocalDateTime.now() + " found 9101112,but already do");
                        }
                    }
                }
    
            }
        }
    
    
        public static void excute() {
            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 = count - 5; x <=count; x++) {
                Dispatch sMail = Dispatch.call(items, "Item", new Integer(x)).toDispatch();
                Variant vSubj = Dispatch.get(sMail, "Subject");
                Variant receivedTime = Dispatch.get(sMail, "ReceivedTime");
                Variant senderName = Dispatch.get(sMail, "SenderName");
                String subj = vSubj.getString();
                System.out.println(subj);
    
                if (subj.equals("1234") &&  senderName.getString().equals("more")) {
                    if(!doList.contains(receivedTime.toString())){
                        System.out.println("shutdown");
                        shutdown();
                        doList.add(receivedTime.toString());
                    }else{
                        if(count%5==0){
                            System.out.println(LocalDateTime.now()+" found 1234,but already do");
                        }
                    }
                }else if (subj.equals("5678") &&  senderName.getString().equals("more")) {
                    if(!doList.contains(receivedTime.toString())){
                        System.out.println("sin");
                        sin();
                        doList.add(receivedTime.toString());
                    }else{
                        if(count%5==0){
                            System.out.println(LocalDateTime.now()+" found 5678,but already do");
                        }
                    }
                }else if (subj.equals("9101112") &&  senderName.getString().equals("more")) {
                    if(!doList.contains(receivedTime.toString())){
                        System.out.println("killAllJava");
                        doList.add(receivedTime.toString());
                        killAllJava();
                    }else{
                        if(count%5==0){
                            System.out.println(LocalDateTime.now()+" found 9101112,but already do");
                        }
                    }
                }
            }
        }
    
        public static void shutdown() {
            System.out.println(LocalDateTime.now()+" shutdown "+doList.toString());
            try {
                sendBack("ok","1234");
                Runtime.getRuntime().exec("shutdown -s -t 0");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static void sin() {
            System.out.println(LocalDateTime.now()+" sin "+doList.toString());
            try {
                Thread.sleep(5000);
                HttpResponse<String> re = clockin.signin();
                if(re.getBody().contains("success")){
                    sendBack("sdfsdfoook","5678");
                }else{
                    sendBack("nnnoosdttfsdfoook","5678");
                }
    
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        public static void killAllJava(){
            System.out.println(LocalDateTime.now()+" killAllJava "+doList.toString());
            try {
                // 获取当前Java进程ID
                String pid = ManagementFactory.getRuntimeMXBean().getName();
                // 构建kill命令并执行
                Process process = Runtime.getRuntime().exec("cmd /c taskkill /F /IM java.exe");
                sendBack("即将结束所有Java进程","9101112");
                int exitCode = process.waitFor();
                String doResult="";
                if (exitCode == 1) {
                    doResult="已成功结束所有Java进程!";
    
                } else {
                    doResult="无法结束Java进程";
                }
    
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        public static void sendBack(String content,String command){
            ActiveXComponent axOutlook = new ActiveXComponent("Outlook.Application");
    
            Dispatch mailItem = Dispatch.call(axOutlook, "CreateItem", 0).getDispatch();
            Dispatch inspector = Dispatch.get(mailItem, "GetInspector").getDispatch();
            Dispatch recipients = Dispatch.call(mailItem, "Recipients").getDispatch();
            Dispatch.call(recipients, "Add", "12738022@qq.com");
            Dispatch.put(mailItem, "Subject", command);
            String body = content;
            Dispatch.put(mailItem, "HTMLBody", "
    " + body + "
    "
    ); // Dispatch.call(mailItem, "Display"); Dispatch.call(mailItem, "Send"); System.gc(); } }
    • 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
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
  • 相关阅读:
    前端开发工具有哪些?17款前端工程师必备工具推荐!
    HCNP Routing&Switching之组播技术-组播路由协议PIM
    Java知识记录
    外包干了5天,技术明显退步
    Rust 流程控制
    Hololens2代码控制手部网格、手部关节、手部射线、性能面板的显示状态
    linux /proc进程文件目录介绍
    C#8.0本质论第十章--合式类型
    Android红外遥控ConsumerIrManager
    C# Winform串口助手
  • 原文地址:https://blog.csdn.net/m0_60688978/article/details/136214325