设为首页 收藏本站
查看: 1375|回复: 0

[经验分享] Outlook 开发

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-9-13 09:45:26 | 显示全部楼层 |阅读模式
  转自:http://www.cnblogs.com/madebychina/archive/2011/09/20/madebychina_2.html
  
  
  C#使用如下代码调用Outlook2003发送邮件:
  




DSC0000.gif
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的安全机制,总是会弹出安全提示对话框,如下:
DSC0001.jpg
  而且在我们选择允许访问,点击“是”按钮后还会弹出另外一个对话框,如下:
DSC0002.jpg
  并且“是”按钮还会被冻结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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-112902-1-1.html 上篇帖子: Error 18000 during outlook 2011 rebuild 下篇帖子: Outlook 2013 在邮件里面点击超链接时弹出“组织策略阻止我们为您完成此操作”
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表