PDA

View Full Version : نمونه کد دریافت ایمیل با کمی اشکال



negar.rafie
دوشنبه 07 اسفند 1391, 17:32 عصر
سلام دوستان
من این نمونه کد را برای دریافت ایمیل پیدا کردم که فکر کنم کار میده

using System;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;
namespace Isrcomputing
{
class ReadingMailWebDAV
{
public const string strServer = ""; // Exchange server name
public const string strPassword = ""; // Account Domain Password
public const string strDomain = ""; // Domain
public const string strAlias = ""; // username


[STAThread]
static void Main(string[] args)
{
System.Net.HttpWebRequest SEARCHRequest;
System.Net.WebResponse SEARCHResponse;
System.Net.CredentialCache MyCredentialCache;
string strRootURI = "";
string strQuery = "";
byte[] bytes = null;
System.IO.Stream SEARCHRequestStream = null;

try
{
// Build the mailbox URI.
strRootURI = "https://" + strServer + "/exchange/" + strAlias + "/Inbox/";
MyCredentialCache = new System.Net.CredentialCache();
MyCredentialCache.Add(new System.Uri(strRootURI),
"NTLM",
new System.Net.NetworkCredential(strAlias, strPassword, strDomain)
);

// Build the SQL query.
strQuery = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D = \"DAV:\">"
+ "<D:sql>SELECT \"urn:schemas:httpmail:sendername\" , \"urn:schemas:httpmail:subject\","
+ " \"urn:schemas:mailheader:from\", \"urn:schemas:httpmail:datereceived\" ,"
+ " \"urn:schemas:httpmail:date\", \"urn:schemas:httpmail:textdescription\" ,"
+ " \"urn:schemas:httpmail:htmldescription\", \"DAV:id\""
+ ", \"DAV:href\""
+ " FROM \"" + strRootURI + "\""
+ " WHERE \"DAV:ishidden\" = false AND \"DAV:isfolder\" = false"
+ " </D:sql></D:searchrequest>";
// Create a new CredentialCache object and fill it with the network
// credentials required to access the server.
SEARCHRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(s trRootURI);

ServicePointManager.ServerCertificateValidationCal lback = delegate(object sender, System.Security.Cryptography.X509Certificates.X509 Certificate pCertificate, System.Security.Cryptography.X509Certificates.X509 Chain pChain, System.Net.Security.SslPolicyErrors pSSLPolicyErrors)
{
return true;
};

SEARCHRequest.Credentials = MyCredentialCache;
SEARCHRequest.Method = "SEARCH";
SEARCHRequest.ContentType = "text/xml";
bytes = Encoding.UTF8.GetBytes((string)strQuery);
SEARCHRequest.ContentLength = bytes.Length;
SEARCHRequestStream = SEARCHRequest.GetRequestStream();
SEARCHRequestStream.Write(bytes, 0, bytes.Length);
SEARCHRequestStream.Close();

SEARCHResponse = SEARCHRequest.GetResponse();
System.Text.Encoding enc = System.Text.Encoding.Default;
if (SEARCHResponse == null)
{
Console.WriteLine("Response returned NULL!");
}
Console.WriteLine(SEARCHResponse.ContentLength);
System.IO.StreamReader sr = new System.IO.StreamReader(SEARCHResponse.GetResponseS tream(), System.Text.Encoding.ASCII);
string sa = sr.ReadToEnd();
sr.Close();
SEARCHResponse.Close();

XmlDocument doc = new XmlDocument();
doc.InnerXml = sa;
XmlNodeList mailId = doc.GetElementsByTagName("a:id");
XmlNodeList mailFrom = doc.GetElementsByTagName("e:from");
XmlNodeList mailReceivedDate = doc.GetElementsByTagName("d:datereceived");
XmlNodeList mailSubject = doc.GetElementsByTagName("d:subject");
XmlNodeList mailText = doc.GetElementsByTagName("d:textdescription");
Console.WriteLine(mailId.Count);

for (int i = 0; i < mailId.Count; ++i)
{
Console.WriteLine("-------------------------------\n{0}: E-mail ID: {1}", i + 1, mailId[i].InnerText);
Console.WriteLine("From:\t{0}", mailFrom[i].InnerText);
Console.WriteLine("Received:\t{0}", mailReceivedDate[i].InnerText);
Console.WriteLine("Subject:\t{0}", mailSubject[i].InnerText);
Console.WriteLine("Content:\n{0}", mailText[i].InnerText);
}

}
catch (Exception ex)
{
// Catch any exceptions. Any error codes from the PUT
// or MOVE method requests on the server will be caught
// here, also.
Console.WriteLine("Problem: {0}", ex.Message);
}
}
}
}


حالا مثلا برای جیمیل یا یاهو موارد زیر را باید چی قرار بدم

public const string strServer = ""; // Exchange server name
public const string strPassword = ""; // Account Domain Password
public const string strDomain = ""; // Domain
public const string strAlias = ""; // username

negar.rafie
سه شنبه 08 اسفند 1391, 08:54 صبح
???????????????????????????????????????