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

C++ Builder 實作 Web 郵件收信系統

     編輯:關於C++

早期免費信箱(如 163、長通飛華)提供的 Web 收信功能多以 CGI 實現,隨著 ISAPI/NSAPI 技術成熟,以 DLL 形式運行於 Web 伺服器進程內,效能大幅提升。本文示範使用 C++ Builder 4 開發 ISAPI 架構的 Web 郵件收信系統,包含前端 HTML 介面與後端 DLL 邏輯。

系統架構

  • index.htm:放置於 Web 根目錄(如 C:\Inetpub\wwwroot),提供輸入主機、連接埠、帳號、密碼的表單。
  • ReadMail.dll:ISAPI 延伸模組,放置於 Scripts 目錄(如 C:\Inetpub\scripts),負責連接 POP3 伺服器、產生郵件列表與郵件內容 HTML。

前端介面:index.htm

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>郵件查看</title>
</head>
<body bgcolor="#FFFFFF">
<form action="/scripts/ReadMail.dll/MailList" method="POST">
<p>主機:<input type="text" size="20" name="Host"></p>
<p>連接埠:<input type="text" size="20" name="Port"></p>
<p>用戶名:<input type="text" size="20" name="Name"></p>
<p>密碼:<input type="password" size="20" name="Code"></p>
<p><input type="submit" name="B1" value="提交"><input type="reset" name="B2" value="重置"></p>
</form>
</body>
</html>

後端核心邏輯:ReadMail.dll

WebModule 變數宣告

AnsiString HostName, HostPort, UserName, UserCode;
TStrings *URLData;      // HTTP 參數
bool ConnectFlag;       // 連線狀態旗標
int EmailOrder;         // 郵件序號

動作項 1:/MailList — 顯示郵件列表

void __fastcall TWebModule1::WebModule1WebActionItem1Action(
    TObject *Sender, TWebRequest *Request, TWebResponse *Response, bool &Handled)
{
    // 1. 取得 HTTP 參數
    URLData = NULL;
    switch (Request->MethodType)
    {
        case mtPost: URLData = Request->ContentFields; break;
        case mtGet:  URLData = Request->QueryFields;  break;
    }
    HostName = URLData->Values["Host"];
    HostPort = URLData->Values["Port"];
    UserName = URLData->Values["Name"];
    UserCode = URLData->Values["Code"];

    // 2. 設定 POP3 元件並連線
    NMPOP31->AttachFilePath = ".";           // 附件儲存路徑
    NMPOP31->DeleteOnRead = false;           // 不刪除伺服器郵件
    NMPOP31->ReportLevel = Status_Basic;
    NMPOP31->TimeOut = 20000;
    NMPOP31->Host = HostName;
    NMPOP31->Port = StrToInt(HostPort);
    NMPOP31->UserID = UserName;
    NMPOP31->Password = UserCode;
    ConnectFlag = true;
    NMPOP31->Connect();

    // 3. 產生郵件列表 HTML
    PageProducer1->HTMLDoc->Clear();
    PageProducer1->HTMLDoc->Add("");
    if (ConnectFlag)
    {
        PageProducer1->HTMLDoc->Add("郵件總數:" + IntToStr(NMPOP31->MailCount));
        if (NMPOP31->MailCount > 0)
        {
            for (int i = 1; i <= NMPOP31->MailCount; i++)
            {
                PageProducer1->HTMLDoc->Add("" + IntToStr(i) + "
"); } } } else PageProducer1->HTMLDoc->Add("連接伺服器失敗!"); PageProducer1->HTMLDoc->Add(""); // 4. 回應瀏覽器 Response->Content = PageProducer1->Content(); NMPOP31->Disconnect(); }

動作項 2:/ReadMail — 顯示指定郵件內容

void __fastcall TWebModule1::WebModule1WebActionItem2Action(
    TObject *Sender, TWebRequest *Request, TWebResponse *Response, bool &Handled)
{
    // 1. 取得郵件序號
    URLData = NULL;
    switch (Request->MethodType)
    {
        case mtPost: URLData = Request->ContentFields; break;
        case mtGet:  URLData = Request->QueryFields;  break;
    }
    EmailOrder = StrToInt(URLData->Values["Mx"]);

    // 2. 重新連線(ISAPI 無狀態,需再次認證)
    NMPOP31->AttachFilePath = ".";
    NMPOP31->DeleteOnRead = false;
    NMPOP31->ReportLevel = Status_Basic;
    NMPOP31->TimeOut = 20000;
    NMPOP31->Host = HostName;
    NMPOP31->Port = StrToInt(HostPort);
    NMPOP31->UserID = UserName;
    NMPOP31->Password = UserCode;
    ConnectFlag = true;
    NMPOP31->Connect();

    // 3. 產生郵件內容 HTML
    PageProducer1->HTMLDoc->Clear();
    PageProducer1->HTMLDoc->Add("");
    if (ConnectFlag)
    {
        PageProducer1->HTMLDoc->Add("第 " + IntToStr(EmailOrder) + " 封郵件內容:
"); NMPOP31->GetMailMessage(EmailOrder); PageProducer1->HTMLDoc->AddStrings(NMPOP31->MailMessage->Head); PageProducer1->HTMLDoc->AddStrings(NMPOP31->MailMessage->Body); } else PageProducer1->HTMLDoc->Add("連接伺服器失敗!"); PageProducer1->HTMLDoc->Add(""); Response->Content = PageProducer1->Content(); NMPOP31->Disconnect(); }

連線失敗事件處理

void __fastcall TWebModule1::NMPOP31ConnectionFailed(TObject *Sender)
{
    ConnectFlag = false;
}

部署與限制

  1. 編譯產生的 DLL 重新命名為 ReadMail.dll,放入 C:\Inetpub\scripts。
  2. 於 PWS 4.0 / Windows 98 環境測試通過。
  3. 已知限制:僅支援純文字郵件,缺乏例外處理機制,MIME/HTML 郵件需自行擴充。

此範例展示了 C++ Builder 開發 ISAPI 擴展的完整流程,為建立高效能 Web 應用奠定基礎。

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