如何使用c#从outlook中读取电子邮件 [英] how to read emails from outlook using c#

查看:516
本文介绍了如何使用c#从outlook中读取电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用此代码阅读电子邮件,但我收到了错误消息。请给我任何建议。

Hi,
I am using this code for reading emails but i got an error. Please give me any suggestions.

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;

               try
               {
                 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["Test"]; //folder.Folders[1]; also works
                 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("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());
               }
               finally
               {
                 ns = null;
                 app = null;
                 inboxFolder = null;
               }











********错误是






******** error is

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.PostItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063024-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

推荐答案

如何从Outlook或.PST文件中读取邮件 [ ^ ]



在Outlook中访问您的电子邮件(最终部分)C# [ ^ ]



在C#中展望集成 [ ^ ]



阅读来自C#的Outlook电子邮件 [ ^ ]



从C#读取Outlook邮件 [ ^ ]



CodeProject类似的QA [ ^ ]
How to read mails from Outlook or .PST Files[^]

Access your Email within Outlook (Final Part) C#[^]

Outlook Integration in C#[^]

Reading outlook emails from C# [^]

Read Outlook mails from C#[^]

CodeProject similar QA[^]


这可能会帮到你:< a href =http://stackoverflow.com/questions/4656360/unable-to-cast-com-object-microsoft-outlook-c-sharp> http://stackoverflow.com/questions/4656360/unable-to -cast-com-object-microsoft-outlook-c-sharp [ ^ ]
This might help you out: http://stackoverflow.com/questions/4656360/unable-to-cast-com-object-microsoft-outlook-c-sharp[^]


我从不推荐Office.Interop,它们非常复杂,MS不支持这种方式你喜欢用它。这不是Office Interop程序集的目的

步骤

1.从 Exchange Web Services托管API 1.2 并安装32或64的目标版本。请记住以后需要注意安装它的目录。 。值得注意的是,安装目录包含一个优秀的文档入门,其中包含显示Web服务功能的示例。

2.在Studio中创建项目,下面的示例C#代码以控制台应用程序为目标。

3.通过浏览到步骤1中加载的目录并选择Microsoft.Exchange.WebServices.dll,添加对Microsoft.Exchange.WebServices的引用。记得使用Microsoft.Exchange.WebServices.Data添加;在你的代码中。

4.添加以下代码行来初始化Web服务:



I never recommend Office.Interop and they are highly complicated and MS doesnt support the way you like to use it. Thats not the purpose of Office Interop Assemblies
Steps
1. Download and install the EWS assemblies from Exchange Web Services Managed API 1.2 and install the target version either 32 or 64. Please remember to note the directory which it gets installed to for it is needed later. Of note is that the install directory contains an excellent document Getting Started which has examples that show the capabilities of the web services.
2. Create your project in Studio, the below example C# code targets a Console Application.
3. Add a reference to Microsoft.Exchange.WebServices by browsing to the directory loaded in step 1 and selecting the Microsoft.Exchange.WebServices.dll. Remember to add using Microsoft.Exchange.WebServices.Data; in your code.
4. Add these lines of code to initialize the web service:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
 
//service.Credentials = new NetworkCredential( "{Active Directory ID}", "{Password}", "{Domain Name}" );
 
service.AutodiscoverUrl( "First.Last@MyCompany.com" );







o Line 1:创建一个Exchange Web服务实例,指定我们的目标是特定版本的Exchange。

o第3行:您可能需要有一个服务帐户执行您的脏工作并且此行未注释是您的关键这样做。

o第5行:我们没有指定Exchange服务器,但是自动发现它。服务器可以是群集的,这将是最好的工作。我们还指定要使用的帐户邮箱。

5.一旦服务启动并运行,我们现在可以在框中找到电子邮件。我们是这样做的:






o Line 1 : Create an Exchange Web Service instance specifying that we are targeting a specific version of Exchange.
o Line 3 : You may need to have a service account do your dirty work and this line uncommented out is your key to doing that.
o Line 5 : We don’t specify an Exchange server, but auto discover it. The server may be clustered and this will get the best one for the job. We also specify the account’s mailbox to use.
5. Once the service is up and running we can now get the emails found in the box. Here is how we do it:

FindItemsResults<item> findResults = service.FindItems(
   WellKnownFolderName.Inbox,
   new ItemView( 10 ) );
 
foreach ( Item item in findResults.Items )
   Console.WriteLine( item.Subject );</item>





第1行:我们致电该服务了解邮箱。

第2行:我们只对收件箱感兴趣,但我们可能对日历感兴趣。这是指定这些项目的地方。

第3行:我们只想要10个项目...更改不同结果的数字

第5行:打印出项目,我们只是将显示主题,但我们可以显示其他标题项。

运行程序。如果一切顺利,那么我们已经获得了邮箱的前十项。

这应该让你开始。再次查看安装文件中的安装文件夹的入门文档以获取更多示例!



Line 1 : We call the service to find out about the mailbox.
Line 2 : We are interested in the inbox only, but we could be interested in the calendar. This is where to specify those items.
Line 3 : We only want 10 items…change that number for different results
Line 5: Print out the items, we are only going to show the subject but there are other header items we could show.
Run the program. If everything goes alright then we have gotten the top ten items of the mailbox.
That should get you started. Again check the Getting Started document which the install dropped into the install folder for more examples!


这篇关于如何使用c#从outlook中读取电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆