转自: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

Outlook 开发的更多相关文章

  1. Outlook 开发2

    转自:http://www.cnblogs.com/madebychina/archive/2011/09/20/madebychina_2.html C#使用如下代码调用Outlook2003发送邮 ...

  2. VSTO:使用C#开发Excel、Word【10】

    第二部分:.NET中的Office编程本书前两章介绍了Office对象模型和Office PIA. 您还看到如何使用Visual Studio使用VSTO的功能构建文档中的控制台应用程序,加载项和代码 ...

  3. outlook2013插件 VSTO开发与部署

    一.背景 最近因为项目需要对outlook开发一个插件,功能是将outlook的邮件作导出功能,需要使用VSTO开发一个插件将邮件进行导出的操作.于是,开始学习VSTO outlook的开发了,折腾了 ...

  4. outlook 插件:导出rss的link地址

    由于对于rss的应用程序不熟悉,所以使用Outlook接收rss.使用过程和平时收邮件没有什么差别. 唯一的遗憾是鉴于安全考虑,outlook没有全部下载网页,所以每次都要打开浏览器.有时候遇到一些需 ...

  5. 如何使用.NET开发全版本支持的Outlook插件产品(四)——进阶探讨

    插件项目所有代码都已经上传至 https://github.com/VanPan/TestOutlookAdding 如何定制Ribbon在不同界面的显示 实际使用过程中出现的问题 这个问题的来自十分 ...

  6. 如何使用.NET开发全版本支持的Outlook插件产品(三)——全面控制

    插件项目所有代码都已经上传至 https://github.com/VanPan/TestOutlookAdding 进阶基础--COM查看 首先,对于Outlook对象模型,MSDN早就有非常详细的 ...

  7. 如何使用.NET开发全版本支持的Outlook插件产品(二)——完善插件

    插件项目所有代码都已经上传至 https://github.com/VanPan/TestOutlookAdding 勿在浮砂筑高台--定位错误 在介绍后面的插件开发技术之前,让我们先来看看已经达到的 ...

  8. 如何使用.NET开发全版本支持的Outlook插件产品(一)——准备工作

    这半年一直在做Outlook的插件,因为不会VC++,所以想找一款基于.NET,用C#开发Outlook插件的技术方案.没想到,光技术选型这件事,就用各种技术手段验证了将近一个月,还花费了大量的精力做 ...

  9. 我的VSTO之路(五):Outlook初步开发之联系人扩展

    原文:我的VSTO之路(五):Outlook初步开发之联系人扩展 上一讲我们完成对Word的介绍,文本开始,我将着重介绍Outlook.Outlook是微软Office中一个非常实用的工具,尤其在一个 ...

随机推荐

  1. javascript 高级编程系列 - 基本数据类型

    javascript中的基本数据类型包括: Undefined, Null, Boolean, Number, String 5种数据类型 1. Undefined 类型 (只有一个值 undefin ...

  2. OcelotAPI 简单使用—服务发现、流控

    我这人比较懒 直接上配置文件的图 其中serviceName是服务名称, LoadBalancer是负载均衡策略. 对于流控我为了做测试写的1s 限制5次请求. 剩下的看名字就OK了. 要使用服务发现 ...

  3. XML 解析错误:找不到根元素

    大家在开发web项目的过程中,可能会遇到“XML 解析错误:找不到根元素”这么一个问题,引起这个问题的原因可能有很多种,在这儿我只是跟大家分享一下我遇到一种情况. 1.项目背景描述 extjs 结合a ...

  4. Hadoop实战-Flume之Hdfs Sink(十)

    a1.sources = r1 a1.sinks = k1 a1.channels = c1 # Describe/configure the source a1.sources.r1.type = ...

  5. Git简介和安装

    一.什么是Git? Git是一个分布式版本控制系统,客户端并不只提取最新版本的文件快照,而是把代码仓库完整地镜像下来.如图所示: 任何一处的服务器或者个人机发生故障,都可以用其它机器的任何一个镜像出来 ...

  6. rtmp搭建直播系统

    开发环境 Ubuntu 14.04 server nginx-1.8.1 nginx-rtmp-module nginx的服务器的搭建 安装nginx的依赖库 sudo apt-get update ...

  7. Xcode使用的一些小技巧,值得一看。

    有时我们需要对一个已有项目进行重构,改进设计,提高代码质量.以下几个Xcode 4中的功能,会使重构的工作变得轻松很多. 1.打开项目我的项目是Xcode3.x中编辑的,在用Xcode 4 打开时出现 ...

  8. linux apache 用户认证:

    root@ubuntu:/# htpasswd -c /etc/apache2/password zhangsan (-c表示要创建一个password密码文件,文件存放目录是/etc/apache2 ...

  9. [noi2002]M号机器人

    3030年,Macsy正在火星部署一批机器人.第1秒,他把机器人1号运到了火星,机器人1号可以制造其他的机器人.第2秒,机器人1号造出了第一个机器人——机器人2号.第3秒,机器人1号造出了另一个机器人 ...

  10. javascript ajax和jquery ajax

    一 进行ajax步骤: 1 获取dom值 2发送ajax请求 3返回成功进行前端逻辑处理 二 原生javascript的ajax <!DOCTYPE html> <html> ...