如题:
公司邮箱目前使用的是Exchange 2007。通过 OutLook Web Access 客户端登录邮箱可以正常使用。现在有一个需求是:写一个windows 程序,使用指定的用户账户和密码登录Exchange Server 端,读取邮件的相关信息。
遇到的问题是:
在邮箱系统升级到2007 之前,使用的是Exchange 2003。 我根据网上的查到的资料写了一段代码,可以从Exchange Server 端获取指定用户的 邮件信息。当邮件系统升级到2007 之后,总是出现 "远程服务器返回错误: (440) Login Timeout。"错误。
特向各位XDJM 求救!如果大家有好方法或例子也请拿出来分享一下~关键代码如下:
private void button1_Click(object sender, EventArgs e)
{
System.Net.HttpWebRequest Request;
System.Net.WebResponse Response;
System.Net.CredentialCache MyCredentialCache;
string strRootURI = @"https://mail.abc.cn/owa/";
string strUserName = "zhangsan";
string strPassword = "123456";
string strDomain = "domino";
string strQuery = "";
byte[] bytes = null;
System.IO.Stream RequestStream = null;
System.IO.Stream ResponseStream = null;
XmlDocument ResponseXmlDoc = null;
try
{
// Build the SQL query.
strQuery = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D = \"DAV:\" >"
+ "<D:sql>SELECT \"urn:schemas:httpmail:importance\",\"urn:schemas:httpmail:from\",\"urn:schemas:httpmail:read\",\"urn:schemas:httpmail:datereceived\",\"urn:schemas:httpmail:subject\",\"urn:schemas:httpmail:hasattachment\","
+ "\"DAV:contentclass\",\"DAV:getcontentlength\",\"DAV:displayname\""
+ "FROM \"" + strRootURI + "\""
+ "WHERE \"DAV:ishidden\" = false AND \"DAV:isfolder\" = false"
+ "</D:sql></D:searchrequest>";
MyCredentialCache = new System.Net.CredentialCache();
MyCredentialCache.Add(new System.Uri(strRootURI),
"Basic",
new System.Net.NetworkCredential(strUserName, strPassword)
); Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strRootURI);
Request.Credentials = MyCredentialCache; // Specify the method.
Request.Method = "SEARCH"; Request.ContentType = "application/x-www-form-urlencoded";
// Encode the body using UTF-8.
bytes = Encoding.UTF8.GetBytes((string)strQuery); Request.ContentLength = bytes.Length; ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
// Get a reference to the request stream.
RequestStream = Request.GetRequestStream(); // Write the SQL query to the request stream.
RequestStream.Write(bytes, 0, bytes.Length); // Close the Stream object to release the connection
// for further use.
RequestStream.Close(); // Set the content type header.
Request.ContentType = "text/xml"; // Send the SEARCH method request and get the
// response from the server.
Response = (HttpWebResponse)Request.GetResponse();
ResponseStream = Response.GetResponseStream();
//运行到此处就出现异常"远程服务器返回错误: (440) Login Timeout。" // Create the XmlDocument object from the XML response stream.
ResponseXmlDoc = new XmlDocument();
ResponseXmlDoc.Load(ResponseStream); XmlNodeList idNodes = ResponseXmlDoc.GetElementsByTagName("a:displayname");
XmlNodeList SenderNodes = ResponseXmlDoc.GetElementsByTagName("d:from");
XmlNodeList importanceNodes = ResponseXmlDoc.GetElementsByTagName("d:importance");
XmlNodeList contextclassNodes = ResponseXmlDoc.GetElementsByTagName("a:contentclass");
XmlNodeList readNodes = ResponseXmlDoc.GetElementsByTagName("d:read");
XmlNodeList datareceiveNodes = ResponseXmlDoc.GetElementsByTagName("d:datereceived");
XmlNodeList subjectNodes = ResponseXmlDoc.GetElementsByTagName("d:subject");
XmlNodeList getcontentlengthNodes = ResponseXmlDoc.GetElementsByTagName("a:getcontentlength");
XmlNodeList hasattachmentNodes = ResponseXmlDoc.GetElementsByTagName("d:hasattachment");
int j = 0;
if (idNodes.Count > 0)
{
label2.Text = "共" + idNodes.Count + "封邮件";
for (int i = 0; i < idNodes.Count; i++)
{
// label1.Text = idNodes[i].InnerText + "/n" + label1.Text;
if (hasattachmentNodes[i].InnerText == "1")
{
.......
}
label3.Text = "其中分离了附件的邮件有" + j + "封";
}
ResponseStream.Close();
Response.Close();
}
catch (Exception ex)
{
}
}
怎么不能开200分啊,如果问题解决,另散200分!
公司邮箱目前使用的是Exchange 2007。通过 OutLook Web Access 客户端登录邮箱可以正常使用。现在有一个需求是:写一个windows 程序,使用指定的用户账户和密码登录Exchange Server 端,读取邮件的相关信息。
遇到的问题是:
在邮箱系统升级到2007 之前,使用的是Exchange 2003。 我根据网上的查到的资料写了一段代码,可以从Exchange Server 端获取指定用户的 邮件信息。当邮件系统升级到2007 之后,总是出现 "远程服务器返回错误: (440) Login Timeout。"错误。
特向各位XDJM 求救!如果大家有好方法或例子也请拿出来分享一下~关键代码如下:
private void button1_Click(object sender, EventArgs e)
{
System.Net.HttpWebRequest Request;
System.Net.WebResponse Response;
System.Net.CredentialCache MyCredentialCache;
string strRootURI = @"https://mail.abc.cn/owa/";
string strUserName = "zhangsan";
string strPassword = "123456";
string strDomain = "domino";
string strQuery = "";
byte[] bytes = null;
System.IO.Stream RequestStream = null;
System.IO.Stream ResponseStream = null;
XmlDocument ResponseXmlDoc = null;
try
{
// Build the SQL query.
strQuery = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D = \"DAV:\" >"
+ "<D:sql>SELECT \"urn:schemas:httpmail:importance\",\"urn:schemas:httpmail:from\",\"urn:schemas:httpmail:read\",\"urn:schemas:httpmail:datereceived\",\"urn:schemas:httpmail:subject\",\"urn:schemas:httpmail:hasattachment\","
+ "\"DAV:contentclass\",\"DAV:getcontentlength\",\"DAV:displayname\""
+ "FROM \"" + strRootURI + "\""
+ "WHERE \"DAV:ishidden\" = false AND \"DAV:isfolder\" = false"
+ "</D:sql></D:searchrequest>";
MyCredentialCache = new System.Net.CredentialCache();
MyCredentialCache.Add(new System.Uri(strRootURI),
"Basic",
new System.Net.NetworkCredential(strUserName, strPassword)
); Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strRootURI);
Request.Credentials = MyCredentialCache; // Specify the method.
Request.Method = "SEARCH"; Request.ContentType = "application/x-www-form-urlencoded";
// Encode the body using UTF-8.
bytes = Encoding.UTF8.GetBytes((string)strQuery); Request.ContentLength = bytes.Length; ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
// Get a reference to the request stream.
RequestStream = Request.GetRequestStream(); // Write the SQL query to the request stream.
RequestStream.Write(bytes, 0, bytes.Length); // Close the Stream object to release the connection
// for further use.
RequestStream.Close(); // Set the content type header.
Request.ContentType = "text/xml"; // Send the SEARCH method request and get the
// response from the server.
Response = (HttpWebResponse)Request.GetResponse();
ResponseStream = Response.GetResponseStream();
//运行到此处就出现异常"远程服务器返回错误: (440) Login Timeout。" // Create the XmlDocument object from the XML response stream.
ResponseXmlDoc = new XmlDocument();
ResponseXmlDoc.Load(ResponseStream); XmlNodeList idNodes = ResponseXmlDoc.GetElementsByTagName("a:displayname");
XmlNodeList SenderNodes = ResponseXmlDoc.GetElementsByTagName("d:from");
XmlNodeList importanceNodes = ResponseXmlDoc.GetElementsByTagName("d:importance");
XmlNodeList contextclassNodes = ResponseXmlDoc.GetElementsByTagName("a:contentclass");
XmlNodeList readNodes = ResponseXmlDoc.GetElementsByTagName("d:read");
XmlNodeList datareceiveNodes = ResponseXmlDoc.GetElementsByTagName("d:datereceived");
XmlNodeList subjectNodes = ResponseXmlDoc.GetElementsByTagName("d:subject");
XmlNodeList getcontentlengthNodes = ResponseXmlDoc.GetElementsByTagName("a:getcontentlength");
XmlNodeList hasattachmentNodes = ResponseXmlDoc.GetElementsByTagName("d:hasattachment");
int j = 0;
if (idNodes.Count > 0)
{
label2.Text = "共" + idNodes.Count + "封邮件";
for (int i = 0; i < idNodes.Count; i++)
{
// label1.Text = idNodes[i].InnerText + "/n" + label1.Text;
if (hasattachmentNodes[i].InnerText == "1")
{
.......
}
label3.Text = "其中分离了附件的邮件有" + j + "封";
}
ResponseStream.Close();
Response.Close();
}
catch (Exception ex)
{
}
}
怎么不能开200分啊,如果问题解决,另散200分!
如果服务器启用了SSL 方式,例如 地址变为: https://abc.cn
使用ping https://abc.cn 就ping 不通了呢?
能不能解释清楚点呢?本人对Exchange 不太了解。
以上这段代码所在服务器必须在与Exchange 相同的域中,或者以域账号身份运行
http://msdn2.microsoft.com/en-us/library/bb856559.aspx
http://msdn2.microsoft.com/en-us/library/aa563373.aspx
http://msdn2.microsoft.com/en-us/library/bb408417.aspx
http://msdn2.microsoft.com/en-us/library/bb421489.aspx
http://msdn2.microsoft.com/en-us/library/bb508825.aspx