从 Exchange 获取电子邮件附件时遇到问题
我收到错误消息“远程服务器返回错误:(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有 2 种可能的解决方案:
尝试使用 POP3 协议而不是 HTTP。您可以尝试自己实现这一点(请参阅“如何 POP3例如,在 C# 中”),或者您可以使用支持 SSL 的现成 POP3 库(POP3Client 例如)或看看这个问题
您的错误也可能是因为未处理 https 连接。尝试添加此代码:
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
这是类的实现:
There are 2 possible solutions:
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
Also your error probably because of not handling https connections. Try to add this code:
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
Here is class implementation:
您似乎正在针对 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.