http://uule.iteye.com/blog/1195597
- 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");
- }
- }
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/
- 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);
- }
- }
- }
- }
- }
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);
}
}
}
}
}
- 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);
- }
- }
- }
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://sourceforge.net/projects/jacob-project/forums/forum/375946/topic/3734426/index/page/1
。。。