Outlook 开发2
转自: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插件,代码如下:
private void 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(0) 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();
} private static object 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));
}
return null;
}
private static void SetPropValue(object item, string propertyName,object propertyValue)
{
try
{
object[] args = new Object[1];
args[0] = 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 开发2的更多相关文章
- Outlook 开发
转自:http://www.cnblogs.com/madebychina/archive/2011/09/20/madebychina_2.html C#使用如下代码调用Outlook2003发送邮 ...
- VSTO:使用C#开发Excel、Word【10】
第二部分:.NET中的Office编程本书前两章介绍了Office对象模型和Office PIA. 您还看到如何使用Visual Studio使用VSTO的功能构建文档中的控制台应用程序,加载项和代码 ...
- outlook2013插件 VSTO开发与部署
一.背景 最近因为项目需要对outlook开发一个插件,功能是将outlook的邮件作导出功能,需要使用VSTO开发一个插件将邮件进行导出的操作.于是,开始学习VSTO outlook的开发了,折腾了 ...
- outlook 插件:导出rss的link地址
由于对于rss的应用程序不熟悉,所以使用Outlook接收rss.使用过程和平时收邮件没有什么差别. 唯一的遗憾是鉴于安全考虑,outlook没有全部下载网页,所以每次都要打开浏览器.有时候遇到一些需 ...
- 如何使用.NET开发全版本支持的Outlook插件产品(四)——进阶探讨
插件项目所有代码都已经上传至 https://github.com/VanPan/TestOutlookAdding 如何定制Ribbon在不同界面的显示 实际使用过程中出现的问题 这个问题的来自十分 ...
- 如何使用.NET开发全版本支持的Outlook插件产品(三)——全面控制
插件项目所有代码都已经上传至 https://github.com/VanPan/TestOutlookAdding 进阶基础--COM查看 首先,对于Outlook对象模型,MSDN早就有非常详细的 ...
- 如何使用.NET开发全版本支持的Outlook插件产品(二)——完善插件
插件项目所有代码都已经上传至 https://github.com/VanPan/TestOutlookAdding 勿在浮砂筑高台--定位错误 在介绍后面的插件开发技术之前,让我们先来看看已经达到的 ...
- 如何使用.NET开发全版本支持的Outlook插件产品(一)——准备工作
这半年一直在做Outlook的插件,因为不会VC++,所以想找一款基于.NET,用C#开发Outlook插件的技术方案.没想到,光技术选型这件事,就用各种技术手段验证了将近一个月,还花费了大量的精力做 ...
- 我的VSTO之路(五):Outlook初步开发之联系人扩展
原文:我的VSTO之路(五):Outlook初步开发之联系人扩展 上一讲我们完成对Word的介绍,文本开始,我将着重介绍Outlook.Outlook是微软Office中一个非常实用的工具,尤其在一个 ...
随机推荐
- 简化动态MERGE的SQL计算
MSSQL.ORACLE等数据库支持MERGE语句更新表.但表结构未知时,因为缺乏集合类数据.用存储过程获得表结构再动态拼出SQL很麻烦,代码会有几十行之多:相同原因,用Java等高级语言实现也不简单 ...
- CentOS7配置opencv for python && eclipse c/c++[更新]
更改前的安装过程有些问题,主要是ffmpeg-devel的安装部分,这里重新说一下 两种安装方法: 第一种,直接: # yum install numpy opencv* 这种方法安装了之后,能够在p ...
- iOS移动开发周报-第20期
iOS移动开发周报-第20期iOS移动开发周报-第20期 [摘要]:本期iOS移动开发周报带来如下内容:iOS 通知中心扩展制作入门,iOS APP可执行文件的组成,objc非主流代码技巧等. 教程 ...
- Linux内核RCU(Read Copy Update)锁简析
在非常早曾经,大概是2009年的时候.写过一篇关于Linux RCU锁的文章<RCU锁在linux内核的演变>,如今我承认.那个时候我尽管懂了RCU锁,可是我没有能力用一种非常easy的描 ...
- C++ 坑人系列(1): 让面试官晕倒的题目
今天和几位同仁一起探讨了一下C++的一些基础知识,在座的同仁都是行家了,有的多次当过C++技术面试官.不过我出的题过于刁钻: 不是看起来太难,而是看起来极其容易,但是其实非常难! 结果一圈下来,3道 ...
- 在XP上安装VS2002
在2002 年,随着 .NET 口号的提出与 Windows XP/Office XP 的公布,微软公布了 Visual Studio .NET(内部版本为 7.0). 使用VS2002+Object ...
- Bullet Physics OpenGL 刚体应用程序模板 Rigid Simulation in Bullet
利用Bullet物理引擎实现刚体的自由落体模拟的模板 Bullet下载地址 Main.cpp #include <GLUT/glut.h> #include <cstdlib> ...
- 【BZOJ4407】于神之怒加强版 莫比乌斯反演
[BZOJ4407]于神之怒加强版 Description 给下N,M,K.求 Input 输入有多组数据,输入数据的第一行两个正整数T,K,代表有T组数据,K的意义如上所示,下面第二行到第T+1行, ...
- wcf系列(一)--- 寄宿方式
一.自我寄宿(self-hosting) 1.wcf采用基于终结点(Endpoint)的通信手段:终结点由:地址(Address)+绑定(Binding)+契约(Contract)组成: Enpoi ...
- Vue中data重置问题
Object.assign() Object.assign()方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.它将返回目标对象. 目标对象有1个,后边可以有多个源对象.注意他只会拷贝源对 ...