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

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

  

            // Create the Outlook application.                 Outlook.Application  oApp =new Outlook.Application();
//Outlook.ApplicationClass oApp = new Outlook.ApplicationClass();
// Get the NameSpace and Logon information. Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
// Log on by using a dialog box to choose the profile. oNS.Logon(Missing.Value, Missing.Value, true, true);
// Alternate logon method that uses a specific profile.
// TODO: If you use this logon method,
// change the profile name to an appropriate value.
//oNS.Logon("YourValidProfile", Missing.Value, false, true);
// Create a new mail item. Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
// Set the subject. oMsg.Subject ="mail";
//oMsg.Attachments.Add("C:\\mailLog.txt", Outlook.OlItemType.olMailItem, 1, "report");
// Set HTMLBody. String sHtml;
sHtml ="<HTML>\n"+ "<HEAD>\n"+ "<TITLE>Sample GIF</TITLE>\n"+ "</HEAD>\n"+ "<BODY><P>\n"+ "<h1><Font Color=Green>Inline graphics</Font></h1></P>\n"+ "</BODY>\n"+ "</HTML>";
oMsg.HTMLBody = sHtml;
// Add a recipient. Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
// TODO: Change the recipient in the next line if necessary. string reciMailAdress=this.txtMail.Text;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(reciMailAdress);
oRecip.Resolve();
// Send. oMsg.Send();
//((Microsoft.Office.Interop.Outlook._MailItem)oMsg).Send();
// Log off. oNS.Logoff();
// Clean up. oRecip =null;
oRecips =null;
oMsg =null;
oNS =null;
oApp =null;

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

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

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

 1       privatevoid Send()          {              Outlook.Application olApp =new Outlook.ApplicationClass();              Outlook.NameSpace olNS = olApp.GetNamespace("MAPI");              olNS.Logon(Missing.Value, Missing.Value, false, false);              SafeMailItem sItem =new Redemption.SafeMailItem();               Outlook.MailItem olItem = olApp.CreateItem() as Outlook.MailItem;               String sHtml;              sHtml ="<HTML>\n"+ "<HEAD>\n"+ "<TITLE>Sample GIF</TITLE>\n"+ "</HEAD>\n"+ "<BODY><P>\n"+ "<h1><Font Color=Green>Inline graphics</Font></h1></P>\n"+ "</BODY>\n"+ "</HTML>";              olItem.HTMLBody = sHtml;               sItem.Item = olItem;   string reciMailAdress=txtMail.Text;              sItem.Recipients.Add(reciMailAdress);              sItem.Recipients.ResolveAll();              SetPropValue(sItem, "Subject", "Testing Redemption");              sItem.Send();          }   privatestaticobject GetPropValue(object item, string propertyName)          {   try             {  object[] args =new Object[]{}; // dummy argument array                 Type type = item.GetType();  return type.InvokeMember(                      propertyName,                      BindingFlags.Public | BindingFlags.GetField  | BindingFlags.GetProperty,  null,                      item,                      args);              }  catch (SystemException ex)              {  //                Console.WriteLine(  //                    string.Format(  //                    "GetPropValue for {0} Exception: {1} ",  //                    propertyName, ex.Message));             }  returnnull;          }  privatestaticvoid SetPropValue(object item, string propertyName,object propertyValue)          {  try             {  object[] args =new Object[];                  args[] = propertyValue;                  Type type = item.GetType();                  type.InvokeMember(propertyName,                      BindingFlags.Public | BindingFlags.SetField |                     BindingFlags.SetProperty,  null,                      item,                      args);              }  catch (SystemException ex)              {  //                Console.WriteLine(  //                    string.Format(  //                    "SetPropValue for {0} Exception: {1} ",  //                    propertyName, ex.Message));             }  return;          }

  这样就能避免弹出对话框了,其他高版本的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. .NET LINQ 转换数据类型
  2. js 漩涡
  3. PHP数组处理函数的使用array_push(一)
  4. python : jquery实现左侧菜单
  5. Ingress qdisc
  6. 分析实现Android自定义View之扇形图
  7. Spring与其他Web框架集成
  8. 针对PIL中ImageDraw.py报错的解决方案
  9. vs2013+sql server2012 +win8.1+entity framework + linq
  10. layUI弹出框提示
  11. webpack学习记录 - 学习webpack-dev-server(三)
  12. RabbitMQ安装详解
  13. 第一阶段——站立会议总结DAY10
  14. Java遍历集合的几种方法分析(实现原理、算法性能、适用场合)
  15. UniGui之锱铢积累(仔细看这个文件)
  16. [buaa-SE-2017]结对项目-数独程序扩展
  17. JVM中各种变量保存位置
  18. 奇葩属性:layout_weight 的解释及使用
  19. 云计算之路-阿里云上:踩着RDS的2个坑
  20. 专题1-MMU-lesson2-深入剖析地址转化

热门文章

  1. Android自己定义控件
  2. hibernate5(10)注解映射[2]一对多单向关联
  3. 【每日Scrum】第三天(4.13) TD学生助手Sprint1站立会议
  4. H5 手机横竖屏判读
  5. RYU改动监听port Mininet在custom自建拓扑和连接到指定控制器命令解释
  6. leetcode题目解答报告(2)
  7. mongodb的锁和高并发
  8. Netty聊天器(实战一):从0开始实战100w级流量应用
  9. IE6 PNG图片不透明的解决方案-tinypng
  10. R in Action(0) 开篇