首页  ·  知识 ·  编程语言
C#访问hotmail邮件
夜星海  http://www.taiwanren.com/blog/  .NET  编辑:dezai  图片来源:网络
如何使用程序来访问HOTMAIL的邮件using System;using System.Diagnostics;using System.IO;using System.Net;using System.Text;using Sys
如何使用程序来访问HOTMAIL的邮件using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using System.Web;namespace Hotmail
{
 ///
 /// HotmailProxy is able to speak HttpMail to various
 /// Hotmail servers across the world.
 ///

 public class HotmailProxy
 {
  ///
  /// Storage area for cookies.
  ///

  private CookieContainer ccContainer;  ///
  /// Creates a new instance of HotmailHttp.
  ///

  public HotmailProxy()
  {
   Trace.WriteLine("Creating new instance.");
   ccContainer = new CookieContainer();
  }  ///
  /// Sends the given request to the given destination
  /// and returns the answer from the server.
  ///

  /// The request to send.
  /// The destination to send the request to.
  /// The answer from the remote host.
  public string SendRequest(string request, Uri destination)
  {
   // pass along
   return SendRequest(request,destination,null);
  }  ///
  /// Sends the given request to the given destination
  /// and returns the answer from the server.
  ///

  /// The request to send.
  /// The destination to send the request to.
  /// The network credentials to use with the request.
  /// The answer from the remote host.
  public string SendRequest(string request, Uri destination, NetworkCredential credential)
  {
   // Verify input
   if(request == null || request.Trim().Length == 0)
    throw new ArgumentNullException("request");
   else if (destination == null)
    throw new ArgumentNullException("destination");
   else
   {
    // Get byte[] and send the request using private method.
    byte[] xmlBytes = Encoding.ASCII.GetBytes(request);
    return SendRequestTo(xmlBytes,destination, credential);
   }
  }  ///
  /// Sends the given byte[] to the given remote host using
  /// authentication with the supplied credentials.
  ///

  ///
  ///
  ///
  ///
  private string SendRequestTo(byte[] requestBytes, Uri destination, NetworkCredential credential)
  {
   Trace.WriteLine("Sending request to url:" + destination.AbsoluteUri);   // Build the request.
   HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(destination);
   webRequest.Method = "PROPFIND";
   webRequest.Accept = "*/*";
   webRequest.AllowAutoRedirect = false;
   webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR v1.1.4322)";
   webRequest.CookieContainer = new CookieContainer();
   webRequest.ContentLength = requestBytes.Length;
   webRequest.ContentType = "text/xml";
   // Set the credentials
   webRequest.Credentials = credential;
   // Add cookies for this request
   webRequest.CookieContainer.Add(ccContainer.GetCookies(destination));   try
   {
    // Write the request
    Stream reqStream = webRequest.GetRequestStream();
    reqStream.Write(requestBytes,0,requestBytes.Length);
    reqStream.Close();
    // Get a response
    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
    if (webRequest.HaveResponse)
    {
     // Handle returned cookies
     foreach(Cookie retCookie in webResponse.Cookies)
     {
      bool cookieFound = false;
     
      foreach(Cookie oldCookie in ccContainer.GetCookies(destination))
      {
       if (retCookie.Name.Equals(oldCookie.Name))
       {
        oldCookie.Value = retCookie.Value;
        cookieFound = true;
       }
      }
      if (!cookieFound)
       ccContainer.Add(retCookie);
     
     }    
     // Handle redirection headers
     if ((webResponse.StatusCode == HttpStatusCode.Found) ||
      (webResponse.StatusCode == HttpStatusCode.Redirect) ||
      (webResponse.StatusCode == HttpStatusCode.Moved) ||
      (webResponse.StatusCode == HttpStatusCode.MovedPermanently))
     {   
      WebHeaderCollection headers = webResponse.Headers;
      return SendRequestTo(requestBytes,new Uri(headers["location"]),credential);
     }
     // Read response
     StreamReader stream = new StreamReader(webResponse.GetResponseStream());
     string responseString = stream.ReadToEnd();
     stream.Close();
  
     return responseString;
    }
    // No response
    throw new ApplicationException("No response received from host.");
   }
   catch(WebException e)
   {
    // Error occured
    Trace.WriteLine("Exception occured " + e.Message);
    throw new ApplicationException("Exception occured while sending request."+e.Message,e);
   }
  
  }
 }
}
using System;
using System.Diagnostics;
using System.Net;
using System.Xml;namespace Hotmail
{
 public class HotmailClient
 {
  #region Class Instances and Properties
  ///
  /// The Http proxy used for Hotmail
  ///

  private HotmailProxy hHttp   = null;
  ///
  /// Indicator if connected to remote host.
  ///

  private bool connected    = false;
  #endregion  #region Public Interface( Constructor, Connect, Disconnect, RetrieveFilledMailBox)
  ///
  /// Create a new instance of HotmailClient.
  ///

  public HotmailClient()
  {
  }  ///
  /// Connect this client to the remote hotmail host and
  /// retrieve a list of mailboxes.
  ///

  /// The username to connect with.
  /// The password to connect with.
  public void Connect(string username, string password)
  {
   // Verify input
   if (username == null || username.Trim().Length == 0)
    throw new ArgumentNullException("username");
   if (password == null || password.Trim().Length == 0)
    throw new ArgumentNullException("password");
   if (username.IndexOf("@") == -1)
    throw new ArgumentException("Illegal username, must contain an @ sign: (user@hotmail.com).");
   // Create the specialized Http client and the neccesary credentials
   hHttp = new HotmailProxy();
   NetworkCredential credentials = new NetworkCredential(username,password,null);
   // Build the query for the msgfolderroot
   string query = "" +
    "    "xmlns:h=‘http://schemas.microsoft.com/hotmail/‘ " +
    "xmlns:hm=‘urn:schemas:httpmail:‘>" +
    ""     +
    "" +
    "
"     +
    "
";
   // Connect
   try
   {
    // Get a response from the query, connect to the hotmail entry page.
    string hotmailEntryPoint = "http://services.msn.com/svcs/hotmail/httpmail.asp";
    string response = hHttp.SendRequest(query, new Uri(hotmailEntryPoint),credentials);
    // Verify response
    if (response == null || response.Trim().Length == 0)
     throw new ApplicationException("No response received from host.");
    // Parse the response, further verifying it.
    Uri folderRootUri = ParseConnectResponse(response);
    // Obtain available folders using folderRootUrl
    RetrieveMailboxes(folderRootUri);
    // Now we‘re connected
    connected = true;
   }
   catch(Exception e)
   {
    // Something went wrong.
    throw new ApplicationException("Exception occured while connecting to remote host.",e);
   }
  }
  #endregion  #region Private Interface( ParseConnectResponse, ParseFillResponse, RetrieveMailboxes, CreateNamespaceManager)  ///
  /// This method parses the response from the server from
  /// the Connect() call, and returns the Uri contained in
  /// the msgfolderroot. This is the root for all mailboxes.
  ///

  /// The response from the remote host.
  /// The Uri contained in msgfolderroot.
  private Uri ParseConnectResponse(string response)
  {
   try
   {
    // Load response into XmlDocument
    XmlDocument dom = new XmlDocument();
    dom.LoadXml(response);
    // Query XmlDocument for msgfolderroot node.
    string xpath = "//hm:msgfolderroot";
    XmlNamespaceManager context = CreateNamespaceManager(dom.NameTable);
    XmlNode folderRoot = dom.SelectSingleNode(xpath,context);
    // Verify node
    if (folderRoot == null)
     throw new ApplicationException("Node ‘" + xpath + "‘ not found.");
    // Get node text and verify,
    string folderRootUrl = folderRoot.InnerText;
    if ((folderRootUrl == null) || (folderRootUrl.Trim().Length == 0))
     throw new ApplicationException("No url found in node ‘" + xpath + "‘.");
    try
    {
     // Return the uri, this may result in a UriFormatException
     return new Uri(folderRootUrl);
    }
    catch
    {
     throw new ApplicationException("Url found in node ‘" + xpath + "‘ is invalid:" + folderRootUrl);
    }
   }
   catch(Exception e)
   {
    // Something went wrong.
    throw new ApplicationException("Error occured while parsing connect response.",e);
   }
  }
 
  ///
  /// Requests available mailboxes from the remote host using the
  /// given msgfolderroot url, and parses the response into
  /// the mailBoxes container.
  ///

  /// The Uri of the msgfolderroot.
  private void RetrieveMailboxes(Uri folderRootUrl)
  {
   try
   {
    // Build the needed query
    string query = "" +
     "" +
     ""    +
     "" +
     ""  +
     "" +
     "" +
     "
"    +
     "
";
    // Get a response from server
    string response = hHttp.SendRequest(query,folderRootUrl);
    // Verify response
    if (response == null || response.Trim().Length == 0)
     throw new ApplicationException("No response received from host.");
    // Load response into XmlDocument
    XmlDocument dom = new XmlDocument();
    dom.LoadXml(response);
    // Query XmlDocument for all mailboxes using XPath
    string xpath = "//D:response";
    XmlNamespaceManager context = CreateNamespaceManager(dom.NameTable);
    XmlNodeList mailBoxNodes = dom.SelectNodes(xpath,context);
    // Parse each node found
    foreach(XmlNode mailBoxNode in mailBoxNodes)
    { 
     try
     {
      // Direct mapping using XPath, should not result in any errors
      // as long as Hotmail keeps it protocol the same.
      string type = mailBoxNode.SelectSingleNode("descendant::hm:special",context).InnerText;    
      string nameUrl = mailBoxNode.SelectSingleNode("descendant::D:href",context).InnerText;
      int visibleCount = Int32.Parse(mailBoxNode.SelectSingleNode("descendant::D:visiblecount",context).InnerText);
      int unreadCount = Int32.Parse(mailBoxNode.SelectSingleNode("descendant::hm:unreadcount",context).InnerText);
      // Add the mailbox
      Console.WriteLine("MailBox found: " + type + "\r\n" +
       "\turl: " + nameUrl + "\r\n" +
       "\tVisible: " + visibleCount + "\r\n" +
       "\tUnread: " + unreadCount + "\r\n");     }
     catch(Exception e)
     {
      Console.WriteLine("Exception occured while obtaining mailbox info: " + e.Message);
     }
    }
   }
   catch(Exception e)
   {
    // Something went wrong.
    throw new ApplicationException("Error occured while retrieving available mailboxes.",e);
   }
  }
 
  ///
  /// Helper method to create the XmlNamespaceManager needed for
  /// correctly querying the response using XPath.
  ///

  ///
  ///
  private XmlNamespaceManager CreateNamespaceManager(XmlNameTable table)
  {
   XmlNamespaceManager m = new XmlNamespaceManager(table);
   m.AddNamespace("hm","urn:schemas:httpmail:");
   m.AddNamespace("D","DAV:");
   m.AddNamespace("m","urn:schemas:mailheader:");
   m.AddNamespace("c","urn:schemas:contacts:");
   m.AddNamespace("h","http://schemas.microsoft.com/hotmail/");
   return m;
  }
  #endregion }
}
程序进入点private void button1_Click(object sender, System.EventArgs e)
  {
   HotmailClient client=new HotmailClient();
   client.Connect("xxxx@hotmail.com","urPassword");
  } 本文作者:夜星海 来源:http://www.taiwanren.com/blog/
CIO之家 www.ciozj.com 微信公众号:imciow
    >>频道首页  >>网站首页   纠错  >>投诉
版权声明:CIO之家尊重行业规范,每篇文章都注明有明确的作者和来源;CIO之家的原创文章,请转载时务必注明文章作者和来源;
延伸阅读