代码之家  ›  专栏  ›  技术社区  ›  juan

访问C中的Exchange电子邮件#

  •  14
  • juan  · 技术社区  · 17 年前

    你知道有没有办法吗?

    我已经用过 this library 访问POP3服务器,但它不能与Exchange服务器一起工作。

    你知道有没有其他的库或代码可以告诉我怎么做吗?

    我无法更改服务器上的任何设置。

    11 回复  |  直到 10 年前
        1
  •  6
  •   Pete Shalini Gupta    14 年前

    取决于Exchange版本。 WebDAV 适用于2000到2007年,但是 Web Services 需要2007 +。

    这些可能是最容易工作的。CDO是另一种选择,但是 it's not supported from C# -所以你得走了 out of proc .

    交易所也有 OLEDB provider 但是我从来没有用过-它是 supported from .NET 然而。

        2
  •  18
  •   Scott Arrington    17 年前

    如果您使用Exchange2007并启用了Web服务,那么这非常简单。我在我的VS2008项目中添加了一个2.0样式的经典Web引用,我可以得到如下的邮件消息:

    // 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;
    }
    

    “messages”变量将是从Exchange返回的itemtype对象数组,这些对象具有邮件消息(正文、附件等)所需的所有属性。希望这有帮助!

        3
  •  5
  •   Ryan Barrett    16 年前

    直接在托管代码(vb.net/c)中使用EWS说得最好是笨拙的。

    我已经摸索了几天了,并且得出了这样的结论:最好围绕API构建自己的包装类,使服务在一两行代码中可用,而不是当前实现的页面。

    你猜怎么着?微软已经打败了我:Exchange Web Services Managed API的第一个候选版本可供下载 here .

    安装、注册dll引用(\Program Files\Microsoft\Exchange\Web Services\1.0\Micorosft.Exchange.WebServices.dll),然后导入命名空间(Microsoft.Exchange.WebServices.Data),就可以开始了。

        4
  •  3
  •   Jim Scott    17 年前

    我认为您的问题是您的Exchange服务器只支持NTLM身份验证,不允许纯文本身份验证?或者您可能没有使用正确的用户名约定。例如,您可以尝试使用格式username@domain,其中domain是内部NT域,它可能与Internet域不同。

    如果是这种情况,请查找支持NTLM的库。

    通过telnet测试的步骤

    转到命令提示 类型:telnet my.server.com 110 您应该从Exchange服务器得到这样的响应 +好的,Microsoft Exchange Server 2003 POP3服务器版本6.5.7638.1(my.server.com)准备就绪。

    类型:CAPA 这将返回您的Exchange服务器支持的功能列表。 卡帕 +OK能力列表如下 顶部 用户 流水线技术 永不过期 通用数据接口 SASL NTLM .

    注意,我的不清楚

    这是一个电子邮件服务器的回复,该服务器准备好了+OK DoveCot。 卡帕 +ok 卡帕 顶部 通用数据接口 责任码 流水线技术 STLS 用户 萨斯平平原 .

    如果您的回答不包括plain,那么请停止,因为您需要一个支持SPA的库。

    类型:用户myusername 或 键入:user myusername@domain.corp将domain.corp替换为您的域

    你应该收到 +ok

    类型:通过MyPass

    你应该得到答复 +ok

    类型:列表

    应该有一个电子邮件列表。这可能有助于查看您的问题是否是用户名格式问题。

        5
  •  2
  •   Matt    15 年前

    我写这个是为了从我的收件箱中获取电子邮件。如果有电子邮件,我每天早上都要做一件乏味的工作,所以我写了这段代码来检查我的文件夹中是否有电子邮件标题。 我添加了一点XML创建来展示这种可能性。 它不是交换,但在我的情况下是有效的。 XCData对正文中的任何特殊字符进行编码。 我想我应该指出我要找的电子邮件主题是[票-支持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");
    
    
            }
        }
    }
    

    马特

        6
  •  1
  •   Christian    17 年前
        7
  •  1
  •   Jose Basilio    17 年前

    你需要使用 Exchange SDK 如果在Exchange服务器中未启用POP3。另一个选择是使用 WebDAV .

        8
  •  1
  •   to StackOverflow    17 年前

    另一个选项是将Exchange配置为启用IMAP4。存在用于.NET的第三方IMAP4库,例如Rebex。

        9
  •  1
  •   Christian    17 年前

    可以从Exchange2007开始使用EWS(ExchangeWebServices)。它们似乎总是被安装的,并且比WebDAV更受支持。

    您只需从ExchangeServer导入WebService(在安装目录中搜索asmx文件)。您可以从邮件中下载EML文件,并执行更多操作!

        10
  •  1
  •   Can Gencer    15 年前

    你可以使用 Exchange Web Services (Exchange 2007或2010)或 WebDav (截至2007年和2010年的Exchange不支持WebDAV),但是如果您想快速开发,API和实现可能有点麻烦。

    我用IndependentSoft的图书馆 WebDav Exchange Web Services 过去是成功的,它提供了一个围绕Exchange API的包装器,使用起来更简单。他们也能很好地解析消息和mime。

        11
  •  0
  •   Dmitry Streblechenko    10 年前
    1. 目前首选的API(在Exchange 2013和2016中)是 EWS . 它完全基于HTTP,可以从任何语言访问,但是 .Net Java 特定库。

      你可以使用 EWSEditor 使用API。

    2. Extended MAPI . 这是Outlook使用的本机API。它最终使用msems-exchange-mapi提供程序,该提供程序可以使用rpc(Exchange 2013不再支持它)或rpc-over-http(Exchange 2007或更高版本)或mapi-over-http(Exchange 2013或更高版本)与Exchange通信。

      API本身只能从非托管C++访问。 Delphi . 您也可以使用 Redemption (任何语言)-ITS RDO 对象系列是扩展的MAPI包装。要使用扩展的MAPI,您需要安装Outlook或 standalone (Exchange) version of MAPI (在扩展支持上,它不支持Unicode PST和MSG文件,并且无法访问Exchange 2016)。可在服务中使用扩展的MAPI。

      您可以使用 OutlookSpy MFCMAPI .

    3. Outlook Object Model -不特定于Exchange,但它允许访问运行代码的计算机上Outlook中可用的所有数据。不能在服务中使用。

    4. Exchange Active Sync . 微软不再向该协议投入任何重要资源。

    5. Outlook以前安装CDO1.21库(它包装了扩展的MAPI),但它已被Microsoft弃用,不再接收任何更新。

    6. 以前有一个名为MAPI33的第三方.NET MAPI包装器,但它不再被开发或支持。

    7. WebDAV-已弃用。

    8. 用于Exchange的协作数据对象(CDOEX)-已弃用。

    9. Exchange OLE DB提供程序(ExoleDB)-已弃用。