java调用outlook

57 篇文章 0 订阅

http://uule.iteye.com/blog/1195597

Java代码 复制代码  收藏代码
  1. package test;   
  2. import java.io.BufferedReader;   
  3.   
  4. public class TestMail {   
  5.   
  6.     public static void main(String[] args) throws Exception {   
  7.         /*String body = "<div style='color:red;'>This is a Test !</div>";  
  8.         Runtime.getRuntime().exec("D:\\Program Files\\Microsoft Office\\Office14\\OUTLOOK.EXE  mailto:KO@techson.com.hk?subject=Chinese New Year backup arrangement" +  
  9.                 "&cc=Ko@techson.com.hk&body="+body);*/  
  10.            
  11.         //System.out.println(System.getProperty("java.library.path"));     
  12.            
  13.            ActiveXComponent axOutlook = new ActiveXComponent("Outlook.Application");     
  14.            //Dispatch ds = axOutlook.getObject();   
  15.           /* Dispatch namespace = Dispatch.call(axOutlook, "GetNamespace", "MAPI").toDispatch() ;  
  16.            String path = new File("F:/test.htm").getAbsolutePath();   
  17.            Dispatch mailItem = Dispatch.call(namespace, "OpenSharedItem", path).toDispatch(); */  
  18.               
  19.               
  20.            Dispatch mailItem = Dispatch.call(axOutlook, "CreateItem"0).getDispatch();     
  21.               
  22.            //Dispatch mailItem = Dispatch.call(axOutlook, "Open", "F:/test.htm").toDispatch();   
  23.           // Dispatch body = Dispatch.call(mailItem, "Body").getDispatch();;     
  24.           // Dispatch.call(body, "Copy");   
  25.               
  26.            Dispatch inspector = Dispatch.get(mailItem, "GetInspector").getDispatch();   
  27.   
  28.            //收件人     
  29.            Dispatch recipients = Dispatch.call(mailItem, "Recipients").getDispatch();     
  30.            Dispatch.call(recipients, "Add" , "crycbj@163.com");     
  31.            //邮件主题     
  32.            Dispatch.put(mailItem, "Subject""Test5!");     
  33.            Dispatch.put(mailItem, "CC""1@qq.com");     
  34.            //Dispatch.put(mailItem, "CC", "cry@163.com");     
  35.            //Dispatch.put(mailItem, "ReadReceiptRequested", "false");    
  36.               
  37.            String body = "<html><body><div style='color:red;'>This is a Test !</div></body></html>";   
  38.            //Dispatch.put(mailItem, "HTMLBody", body);    
  39.               
  40.           // File file = new File("C:/test.htm");   
  41. //         FileOutputStream f = new FileOutputStream("F:/test.htm");   
  42. //         f.write(body.getBytes());   
  43. //         f.close();   
  44.               
  45.            BufferedReader bf = new BufferedReader(new FileReader(new File("F:/Ro.htm")));   
  46.            String html = "";   
  47.            String str = "";   
  48.            while( (str=bf.readLine()) != null){   
  49.                html += str;   
  50.            }   
  51.            Dispatch.put(mailItem, "HTMLBody", html);    
  52.               
  53.            //Dispatch bodyDis = Dispatch.get(mailItem, "Body").toDispatch();     
  54.            //Dispatch.call(bodyDis, "Add" , "F:/test.htm");     
  55.            //Dispatch.put(mailItem, "Body", new Variant("F:/test.htm"));     
  56.               
  57.            //Dispatch.put(mailItem, "Body", body);     
  58.            //Dispatch.call(mailItem, "InsertAfter",body );//插入一个段落   
  59.               
  60.            //Dispatch selection = Dispatch.get(axOutlook, "Selection").toDispatch();   
  61.               
  62.            //Dispatch inHtmleditor = Dispatch.call(inspector, "HTMLEditor").getDispatch();   
  63.           // Dispatch.call(inHtmleditor, "Paste").getDispatch();     
  64.            //Dispatch.invoke(inHtmleditor, "Paste", Dispatch.Method, new Object[]{false}, new Variant(false));   
  65.               
  66.            //附件     
  67. //             Dispatch attachments = Dispatch.call(mailItem, "Attachments").getDispatch();     
  68. //             Dispatch.call(attachments, "Add" , "D:\\20110127.txt");     
  69. //             Dispatch.call(attachments, "Add" , "D:\\20110128.txt");     
  70.            //显示新邮件窗口     
  71.               
  72.            Dispatch.call(mailItem, "Display");     
  73.            Dispatch.call(mailItem, "Send");     
  74.               
  75.               
  76.     }   
  77. }  
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代码 复制代码  收藏代码
  1. package test;   
  2.   
  3. import java.io.File;   
  4.   
  5. public class TestMail2 {   
  6.   
  7.     /**  
  8.      * @param args  
  9.      */  
  10.     public static void main(String[] args) {   
  11.         ActiveXComponent xl = new ActiveXComponent("Outlook.Application");   
  12.   
  13.         Dispatch explorer = Dispatch.get(xl,"ActiveExplorer").toDispatch();   
  14.         Dispatch selection = Dispatch.get(explorer, "Selection").toDispatch();   
  15.         Variant count = Dispatch.get(selection, "Count");   
  16.   
  17.         for (int mailIndex = 1; mailIndex <= count.toInt(); mailIndex++ ) {   
  18.             Dispatch mailItem = Dispatch.call(selection, "Item"new Variant(mailIndex)).toDispatch();   
  19.   
  20.             Variant senderName = Dispatch.get(mailItem, "SenderName");   
  21.             Variant subject = Dispatch.get(mailItem, "Subject");   
  22.             Variant body = Dispatch.get(mailItem, "HTMLBody");   
  23.        
  24.             String emailFileName = subject.toString() +".txt";     
  25.                
  26.             try {   
  27.                 File email = new File(emailFileName);   
  28.                 PrintWriter writer = new PrintWriter( new FileWriter(email) );   
  29.                 writer.println("From: "+ senderName );   
  30.                 writer.println("Subject: "+ subject);   
  31.                 writer.println("");   
  32.                 writer.print( body );   
  33.                 writer.close();   
  34.             }   
  35.             catch (IOException e) {   
  36.                 System.out.println("IOException writing e-mail with subject: '"+ subject +"'");   
  37.                 continue;   
  38.             }   
  39.   
  40.             Dispatch attachments = Dispatch.get(mailItem, "Attachments").toDispatch();   
  41.             Variant attachmentCount = Dispatch.get(attachments, "Count");   
  42.   
  43.             if ( attachmentCount.toInt() > 0 ) {   
  44.                 forint attachmentIndex = 1; attachmentIndex<=attachmentCount.toInt(); attachmentIndex++ ) {   
  45.                     Dispatch attachment = Dispatch.call(attachments, "Item"new Variant(attachmentIndex)).toDispatch();   
  46.                     Variant fileNameVariant = Dispatch.get(attachment, "FileName");   
  47.                     String fileName = fileNameVariant.toString();   
  48.                
  49.                     Variant saveResult = Dispatch.call(attachment, "SaveAsFile""F:\\"+fileName);   
  50.                 }   
  51.             }   
  52.            
  53.         }   
  54.     }   
  55.   
  56. }  
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代码 复制代码  收藏代码
  1. package test;   
  2.   
  3. import com.jacob.activeX.ActiveXComponent;   
  4. import com.jacob.com.Dispatch;   
  5.   
  6. public class TestMail3 {   
  7.   
  8.     /**  
  9.      * @param args  
  10.      */  
  11.     public static void main(String[] args) {   
  12.         final int olFolderContacts = 10;   
  13.   
  14.         ActiveXComponent ol = new ActiveXComponent("Outlook.Application");   
  15.            
  16.         Dispatch olo = ol.getObject();   
  17.         Dispatch myNamespace = Dispatch.call(olo, "GetNamespace","MAPI").toDispatch();   
  18.         Dispatch myFolder = Dispatch.call(myNamespace, "GetDefaultFolder",new Integer(olFolderContacts)).toDispatch();   
  19.   
  20.         //nächste zeile Fehlermeldung   
  21.   
  22.         Dispatch items = Dispatch.get(myFolder, "Items").toDispatch();   
  23.   
  24.         int count = Dispatch.call(items, "Count").toInt();   
  25.   
  26.         for (int i = 1; i <= count; i++) {   
  27.             Dispatch item;   
  28.             item = Dispatch.call(items, "Item"new Integer(i)).toDispatch();   
  29.        
  30.             String fullName = Dispatch.get(item, "Fullname").toString();   
  31.             String emailAddress = Dispatch.get(item, "Email1Address").toString();   
  32.             String addressStreet = Dispatch.get(item, "BusinessAddressStreet").toString();   
  33.             String addressCity = Dispatch.get(item, "BusinessAddressCity").toString();   
  34.             String addressCountry = Dispatch.get(item, "BusinessAddressCountry").toString();   
  35.             String addressPostalCode = Dispatch.get(item,"BusinessAddressPostalCode").toString();   
  36.             String addressState = Dispatch.get(item, "BusinessAddressState").toString();   
  37.             String title = Dispatch.get(item, "Title").toString();   
  38.             String phone = Dispatch.get(item, "BusinessTelephoneNumber").toString();   
  39.             String fax = Dispatch.get(item, "BusinessFaxNumber").toString();   
  40.        
  41.             printField("", fullName);   
  42.             printField("", title);   
  43.             printField("", addressStreet);   
  44.             printField("", addressCity);   
  45.             printField("", addressState);   
  46.             printField("", addressPostalCode);   
  47.             printField("", addressCountry);   
  48.             printField("Tel:", phone);   
  49.             printField("Fax:", fax);   
  50.             printField("Email:", emailAddress);   
  51.        
  52.             System.out.println();   
  53.         }   
  54.     }   
  55.   
  56.         public static void printField(String label, String value) {   
  57.             if (value.length() != 0) {   
  58.                 System.out.println(label + " " + value);   
  59.             }   
  60.         }   
  61.   
  62. }  
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 Fehlermeldung

		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);
			}
		}

}

 第二例地址:

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

。。。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值