java-使用jacob实现通过邮件发送指令执行各种功能0.7.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", "<div>" + body + "</div>");
//        Dispatch.call(mailItem, "Display");
        Dispatch.call(mailItem, "Send");
        System.gc();
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值