js确认对话框,C#调用Outlook2003发送邮件时,避免弹出安全确认对话框的方法

 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 _disibledevent=>= oApp.GetNamespace("mapi"); 7 8 // Log _disibledevent=> 9 _disibledevent=>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 = "\n" + 26 "\n" + 27 "Sample GIF\n" + 28 "\n" + 29 "\n" + 30 "

Inline graphics


\n" + 31 "\n" + 32 ""; 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 _disibledevent=>48 49 // Clean up. 50 oRecip = null; 51 oRecips = null; 52 oMsg = null; 53 _disibledevent=>= null; 54 oApp = null;

但是因为Outlook2003的安全机制,总是会弹出安全提示对话框,如下:
C#调用Outlook2003发送邮件时,避免弹出安全确认对话框的方法js确认对话框
而且在我们选择允许访问,点击“是”按钮后还会弹出另外一个对话框,如下:
C#调用Outlook2003发送邮件时,避免弹出安全确认对话框的方法js确认对话框
并且“是”按钮还会被冻结5秒钟。分析原因是我们使用的是Outlook.MailItem邮件模式,这是一种非安全的模式,所以在第三方程序调用Outlook2003时会弹出确认对话框,如何来避免弹出安全提示对话框呢?我们可以使用SafeMailItem模式。但是在使用这种模式时需要引用Redemption.dll,而且还要安装Redemption插件,代码如下:
1 private void 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 = "\n" + 12 "\n" + 13 "Sample GIF\n" + 14 "\n" + 15 "\n" + 16 "

Inline graphics


\n" + 17 "\n" + 18 ""; 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 private static object GetPropValue(object item, string propertyName) 31 { 32 33 try 34 { 35 object[] args = new Object[]{}; // dummy argument array 36 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 return null; 53 } 54 private static void SetPropValue(object item, string propertyName,object propertyValue) 55 { 56 try 57 { 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
Tags: 

延伸阅读

最新评论

发表评论