在 C# 中访问交换电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/865267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access exchange e-mail in C#
提问by juan
Do you know if there's a way?
你知道有没有办法?
I've used this libraryto access a pop3 server, but it doesn't work with an exchange server.
我已经使用这个库来访问 pop3 服务器,但它不适用于交换服务器。
Do you know of any other library or piece of code that'll show me how to do it?
你知道任何其他库或一段代码可以告诉我如何做吗?
I cannot change any settings on the server.
我无法更改服务器上的任何设置。
采纳答案by Mark Brackett
Depends on the Exchange version. WebDAVworks with 2000 thru 2007, but Web Servicesrequires 2007+.
取决于 Exchange 版本。WebDAV适用于 2000 年到 2007 年,但Web 服务需要 2007+。
Those are probably the easiest to get working. CDO is another option, but it's not supported from C#- so you'll have to go out of proc.
这些可能是最容易开始工作的。CDO 是另一种选择,但C# 不支持它- 因此您必须退出 proc。
Exchange also has an OLEDB provider, but I've never used it - it is supported from .NET, however.
Exchange 也有一个OLEDB provider,但我从未使用过它 - 但是.NET 支持它。
回答by Jose Basilio
You need to use the Exchange SDKif POP3 is not enabled in the Exchange Server. Another options is to use WebDAV.
如果 Exchange Server 中未启用 POP3,则需要使用Exchange SDK。另一种选择是使用WebDAV。
回答by Christian
You could use this library: http://www.dimastr.com/redemption/
你可以使用这个库:http: //www.dimastr.com/redemption/
回答by Joe
Another option is to configure Exchange to enable IMAP4. There exist 3rd party IMAP4 libraries for .NET, e.g. rebex.
另一种选择是配置 Exchange 以启用 IMAP4。存在用于 .NET 的 3rd 方 IMAP4 库,例如 rebex。
回答by Christian
You could use EWS (Exchange webservices) starting from Exchange 2007. They seem to be always installed and are better supported than Webdav.
您可以从 Exchange 2007 开始使用 EWS(Exchange webservices)。它们似乎总是被安装并且比 Webdav 得到更好的支持。
You can just import the webservice from your Exchangeserver (Search for an asmx-file in the installation directory). You can download EML-files from your mails and do many more things!
您可以从 Exchangeserver 导入 webservice(在安装目录中搜索 asmx 文件)。您可以从邮件中下载 EML 文件并执行更多操作!
回答by Scott Anderson
If you use Exchange 2007 and have web services enabled, this is pretty easy. I added a 2.0-style classic Web Reference to my VS2008 project, and I can get mail messages like this:
如果您使用 Exchange 2007 并启用了 Web 服务,这将非常容易。我在我的 VS2008 项目中添加了一个 2.0 风格的经典 Web Reference,我可以收到这样的邮件:
// exchange 2007 lets us use web services to check mailboxes.
using (ExchangeServiceBinding exchangeServer = new ExchangeServiceBinding())
{
ICredentials creds = new NetworkCredential("user","password");
exchangeServer.Credentials = creds;
exchangeServer.Url = "https://myexchangeserver.com/EWS/Exchange.asmx";
FindItemType findItemRequest = new FindItemType();
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
// define which item properties are returned in the response
ItemResponseShapeType itemProperties = new ItemResponseShapeType();
itemProperties.BaseShape = DefaultShapeNamesType.AllProperties;
findItemRequest.ItemShape = itemProperties;
// identify which folder to search
DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
folderIDArray[0] = new DistinguishedFolderIdType();
folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;
// add folders to request
findItemRequest.ParentFolderIds = folderIDArray;
// find the messages
FindItemResponseType findItemResponse = exchangeServer.FindItem(findItemRequest);
// read returned
FindItemResponseMessageType folder = (FindItemResponseMessageType)findItemResponse.ResponseMessages.Items[0];
ArrayOfRealItemsType folderContents = new ArrayOfRealItemsType();
folderContents = (ArrayOfRealItemsType)folder.RootFolder.Item;
ItemType[] items = folderContents.Items;
// if no messages were found, then return null -- we're done
if (items == null || items.Count() <= 0)
return null;
// FindItem never gets "all" the properties, so now that we've found them all, we need to get them all.
BaseItemIdType[] itemIds = new BaseItemIdType[items.Count()];
for (int i = 0; i < items.Count(); i++)
itemIds[i] = items[i].ItemId;
GetItemType getItemType = new GetItemType();
getItemType.ItemIds = itemIds;
getItemType.ItemShape = new ItemResponseShapeType();
getItemType.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
getItemType.ItemShape.BodyType = BodyTypeResponseType.Text;
getItemType.ItemShape.BodyTypeSpecified = true;
GetItemResponseType getItemResponse = exchangeServer.GetItem(getItemType);
ItemType[] messages = new ItemType[getItemResponse.ResponseMessages.Items.Count()];
for (int j = 0; j < messages.Count(); j++)
messages[j] = ((ItemInfoResponseMessageType)getItemResponse.ResponseMessages.Items[j]).Items.Items[0];
return messages;
}
The "messages" variable will be an array of ItemType objects returned from exchange that have all the properties you'd expect for a mail message (Body, Attachments, etc.). I hope this helps!
“messages”变量将是从交换返回的 ItemType 对象数组,这些对象具有您期望的邮件消息(正文、附件等)的所有属性。我希望这有帮助!
回答by Jim Scott
I assume your issue is that your exchange server only support NTLM authentication and does not allow plain text authentication? Or you might not be using the proper username convention. For example you might try using the format username@domain where domain is the internal NT domain which might not be the same as your internet domain.
我假设您的问题是您的交换服务器仅支持 NTLM 身份验证而不允许纯文本身份验证?或者您可能没有使用正确的用户名约定。例如,您可以尝试使用 username@domain 格式,其中 domain 是内部 NT 域,它可能与您的 Internet 域不同。
If that is the case then look for a library that supports NTLM.
如果是这种情况,请寻找支持 NTLM 的库。
Steps for testing via telnet
通过 telnet 进行测试的步骤
Go to command prompt type: telnet my.server.com 110 you should get a response from your exchange server like this +OK Microsoft Exchange Server 2003 POP3 server version 6.5.7638.1 (my.server.com) ready.
转到命令提示符类型:telnet my.server.com 110 你应该从你的交换服务器得到这样的响应 +OK Microsoft Exchange Server 2003 POP3 服务器版本 6.5.7638.1 (my.server.com) 准备好了。
type: CAPA this should return the list of capabilities your exchange server supports. CAPA +OK Capability list follows TOP USER PIPELINING EXPIRE NEVER UIDL SASL NTLM .
类型:CAPA 这应该返回您的交换服务器支持的功能列表。CAPA +OK 功能列表遵循 TOP USER PIPELINING EXPIRE NEVER UIDL SASL NTLM 。
Notice that mine does not show PLAIN
请注意,我的不显示 PLAIN
Here is a response from an email server that does+OK Dovecot ready. CAPA +OK CAPA TOP UIDL RESP-CODES PIPELINING STLS USER SASL PLAIN .
这是来自电子邮件服务器的响应,它已准备好 +OK Dovecot。CAPA +OK CAPA TOP UIDL RESP 代码流水线 STLS 用户 SASL PLAIN。
If your response does not include PLAIN then stop as you need a library that supports SPA
如果您的回复不包含 PLAIN,则停止,因为您需要一个支持 SPA 的库
type: user myusername OR type: user [email protected] replacing domain.corp with your domain
类型:用户 myusername 或类型:用户 [email protected] 用您的域替换 domain.corp
You should then receive +OK
然后您应该会收到 +OK
Type: pass mypass
类型:pass mypass
You should get a response +OK
你应该得到回复 +OK
type: list
类型:列表
Should get a list of emails. This might help see if your issue is a username format issue.
应该得到一个电子邮件列表。这可能有助于查看您的问题是否是用户名格式问题。
回答by Ryan Barrett
Using EWS directly in managed code (VB.net / C#) is clumsy to say the best.
直接在托管代码 (VB.net / C#) 中使用 EWS 是笨拙的。
I've been fumbling around with it for a few days now, and have come to the conclusion that it's better to build my own wrapper classes around the API, making the services usable in a line or two of code, not the page with the current implementation.
我已经摸索了几天,得出的结论是,最好围绕 API 构建我自己的包装类,使服务可以在一两行代码中使用,而不是在带有当前的实施。
Guess what? Microsoft have beaten me to it: Exchange Web Services Managed API's first Release Candidate is available for download here.
你猜怎么着?Microsoft 击败了我:Exchange Web Services Managed API 的第一个候选发布版本可在此处下载。
Install, register the dll reference (\Program Files\Microsoft\Exchange\Web Services\1.0\Micorosft.Exchange.WebServices.dll), and import the namespace (Microsoft.Exchange.WebServices.Data) and you're ready to roll.
安装、注册 dll 引用 (\Program Files\Microsoft\Exchange\Web Services\1.0\Micorosft.Exchange.WebServices.dll),然后导入命名空间 (Microsoft.Exchange.WebServices.Data),您就可以开始使用了。
回答by Matt
I wrote this to get email from my inbox. I have a tedious task to do every morning if an email is there, so I wrote this bit of code to check my folder for an email title. I added a small bit of xml creation to show the possibilities. Its not exchange but it works in my case. XCData encodes any special char in the body. I guess I should point out that email subject I'm looking for is [Ticket - Support#12345]
我写这个是为了从我的收件箱中接收电子邮件。如果有电子邮件,我每天早上都有一项乏味的任务要做,所以我编写了这段代码来检查我的文件夹中的电子邮件标题。我添加了一点 xml 创建来展示可能性。它不是交换,但它适用于我的情况。XCData 对正文中的任何特殊字符进行编码。我想我应该指出我正在寻找的电子邮件主题是 [Ticket - Support#12345]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Office;
using System.Xml.Linq;
namespace ProcessEmail
{
class Program
{
static void Main(string[] args)
{
Outlook.Application outlook = new Outlook.Application();
Outlook.NameSpace ns = outlook.GetNamespace("Mapi");
object _missing = Type.Missing;
ns.Logon(_missing, _missing, false, true);
Outlook.MAPIFolder inbox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
int unread = inbox.UnReadItemCount;
XElement xmlMail = new XElement("Mail");
foreach (Outlook.MailItem mail in inbox.Items)
{
string s = mail.Subject;
if (s != null)
{
if (s.Contains("Tickets") || (s.Contains("Support")))
{
string[] splitter = s.Split('#');
string[] split = splitter[1].Split(']');
string num = split[0].Trim();
XElement mailrow = new XElement("MailRow",
new XElement("Ticket_Number",num),
new XElement("Subject", mail.Subject),
new XElement("Body", new XCData(mail.Body)),
new XElement("From", mail.SenderEmailAddress)
);
xmlMail.Add(mailrow);
}
}
}
xmlMail.Save("E:\mailxml.xml");
}
}
}
Matt
马特
回答by Can Gencer
You can use Exchange Web Services(Exchange 2007 or 2010) or WebDav(Exchange up to 2007, 2010 does not support WebDAV), but the API and implementation might be a bit cumbersome if you want to do quick development.
您可以使用Exchange Web Services(Exchange 2007 或 2010)或WebDav(Exchange 到2007,2010不支持 WebDAV),但是如果您想快速开发,API 和实现可能会有点麻烦。
I have used IndependentSoft's libraries for WebDavand Exchange Web Servicessuccessfully in the past, which provide a wrapper around the Exchange APIs and are much simpler to use. They handle parsing the message and MIME quite well too.
过去,我成功地使用了 IndependentSoft 的WebDav和Exchange Web 服务库,它们提供了 Exchange API 的包装器,并且使用起来更加简单。他们也能很好地处理消息和 MIME 的解析。