从 Exchange 获取电子邮件附件时遇到问题

发布于 2024-08-29 13:02:22 字数 1153 浏览 4 评论 0原文

我收到错误消息“远程服务器返回错误:(501) 未实现。”当我尝试使用 HttpWebRequest.GetResponse() 使用 GET 方法从 Exchange 获取电子邮件附件时。我尝试更改 HttpVersion,但不认为这是权限问题,因为我可以搜索收件箱。

我知道我的凭据是正确的,因为它们用于使用 HttpWebRequest.Method = 在收件箱上搜索来获取 HREF (https://mail.mailserver.com/exchange/testemailaccount/Inbox/)。

HREF = https://mail.mailserver.com/exchange/ testemailaccount/Inbox/testemail.EML/attachment.csv

示例代码:

HttpWebRequest req = (System.Net.HttpWebRequest)  HttpWebRequest.CreateHREF);                
req.Method = "GET";
req.Credentials = this.mCredentialCache;
string data = string.Empty;
using (WebResponse resp = req.GetResponse())
{
    Encoding enc = Encoding.Default;
    if (resp == null)
    {
        throw new Exception("Response contains no information.");
    }

    using (StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.ASCII))
    {
        data = sr.ReadToEnd();
    }
}

I am getting the error message “The remote server returned an error: (501) Not Implemented.” when I try to use the HttpWebRequest.GetResponse() using the GET Method to get an email attachment from exchange. I have tried to change the HttpVersion and don’t think it is a permissions issue since I can search the inbox.

I know my credentials are correct as they are used to get HREF using the HttpWebRequest.Method = Search on the inbox (https://mail.mailserver.com/exchange/testemailaccount/Inbox/).

HREF = https://mail.mailserver.com/exchange/testemailaccount/Inbox/testemail.EML/attachment.csv

Sample Code:

HttpWebRequest req = (System.Net.HttpWebRequest)  HttpWebRequest.CreateHREF);                
req.Method = "GET";
req.Credentials = this.mCredentialCache;
string data = string.Empty;
using (WebResponse resp = req.GetResponse())
{
    Encoding enc = Encoding.Default;
    if (resp == null)
    {
        throw new Exception("Response contains no information.");
    }

    using (StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.ASCII))
    {
        data = sr.ReadToEnd();
    }
}

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

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

发布评论

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

评论(2

瑶笙 2024-09-05 13:02:22

有 2 种可能的解决方案:

  1. 尝试使用 POP3 协议而不是 HTTP。您可以尝试自己实现这一点(请参阅“如何 POP3例如,在 C# 中”),或者您可以使用支持 SSL 的现成 POP3 库(POP3Client 例如)或看看这个问题

  2. 您的错误也可能是因为未处理 https 连接。尝试添加此代码:

    ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();

这是类的实现:

internal class AcceptAllCertificatePolicy : ICertificatePolicy
{
    public AcceptAllCertificatePolicy()
    {
    }
    public bool CheckValidationResult(ServicePoint sPoint, X509Certificate cert,
    WebRequest wRequest, int certProb)
    {
        //Allways accept
        return true;
    }
}

There are 2 possible solutions:

  1. Try to use POP3 protocol instead of HTTP. You can try to implement this yourself (see "How to POP3 in C#" for example) or you can use ready-made POP3 library with SSL support (POP3Client for example) or take a look on this question

  2. Also your error probably because of not handling https connections. Try to add this code:

    ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();

Here is class implementation:

internal class AcceptAllCertificatePolicy : ICertificatePolicy
{
    public AcceptAllCertificatePolicy()
    {
    }
    public bool CheckValidationResult(ServicePoint sPoint, X509Certificate cert,
    WebRequest wRequest, int certProb)
    {
        //Allways accept
        return true;
    }
}
滿滿的愛 2024-09-05 13:02:22

您似乎正在针对 Exchange 2007 使用 WebDAV。默认情况下,在 Exchange 2007 中,不启用 WebDAV。因此您可以:

1) 在 Exchange 2007 Server 上启用 WebDAV。

2) 切换到使用 Exchange Web 服务。

我会推荐选项 2,因为您使用的是 C#,因为有 托管 EWS API 使此类任务比使用 WebDAV 简单得多。它还允许您最终以已完全删除 WebDAV 的 Exchange 2010 为目标。

It appears you are using WebDAV against Exchange 2007. By default in Exchange 2007, WebDAV isn't enabled. So you can either:

1) Enable WebDAV on your Exchange 2007 Server.

2) Switch to using Exchange Web Services.

I would recommend Option 2 since you are using C# since there is the Managed EWS API which make this kind of task much simpler than using WebDAV. It also allows you to eventually target Exchange 2010 where WebDAV has been removed completely.

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