Outlook 开发

转自:http://www.cnblogs.com/madebychina/archive/2011/09/20/madebychina_2.html

 

 

C#使用如下代码调用Outlook2003发送邮件:

  

复制代码
 1            // Create the Outlook application. 2                 Outlook.Application  oApp =new Outlook.Application();
 3 //Outlook.ApplicationClass  oApp = new Outlook.ApplicationClass();
 4  5 // Get the NameSpace and Logon information. 6                 Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
 7  8 // Log on by using a dialog box to choose the profile. 9                 oNS.Logon(Missing.Value, Missing.Value, true, true); 
10 11 // Alternate logon method that uses a specific profile.
12 // TODO: If you use this logon method, 
13 //  change the profile name to an appropriate value.
14 //oNS.Logon("YourValidProfile", Missing.Value, false, true); 
15 16 // Create a new mail item.17                 Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
18 19 // Set the subject.20                 oMsg.Subject ="mail";
21 //oMsg.Attachments.Add("C:\\mailLog.txt", Outlook.OlItemType.olMailItem, 1, "report");
22 23 // Set HTMLBody.24                 String sHtml;
25                 sHtml ="<HTML>\n"+26 "<HEAD>\n"+27 "<TITLE>Sample GIF</TITLE>\n"+28 "</HEAD>\n"+29 "<BODY><P>\n"+30 "<h1><Font Color=Green>Inline graphics</Font></h1></P>\n"+31 "</BODY>\n"+32 "</HTML>";
33                 oMsg.HTMLBody = sHtml;
34 35 // Add a recipient.36                 Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
37 // TODO: Change the recipient in the next line if necessary.38 string reciMailAdress=this.txtMail.Text;
39                 Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(reciMailAdress);
40                 oRecip.Resolve();
41 42 // Send.43                 oMsg.Send();
44 //((Microsoft.Office.Interop.Outlook._MailItem)oMsg).Send();
45 46 // Log off.47                 oNS.Logoff();
48 49 // Clean up.50                 oRecip =null;
51                 oRecips =null;
52                 oMsg =null;
53                 oNS =null;
54                 oApp =null;
复制代码

 

  但是因为Outlook2003的安全机制,总是会弹出安全提示对话框,如下:

  而且在我们选择允许访问,点击“是”按钮后还会弹出另外一个对话框,如下:

  并且“是”按钮还会被冻结5秒钟。分析原因是我们使用的是Outlook.MailItem邮件模式,这是一种非安全的模式,所以在第三方程序调用Outlook2003时会弹出确认对话框,如何来避免弹出安全提示对话框呢?我们可以使用SafeMailItem模式。但是在使用这种模式时需要引用Redemption.dll,而且还要安装Redemption插件,代码如下:

 

复制代码
 1       privatevoid Send()  2         {  3             Outlook.Application olApp =new Outlook.ApplicationClass();  4             Outlook.NameSpace olNS = olApp.GetNamespace("MAPI");  5             olNS.Logon(Missing.Value, Missing.Value, false, false);  6             SafeMailItem sItem =new Redemption.SafeMailItem();  7  8             Outlook.MailItem olItem = olApp.CreateItem(0) as Outlook.MailItem;  9 10             String sHtml; 11             sHtml ="<HTML>\n"+12 "<HEAD>\n"+13 "<TITLE>Sample GIF</TITLE>\n"+14 "</HEAD>\n"+15 "<BODY><P>\n"+16 "<h1><Font Color=Green>Inline graphics</Font></h1></P>\n"+17 "</BODY>\n"+18 "</HTML>"; 19             olItem.HTMLBody = sHtml; 20 21             sItem.Item = olItem; 22 23 string reciMailAdress=txtMail.Text; 24             sItem.Recipients.Add(reciMailAdress); 25             sItem.Recipients.ResolveAll(); 26             SetPropValue(sItem, "Subject", "Testing Redemption"); 27             sItem.Send(); 28         } 29 30 privatestaticobject GetPropValue(object item, string propertyName) 31         { 32 33 try34             { 35 object[] args =new Object[]{}; // dummy argument array36                 Type type = item.GetType(); 37 return type.InvokeMember( 38                     propertyName, 39                     BindingFlags.Public | BindingFlags.GetField 40 | BindingFlags.GetProperty, 41 null, 42                     item, 43                     args); 44             } 45 catch (SystemException ex) 46             { 47 //                Console.WriteLine( 48 //                    string.Format( 49 //                    "GetPropValue for {0} Exception: {1} ", 50 //                    propertyName, ex.Message));51             } 52 returnnull; 53         } 54 privatestaticvoid SetPropValue(object item, string propertyName,object propertyValue) 55         { 56 try57             { 58 object[] args =new Object[1]; 59                 args[0] = propertyValue; 60                 Type type = item.GetType(); 61                 type.InvokeMember(propertyName, 62                     BindingFlags.Public | BindingFlags.SetField |63                     BindingFlags.SetProperty, 64 null, 65                     item, 66                     args); 67             } 68 catch (SystemException ex) 69             { 70 //                Console.WriteLine( 71 //                    string.Format( 72 //                    "SetPropValue for {0} Exception: {1} ", 73 //                    propertyName, ex.Message));74             } 75 return; 76         }
复制代码

  这样就能避免弹出对话框了,其他高版本的Outlook好像没有此问题,Outlook2007在开启反病毒软件时不会弹出对话框。

  Redemption下载地址:http://www.dimastr.com/redemption/download.htm

  本文参考资料地址:http://www.outlookcode.com/article.aspx?id=52

           http://www.dotnet247.com/247reference/message.aspx?id=92601

posted @ 2014-02-28 10:57  iFreeSoft  阅读(946)  评论(0编辑  收藏  举报