http://uule.iteye.com/blog/1195597
Java代碼 ?
package?test; ??import?java.io.BufferedReader; ????public?class?TestMail?{ ????????public?static?void?main(String[]?args)?throws?Exception?{ ?????????????????????? ???????????????????? ?????????????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"?,?"crycbj@163.com");?? ??????????????????????????Dispatch.put(mailItem,?"Subject",?"Test5!");?? ?????????????Dispatch.put(mailItem,?"CC",?"1@qq.com");?? ??????????????????????????????????????? ?????????????String?body?=?"<html><body><div?style='color:red;'>This?is?a?Test?!</div></body></html>"; ?????????????????????????? ???????????????? ??????????????? ?????????????BufferedReader?bf?=?new?BufferedReader(new?FileReader(new?File("F:/Ro.htm"))); ?????????????String?html?=?""; ?????????????String?str?=?""; ?????????????while(?(str=bf.readLine())?!=?null){ ?????????????????html?+=?str; ?????????????} ?????????????Dispatch.put(mailItem,?"HTMLBody",?html);? ????????????? ???????????????????????????????????????????????????? ??????????????????????????????????????? ?????????????????????????? ??????????????????????????????????????????????????? ????????????????????????????????????????????? ?????????????Dispatch.call(mailItem,?"Display");?? ?????????????Dispatch.call(mailItem,?"Send");?? ????????????? ????????????? ??????} ??}??
package test;
import java.io.BufferedReader;public class TestMail {public static void main(String[] args) throws Exception {/*String body = "<div style='color:red;'>This is a Test !</div>";Runtime.getRuntime().exec("D:\\Program Files\\Microsoft Office\\Office14\\OUTLOOK.EXE mailto:KO@techson.com.hk?subject=Chinese New Year backup arrangement" +"&cc=Ko@techson.com.hk&body="+body);*///System.out.println(System.getProperty("java.library.path")); ActiveXComponent axOutlook = new ActiveXComponent("Outlook.Application"); //Dispatch ds = axOutlook.getObject();/* Dispatch namespace = Dispatch.call(axOutlook, "GetNamespace", "MAPI").toDispatch() ;String path = new File("F:/test.htm").getAbsolutePath(); Dispatch mailItem = Dispatch.call(namespace, "OpenSharedItem", path).toDispatch(); */Dispatch mailItem = Dispatch.call(axOutlook, "CreateItem", 0).getDispatch(); //Dispatch mailItem = Dispatch.call(axOutlook, "Open", "F:/test.htm").toDispatch();// Dispatch body = Dispatch.call(mailItem, "Body").getDispatch();; // Dispatch.call(body, "Copy");Dispatch inspector = Dispatch.get(mailItem, "GetInspector").getDispatch();//收件人 Dispatch recipients = Dispatch.call(mailItem, "Recipients").getDispatch(); Dispatch.call(recipients, "Add" , "crycbj@163.com"); //郵件主題 Dispatch.put(mailItem, "Subject", "Test5!"); Dispatch.put(mailItem, "CC", "1@qq.com"); //Dispatch.put(mailItem, "CC", "cry@163.com"); //Dispatch.put(mailItem, "ReadReceiptRequested", "false"); String body = "<html><body><div style='color:red;'>This is a Test !</div></body></html>";//Dispatch.put(mailItem, "HTMLBody", body); // File file = new File("C:/test.htm");
// FileOutputStream f = new FileOutputStream("F:/test.htm");
// f.write(body.getBytes());
// f.close();BufferedReader bf = new BufferedReader(new FileReader(new File("F:/Ro.htm")));String html = "";String str = "";while( (str=bf.readLine()) != null){html += str;}Dispatch.put(mailItem, "HTMLBody", html); //Dispatch bodyDis = Dispatch.get(mailItem, "Body").toDispatch(); //Dispatch.call(bodyDis, "Add" , "F:/test.htm"); //Dispatch.put(mailItem, "Body", new Variant("F:/test.htm")); //Dispatch.put(mailItem, "Body", body); //Dispatch.call(mailItem, "InsertAfter",body );//插入一個段落//Dispatch selection = Dispatch.get(axOutlook, "Selection").toDispatch();//Dispatch inHtmleditor = Dispatch.call(inspector, "HTMLEditor").getDispatch();// Dispatch.call(inHtmleditor, "Paste").getDispatch(); //Dispatch.invoke(inHtmleditor, "Paste", Dispatch.Method, new Object[]{false}, new Variant(false));//附件
// Dispatch attachments = Dispatch.call(mailItem, "Attachments").getDispatch();
// Dispatch.call(attachments, "Add" , "D:\\20110127.txt");
// Dispatch.call(attachments, "Add" , "D:\\20110128.txt"); //顯示新郵件窗口 Dispatch.call(mailItem, "Display"); Dispatch.call(mailItem, "Send"); }
}
?調用Word:
http://www.360doc.com/content/07/0330/15/23378_421609.shtml
http://www.360doc.com/content/11/0130/19/3947093_90007570.shtml
http://www.th7.cn/Program/java/2011/10/12/43708.shtml
?
基本語法:
http://hi.baidu.com/meng_fy/blog/item/d7186116a409ae17972b43df.html
http://java.chinaitlab.com/tools/769980.html
http://www.ibm.com/developerworks/cn/java/j-lo-jacob/
?
Java代碼 ?
package?test; ????import?java.io.File; ????public?class?TestMail2?{ ????????????????public?static?void?main(String[]?args)?{ ??????????ActiveXComponent?xl?=?new?ActiveXComponent("Outlook.Application"); ????????????Dispatch?explorer?=?Dispatch.get(xl,"ActiveExplorer").toDispatch(); ??????????Dispatch?selection?=?Dispatch.get(explorer,?"Selection").toDispatch(); ??????????Variant?count?=?Dispatch.get(selection,?"Count"); ????????????for?(int?mailIndex?=?1;?mailIndex?<=?count.toInt();?mailIndex++?)?{ ??????????????Dispatch?mailItem?=?Dispatch.call(selection,?"Item",?new?Variant(mailIndex)).toDispatch(); ????????????????Variant?senderName?=?Dispatch.get(mailItem,?"SenderName"); ??????????????Variant?subject?=?Dispatch.get(mailItem,?"Subject"); ??????????????Variant?body?=?Dispatch.get(mailItem,?"HTMLBody"); ?????? ??????????????String?emailFileName?=?subject.toString()?+".txt";?? ?????????????? ??????????????try?{ ??????????????????File?email?=?new?File(emailFileName); ??????????????????PrintWriter?writer?=?new?PrintWriter(?new?FileWriter(email)?); ??????????????????writer.println("From:?"+?senderName?); ??????????????????writer.println("Subject:?"+?subject); ??????????????????writer.println(""); ??????????????????writer.print(?body?); ??????????????????writer.close(); ??????????????} ??????????????catch?(IOException?e)?{ ??????????????????System.out.println("IOException?writing?e-mail?with?subject:?'"+?subject?+"'"); ??????????????????continue; ??????????????} ????????????????Dispatch?attachments?=?Dispatch.get(mailItem,?"Attachments").toDispatch(); ??????????????Variant?attachmentCount?=?Dispatch.get(attachments,?"Count"); ????????????????if?(?attachmentCount.toInt()?>?0?)?{ ??????????????????for(?int?attachmentIndex?=?1;?attachmentIndex<=attachmentCount.toInt();?attachmentIndex++?)?{ ??????????????????????Dispatch?attachment?=?Dispatch.call(attachments,?"Item",?new?Variant(attachmentIndex)).toDispatch(); ??????????????????????Variant?fileNameVariant?=?Dispatch.get(attachment,?"FileName"); ??????????????????????String?fileName?=?fileNameVariant.toString(); ?????????????? ??????????????????????Variant?saveResult?=?Dispatch.call(attachment,?"SaveAsFile",?"F:\\"+fileName); ??????????????????} ??????????????} ?????????? ??????????} ??????} ????}??
package test;import java.io.File;public class TestMail2 {/*** @param args*/public static void main(String[] args) {ActiveXComponent xl = new ActiveXComponent("Outlook.Application");Dispatch explorer = Dispatch.get(xl,"ActiveExplorer").toDispatch();Dispatch selection = Dispatch.get(explorer, "Selection").toDispatch();Variant count = Dispatch.get(selection, "Count");for (int mailIndex = 1; mailIndex <= count.toInt(); mailIndex++ ) {Dispatch mailItem = Dispatch.call(selection, "Item", new Variant(mailIndex)).toDispatch();Variant senderName = Dispatch.get(mailItem, "SenderName");Variant subject = Dispatch.get(mailItem, "Subject");Variant body = Dispatch.get(mailItem, "HTMLBody");String emailFileName = subject.toString() +".txt"; try {File email = new File(emailFileName);PrintWriter writer = new PrintWriter( new FileWriter(email) );writer.println("From: "+ senderName );writer.println("Subject: "+ subject);writer.println("");writer.print( body );writer.close();}catch (IOException e) {System.out.println("IOException writing e-mail with subject: '"+ subject +"'");continue;}Dispatch attachments = Dispatch.get(mailItem, "Attachments").toDispatch();Variant attachmentCount = Dispatch.get(attachments, "Count");if ( attachmentCount.toInt() > 0 ) {for( int attachmentIndex = 1; attachmentIndex<=attachmentCount.toInt(); attachmentIndex++ ) {Dispatch attachment = Dispatch.call(attachments, "Item", new Variant(attachmentIndex)).toDispatch();Variant fileNameVariant = Dispatch.get(attachment, "FileName");String fileName = fileNameVariant.toString();Variant saveResult = Dispatch.call(attachment, "SaveAsFile", "F:\\"+fileName);}}}}}
?
Java代碼 ?
package?test; ????import?com.jacob.activeX.ActiveXComponent; ??import?com.jacob.com.Dispatch; ????public?class?TestMail3?{ ????????????????public?static?void?main(String[]?args)?{ ??????????final?int?olFolderContacts?=?10; ????????????ActiveXComponent?ol?=?new?ActiveXComponent("Outlook.Application"); ?????????? ??????????Dispatch?olo?=?ol.getObject(); ??????????Dispatch?myNamespace?=?Dispatch.call(olo,?"GetNamespace","MAPI").toDispatch(); ??????????Dispatch?myFolder?=?Dispatch.call(myNamespace,?"GetDefaultFolder",new?Integer(olFolderContacts)).toDispatch(); ???????????? ????????????Dispatch?items?=?Dispatch.get(myFolder,?"Items").toDispatch(); ????????????int?count?=?Dispatch.call(items,?"Count").toInt(); ????????????for?(int?i?=?1;?i?<=?count;?i++)?{ ??????????????Dispatch?item; ??????????????item?=?Dispatch.call(items,?"Item",?new?Integer(i)).toDispatch(); ?????? ??????????????String?fullName?=?Dispatch.get(item,?"Fullname").toString(); ??????????????String?emailAddress?=?Dispatch.get(item,?"Email1Address").toString(); ??????????????String?addressStreet?=?Dispatch.get(item,?"BusinessAddressStreet").toString(); ??????????????String?addressCity?=?Dispatch.get(item,?"BusinessAddressCity").toString(); ??????????????String?addressCountry?=?Dispatch.get(item,?"BusinessAddressCountry").toString(); ??????????????String?addressPostalCode?=?Dispatch.get(item,"BusinessAddressPostalCode").toString(); ??????????????String?addressState?=?Dispatch.get(item,?"BusinessAddressState").toString(); ??????????????String?title?=?Dispatch.get(item,?"Title").toString(); ??????????????String?phone?=?Dispatch.get(item,?"BusinessTelephoneNumber").toString(); ??????????????String?fax?=?Dispatch.get(item,?"BusinessFaxNumber").toString(); ?????? ??????????????printField("",?fullName); ??????????????printField("",?title); ??????????????printField("",?addressStreet); ??????????????printField("",?addressCity); ??????????????printField("",?addressState); ??????????????printField("",?addressPostalCode); ??????????????printField("",?addressCountry); ??????????????printField("Tel:",?phone); ??????????????printField("Fax:",?fax); ??????????????printField("Email:",?emailAddress); ?????? ??????????????System.out.println(); ??????????} ??????} ????????????public?static?void?printField(String?label,?String?value)?{ ??????????????if?(value.length()?!=?0)?{ ??????????????????System.out.println(label?+?"?"?+?value); ??????????????} ??????????} ????}??
package test;import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;public class TestMail3 {/*** @param args*/public static void main(String[] args) {final int olFolderContacts = 10;ActiveXComponent ol = new ActiveXComponent("Outlook.Application");Dispatch olo = ol.getObject();Dispatch myNamespace = Dispatch.call(olo, "GetNamespace","MAPI").toDispatch();Dispatch myFolder = Dispatch.call(myNamespace, "GetDefaultFolder",new Integer(olFolderContacts)).toDispatch();//n?chste zeile FehlermeldungDispatch items = Dispatch.get(myFolder, "Items").toDispatch();int count = Dispatch.call(items, "Count").toInt();for (int i = 1; i <= count; i++) {Dispatch item;item = Dispatch.call(items, "Item", new Integer(i)).toDispatch();String fullName = Dispatch.get(item, "Fullname").toString();String emailAddress = Dispatch.get(item, "Email1Address").toString();String addressStreet = Dispatch.get(item, "BusinessAddressStreet").toString();String addressCity = Dispatch.get(item, "BusinessAddressCity").toString();String addressCountry = Dispatch.get(item, "BusinessAddressCountry").toString();String addressPostalCode = Dispatch.get(item,"BusinessAddressPostalCode").toString();String addressState = Dispatch.get(item, "BusinessAddressState").toString();String title = Dispatch.get(item, "Title").toString();String phone = Dispatch.get(item, "BusinessTelephoneNumber").toString();String fax = Dispatch.get(item, "BusinessFaxNumber").toString();printField("", fullName);printField("", title);printField("", addressStreet);printField("", addressCity);printField("", addressState);printField("", addressPostalCode);printField("", addressCountry);printField("Tel:", phone);printField("Fax:", fax);printField("Email:", emailAddress);System.out.println();}}public static void printField(String label, String value) {if (value.length() != 0) {System.out.println(label + " " + value);}}}
?第二例地址:
http://www.velocityreviews.com/forums/t147294-java-libray-to-extract-email-contact-only-from-outlook-file.html
?
官網問題搜索:
http://sourceforge.net/projects/jacob-project/forums/forum/375946/topic/3734426/index/page/1
http://sourceforge.net/search/?group_id=109543&type_of_search=forums&group_forum_id=375946&words=outlook&search=Search
。。。
?
總結
以上是生活随笔為你收集整理的java调用outlook的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。