在 C# 中访问交换电子邮件

发布于 2024-07-19 04:53:35 字数 219 浏览 4 评论 0 原文

你知道有没有办法吗?

我已经使用 此库 访问 pop3 服务器,但它没有不能与交换服务器一起使用。

您是否知道任何其他库或代码可以向我展示如何做到这一点?

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

Do you know if there's a way?

I've used this library to access a pop3 server, but it doesn't work with an exchange server.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

饭团 2024-07-26 04:53:36

您需要使用 Exchange SDK(如果 Exchange 服务器中未启用 POP3)。 另一种选择是使用 WebDAV

You need to use the Exchange SDK if POP3 is not enabled in the Exchange Server. Another options is to use WebDAV.

谁的年少不轻狂 2024-07-26 04:53:36

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

Another option is to configure Exchange to enable IMAP4. There exist 3rd party IMAP4 libraries for .NET, e.g. rebex.

撩发小公举 2024-07-26 04:53:36

从 Exchange 2007 开始,您可以使用 EWS(Exchange Web 服务)。它们似乎总是被安装,并且比 Webdav 得到更好的支持。

您只需从 Exchange 服务器导入 Web 服务即可(在安装目录中搜索 asmx 文件)。 您可以从邮件中下载 EML 文件并执行更多操作!

You could use EWS (Exchange webservices) starting from Exchange 2007. They seem to be always installed and are better supported than 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!

白昼 2024-07-26 04:53:36

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

我已将 IndependentSoft 的库用于 WebDavExchange Web Services 过去曾取得成功,它提供了 Exchange API 的包装器,并且使用起来更加简单。 他们也很好地处理消息和 MIME 的解析。

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.

I have used IndependentSoft's libraries for WebDav and Exchange Web Services successfully 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.

私藏温柔 2024-07-26 04:53:35

如果您使用 Exchange 2007 并启用了 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”变量将是从交换返回的 ItemType 对象的数组,这些对象具有您期望的邮件的所有属性消息(正文、附件等)。 我希望这有帮助!

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 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!

霊感 2024-07-26 04:53:35

取决于 Exchange 版本。 WebDAV 适用于 2000 年至 2007 年,但 < a href="http://msdn.microsoft.com/en-us/library/aa565934.aspx" rel="nofollow noreferrer">Web 服务 需要 2007 年以上版本。

这些可能是最容易开始工作的。 CDO 是另一种选择,但 C# 不支持它 - 所以你必须去 程序外

Exchange 还有一个 OLEDB 提供程序,但我从未使用过它 - 不过,.NET 支持它

Depends on the Exchange version. WebDAV works with 2000 thru 2007, but Web Services requires 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.

Exchange also has an OLEDB provider, but I've never used it - it is supported from .NET, however.

眼泪淡了忧伤 2024-07-26 04:53:35

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

我已经摸索了几天了,得出的结论是,最好围绕 API 构建我自己的包装类,使服务可以在一两行代码中使用,而不是在带有目前的实施。

你猜怎么了? Microsoft 抢先一步:Exchange Web Services Managed API 的第一个候选版本可供下载 此处

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

Using EWS directly in managed code (VB.net / C#) is clumsy to say the best.

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.

Guess what? Microsoft have beaten me to it: Exchange Web Services Managed API's first Release Candidate is available for download here.

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.

哽咽笑 2024-07-26 04:53:35

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

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

通过 telnet 进行测试的步骤

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

类型:CAPA
这应该返回您的交换服务器支持的功能列表。
CAPA
+OK 能力列表如下
顶部
用户
管道系统
永不过期
UIDL
SASL NTLM

请注意,我的没有显示 PLAIN

这是来自电子邮件服务器的响应,该服务器确实+OK Dovecot 已准备就绪。
CAPA
+确定
CAPA
顶部
UIDL
RESP-代码
管道系统
STLS
用户
SASL 平原

如果您的响应不包含 PLAIN,则停止,因为您需要一个支持 SPA

类型的库:user myusername
或者
类型: user [email protected] 将 domain.corp 替换为您的域名

您应该然后收到
+OK

输入:pass mypass

您应该得到响应
+OK

type: list

应获取电子邮件列表。 这可能有助于确定您的问题是否是用户名格式问题。

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.

If that is the case then look for a library that supports NTLM.

Steps for testing via 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.

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
.

Notice that mine does not show 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
.

If your response does not include PLAIN then stop as you need a library that supports SPA

type: user myusername
OR
type: user [email protected] replacing domain.corp with your domain

You should then receive
+OK

Type: pass mypass

You should get a response
+OK

type: list

Should get a list of emails. This might help see if your issue is a username format issue.

惯饮孤独 2024-07-26 04:53:35

我写这篇文章是为了从我的收件箱中获取电子邮件。 如果有电子邮件,我每天早上都有一项繁琐的任务要做,所以我编写了这段代码来检查我的文件夹中是否有电子邮件标题。
我添加了一点 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

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]

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

如梦亦如幻 2024-07-26 04:53:35

你可以使用这个库:
http://www.dimastr.com/redemption/

You could use this library:
http://www.dimastr.com/redemption/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文