首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 存取 Hotmail 完整實作(一):HTTPMail 協議、XMLHTTP 連線、資料夾 URL 取得與 XML 解析

     編輯:關於C語言

前言

Hotmail 不提供標準 POP3 存取,但 Outlook Express 可直接連線,其使用HTTPMail 協議(基於 WebDAV 的 HTTP/1.1 擴展)。本文為系列第一部分,示範 C# 透過 MSXML2.XMLHTTP 連線 Hotmail、取得收件箱/發件箱 URL。

一、HTTPMail 閘道與驗證

  • 預設閘道:https://services.msn.com/svcs/hotmail/httpmail.asp
  • WebDAV 服務,需 HTTP/1.1 基本驗證
  • XMLHTTP 元件自動處理:重導向、負載平衡、Cookie、驗證交握

二、第一步:PROPFIND 取得資料夾屬性

查詢 XML

<?xml version="1.0"?>
<D:propfind xmlns:D="DAV:" xmlns:h="https://schemas.microsoft.com/hotmail/" xmlns:hm="urn:schemas:httpmail:">
  <D:prop>
    <h:adbar/>           <!-- 廣告列 -->
    <hm:contacts/>       <!-- 通訊錄 -->
    <hm:inbox/>          <!-- 收件箱 -->
    <hm:outbox/>         <!-- 發件箱 -->
    <hm:sendmsg/>        <!-- 發送郵件端點 -->
    <hm:sentitems/>      <!-- 已寄出 -->
    <hm:deleteditems/>   <!-- 刪除項目 -->
    <hm:drafts/>         <!-- 草稿 -->
    <hm:msgfolderroot/>  <!-- 根資料夾 -->
    <h:maxpoll/>         <!-- 最大輪詢間隔 -->
    <h:sig/>             <!-- 簽名 -->
  </D:prop>
</D:propfind>

C# 發送請求

using MSXML2; // 參考 Microsoft XML, v6.0

XMLHTTP xmlHttp = new XMLHTTP();
string serverUrl = "https://services.msn.com/svcs/hotmail/httpmail.asp";
string username = "your@hotmail.com";
string password = "yourpassword";

string query = "..."; // 上方 XML

// 同步請求(false),提供帳密進行基本驗證
xmlHttp.open("PROPFIND", serverUrl, false, username, password);
xmlHttp.setRequestHeader("Content-Type", "text/xml");
xmlHttp.send(query);

string responseXml = xmlHttp.responseText;

關鍵點

  • open 最後兩參數提供帳密,避免彈出登入對話框
  • false = 同步阻塞,適合主控台/服務;GUI 應用建議 true 非同步
  • XMLHTTP 自動處理 302 重導向、Cookie、驗證挑戰

三、解析回應 XML:取得 Inbox 與 SendMsg URL

回應 XML 範例

<?xml version="1.0" encoding="Windows-1252"?>
<D:multistatus>
  <D:response>
    <D:propstat>
      <D:prop>
        <hm:inbox>https://law15.oe.hotmail.com/.../Inbox/</hm:inbox>
        <hm:sendmsg>https://law15.oe.hotmail.com/.../SendMsg/</hm:sendmsg>
        <hm:sentitems>https://.../SentItems/</hm:sentitems>
        <hm:deleteditems>https://.../DeletedItems/</hm:deleteditems>
        <hm:msgfolderroot>https://.../</hm:msgfolderroot>
      </D:prop>
    </D:propstat>
  </D:response>
</D:multistatus>

XmlTextReader 快速解析

string inboxUrl = null, sendUrl = null;

using (var reader = new StringReader(responseXml))
using (var xml = new XmlTextReader(reader))
{
    while (xml.Read())
    {
        if (xml.NodeType == XmlNodeType.Element)
        {
            string name = xml.Name;
            if (name == "hm:inbox" || name == "hm:sendmsg")
            {
                xml.Read(); // 移到文字內容
                if (name == "hm:inbox") inboxUrl = xml.Value;
                else if (name == "hm:sendmsg") sendUrl = xml.Value;
            }
        }
    }
}

Console.WriteLine($"Inbox: {inboxUrl}");
Console.WriteLine($"SendMsg: {sendUrl}");

XmlTextReader 優勢

  • 順向唯讀,記憶體效率極高
  • 無需載入完整 DOM 樹
  • 適合已知結構的單次解析

四、完整程式碼架構

public class HotmailClient
{
    private XMLHTTP _xmlHttp = new XMLHTTP();
    private string _inboxUrl, _sendUrl;
    
    public void Connect(string username, string password)
    {
        string serverUrl = "https://services.msn.com/svcs/hotmail/httpmail.asp";
        string query = BuildFolderQuery();
        
        _xmlHttp.open("PROPFIND", serverUrl, false, username, password);
        _xmlHttp.setRequestHeader("Content-Type", "text/xml");
        _xmlHttp.send(query);
        
        ParseFolderResponse(_xmlHttp.responseText);
    }
    
    private string BuildFolderQuery() { /* 返回上方 XML 字串 */ }
    
    private void ParseFolderResponse(string xml) { /* 上方解析邏輯 */ }
    
    public string InboxUrl => _inboxUrl;
    public string SendUrl => _sendUrl;
}

五、必要參考與部署注意

  • 專案 → 加入參考 → COM → Microsoft XML, v6.0 (MSXML2)
  • 目標機器需安裝 MSXML 6.0(Windows XP SP3+ 內建)
  • 網路需可連線 services.msn.com 及熱導向後的伺服器

六、安全性與現代化建議

風險說明建議
明文密碼基本驗證傳輸 Base64 編碼密碼務必使用 HTTPS(舊閘道可能不支援)
協議非公開HTTPMail 無官方文件,隨時可能變更遷移至 Microsoft Graph API / IMAP
XMLHTTP 元件COM 元件,需註冊、32/64 位元相容性改用 HttpClient + 手動 WebDAV 實作

總結

第一步核心成果:取得 _inboxUrl(收件箱)與 _sendUrl(發送端點)。後續操作(列舉郵件、接收、發送)均依賴這兩個 URL。理解 HTTPMail = WebDAV + Hotmail 擴展,是整合舊系統的關鍵。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved