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);
}
}
}
}
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);
}
}
}
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]);
}
}
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();
}
读出的邮件顺序是倒叙,如果不加处理,显示的从原来到现在顺序排列的邮件,所以要在循环条件处判断 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);
}