开发者

Trouble getting email attachment from Exchange

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();
    }
}


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜