是否可以从SMTP库访问Outlook? (C#控制台应用程序)

是否可以从SMTP库访问Outlook? (C#控制台应用程序)

问题描述:

我有一个C#应用程序,必须附加一个文件并通过电子邮件发送给人们。

I have a C# application that has to attach a file and email it to people.

目前我一直在使用SMTP库并放入收件人手工地址(技术上,通过配置文件)。但我意识到通过电子邮件发送全球通讯组列表而不是手动输入电子邮件
来做电子邮件要好得多。

Currently I've been having it use the SMTP library and putting in the recipient addresses by hand (technically, by a configuration file). But I realize it would be a lot better to do the emails by emailing global distribution lists rather than manually inputting the emails by hand.

有没有办法发送电子邮件到使用SMTP通过配置文件列出全局通讯组列表?或者我将不得不使用微软的Outlook dll?如果我必须使用Outlook,有人可以指点我一些示例代码让我开始吗?

Is there a way to send emails to global distribution lists via a configuration file using SMTP? Or will I have to use Microsofts' Outlook dll? If I have to use Outlook, could someone point me to some example code to get me started?

访问outlook与使用无关SMTP,您可以使用以下内容:

Accessing outlook is independent of using SMTP, you can use the following:

下面介绍如何使用.NET从Outlook文件夹中的项目(在"收件箱"文件夹下称为"MySubFolderName")中检索数据:

The following demostrates how to retreive data from items within an Outlook folder (called "MySubFolderName" under the Inbox folder) using .NET:

首先添加对项目的Outlook COM对象的引用:

First add a reference to the Outlook COM object your project:


  1. 在VS.NET中右键单击"引用"并选择"添加引用" 。
  2. 选择"COM"选项卡
  3. 选择"Microsoft Outlook 11.0对象库"(这适用于MS Office 2003 - 我认为10.0适用于Office XP),然后单击"选择"。
  4. 单击"确定"。

请注意,您可以访问任何Outlook / Exchange对象类型,例如约会,备注,任务,电子邮件等 - 只需使用intellisense选择哪一个(例如
Microsoft.Office.Interop.Outlook .... - 参见定义下面称为'item'的变量。)

Note that you can access any Outlook/Exchange object types, eg Appointments, Notes, Tasks, Emails etc - just use intellisense to select which one (eg Microsoft.Office.Interop.Outlook. ... - see definition of variable called 'item' below).

这是代码:

Microsoft.Office.Interop.Outlook.Application app = null;

Microsoft.Office.Interop.Outlook._NameSpace ns = null;

Microsoft.Office.Interop.Outlook .PostItem item = null;

Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;

Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;

试试
{

  app = new Microsoft.Office.Interop.Outlook.Application();

  ns = app.GetNamespace(" MAPI");

  ns.Logon(null,null,false,false);

  inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

  subFolder = inboxFolder.Folders [" MySubFolderName"]; //folder.Folders[1];也工作

  Console.WriteLine(" Folder Name:{0},EntryId:{1}",subFolder.Name,subFolder.EntryID);

  Console.WriteLine(" Num Items:{0}",subFolder.Items.Count.ToString());

  for(int i = 1; i< = subFolder.Items.Count; i ++)

  {

    item =(Microsoft.Office.Interop.Outlook.PostItem)subFolder.Items [i];

    Console.WriteLine(" Item:{0}",i.ToString());

    Console.WriteLine(" Subject:{0}",item.Subject);

    Console.WriteLine(" Sent:{0} {1}" item.SentOn.ToLongDateString(),item.SentOn.ToLongTimeString());
$
    Console.WriteLine(" Categories:{0}",item.Categories);

    Console.WriteLine(" Body:{0}",item.Body);

    Console.WriteLine(" HTMLBody:{0}",item.HTMLBody);

  }
}

catch(System.Runtime.InteropServices.COMException ex)

{

  Console.WriteLine(ex.ToString());

}

最后

{

  ns = null;

  app = null;

  inboxFolder = null;

}