首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> C# 入門

用 C# 打造郵件通知精靈:System.Net.Mail 與 Microsoft Agent 整合

     編輯:C# 入門

C# 郵件通知器完整實作:POP3 協定、系統匣常駐、Microsoft Agent 精靈互動

結合網路程式設計、Windows Forms 系統匣應用、COM 元件互通,打造實用的郵件到達通知工具。

專案架構

  • 主表單:設定參數 (伺服器、連接埠、帳號、密碼、檢查間隔)、系統匣圖示、右鍵選單
  • 精靈表單:載入 Microsoft Agent 控制項 (Genie/Merlin/Robby/Peedy)
  • 背景執行緒:Timer 觸發定期檢查

核心命名空間

using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Threading;

網路連線與 POP3 協定實作

private NetworkStream netStream;

private void EmailCheck() {
    TcpClient tcpClient = new TcpClient();
    try {
        tcpClient.Connect(serverAddress.Text, int.Parse(serverPort.Text));
    } catch {
        MessageBox.Show($"無法連線至 {serverAddress.Text}:{serverPort.Text}");
        return;
    }
    
    netStream = tcpClient.GetStream();
    if (netStream == null) throw new Exception("網路串流為空");
    
    string response = ReadFromNetStream(ref netStream);
    CheckForError(response);
    
    // POP3 認證流程
    WriteToNetStream(ref netStream, "USER " + username.Text);
    response = ReadFromNetStream(ref netStream);
    CheckForError(response);
    
    WriteToNetStream(ref netStream, "PASS " + password.Text);
    response = ReadFromNetStream(ref netStream);
    CheckForError(response);
    
    // 取得郵件統計
    Stat();
    
    netStream.Close();
    tcpClient.Close();
}

private void WriteToNetStream(ref NetworkStream ns, string cmd) {
    byte[] data = Encoding.ASCII.GetBytes(cmd + "\r\n");
    ns.Write(data, 0, data.Length);
}

private string ReadFromNetStream(ref NetworkStream ns) {
    StreamReader sr = new StreamReader(ns);
    StringBuilder sb = new StringBuilder();
    string line;
    while ((line = sr.ReadLine()) != null) {
        sb.AppendLine(line);
        if (!sr.Peek().Equals(-1)) continue;
        break;
    }
    return sb.ToString();
}

private void CheckForError(string response) {
    if (response.StartsWith("-ERR"))
        throw new Exception("伺服器錯誤: " + response);
}

private void Stat() {
    WriteToNetStream(ref netStream, "STAT");
    string response = ReadFromNetStream(ref netStream);
    CheckForError(response);
    
    // 回應格式: +OK 郵件數 總大小
    string[] parts = response.Split(' ');
    int count = int.Parse(parts[1]);
    int totalSize = int.Parse(parts[2]);
    
    // 呼叫精靈表單通知
    Form2 agent = new Form2(count);
    agent.Show();
}

系統匣常駐設計

// Form1 建構函式
public Form1() {
    InitializeComponent();
    this.Opacity = 0;        // 隱藏主視窗
    this.ShowInTaskbar = false;  // 不在工作列顯示
    timer1.Interval = int.Parse(timeSpan.Text);  // 設定檢查間隔
    timer1.Enabled = true;
}

// Timer Tick 事件
private void timer1_Tick(object sender, EventArgs e) {
    Thread t = new Thread(EmailCheck);
    t.Start();
}

// 系統匣右鍵選單事件
private void menuConfig_Click(object sender, EventArgs e) {
    this.Opacity = 100;  // 顯示設定視窗
    this.ShowInTaskbar = true;
}

private void menuExit_Click(object sender, EventArgs e) {
    Application.Exit();
}

Microsoft Agent 精靈整合

  1. 下載安裝:Microsoft Agent Core + 角色 (Genie/Merlin/Robby/Peedy) + TTS 引擎 + SAPI 4.0
  2. 專案加入參考:Microsoft Agent Control (ActiveX)
  3. Form2 載入 Agent 控制項,程式碼控制角色動作/語音

關鍵技術點總結

技術類別/元件用途
TCP 連線TcpClient, NetworkStreamPOP3 通訊基礎
非同步檢查Thread + Timer避免封鎖 UI
系統匣NotifyIcon, ContextMenu背景常駐、右鍵選單
精靈Agent Control (COM)語音/動畫通知
文字編碼Encoding.ASCII.GetBytes網路傳輸位元組轉換

現代化建議

  • 改用 System.Net.Mail.Pop3Client (第三方) 或 MailKit 成熟函式庫
  • Microsoft Agent 已停止開發,考慮 Toast 通知、SignalR 即時推播
  • async/await 取代手動 Thread 管理
  • 設定加密儲存 (ProtectedData)
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved