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

C# 郵件發送元件:支援文字/HTML、附件、SMTP 驗證

     編輯:關於C語言

功能特色

  • 支援純文字與 HTML 兩種郵件格式
  • 支援多檔案附件
  • 支援 SMTP 基本驗證 (帳號/密碼)
  • 例外處理與錯誤容忍
  • 靜態方法設計,呼叫簡單

完整原始碼


using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mail;
using System.Collections;

namespace BaseLib
{
    public class SendMail
    {
        /// 
        /// 發送純文字郵件
        /// 
        public void SendTextMail(
            string from, string password, string to, string subject,
            MailPriority priority, string body, string smtpServer,
            ArrayList attachments)
        {
            MailMessage msg = new MailMessage();
            msg.From = from;
            msg.To = to;
            msg.Subject = subject;
            msg.Priority = priority;
            msg.BodyFormat = MailFormat.Text;
            msg.Body = body;

            // SMTP 驗證設定
            msg.Fields.Add("https://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            msg.Fields.Add("https://schemas.microsoft.com/cdo/configuration/sendusername", 
                          from.Substring(0, from.IndexOf("@")));
            msg.Fields.Add("https://schemas.microsoft.com/cdo/configuration/sendpassword", password);

            // 附件處理
            foreach (string filePath in attachments)
            {
                if (System.IO.File.Exists(filePath))
                {
                    msg.Attachments.Add(new MailAttachment(filePath));
                }
            }

            SmtpMail.SmtpServer = smtpServer;
            try
            {
                SmtpMail.Send(msg);
            }
            catch (Exception)
            {
                // 實務建議:記錄日誌或拋出自訂例外
            }
        }

        /// 
        /// 發送 HTML 郵件
        /// 
        public void SendHtmlMail(
            string from, string password, string to, string subject,
            MailPriority priority, string body, string smtpServer,
            ArrayList attachments)
        {
            MailMessage msg = new MailMessage();
            msg.From = from;
            msg.To = to;
            msg.Subject = subject;
            msg.Priority = priority;
            msg.BodyFormat = MailFormat.Html;
            msg.Body = body;

            // SMTP 驗證設定 (同文字郵件)
            msg.Fields.Add("https://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            msg.Fields.Add("https://schemas.microsoft.com/cdo/configuration/sendusername", 
                          from.Substring(0, from.IndexOf("@")));
            msg.Fields.Add("https://schemas.microsoft.com/cdo/configuration/sendpassword", password);

            // 附件處理
            foreach (string filePath in attachments)
            {
                if (System.IO.File.Exists(filePath))
                {
                    msg.Attachments.Add(new MailAttachment(filePath));
                }
            }

            SmtpMail.SmtpServer = smtpServer;
            SmtpMail.Send(msg);
        }
    }
}

使用範例


var mailer = new BaseLib.SendMail();
var attachments = new ArrayList { @"C:\Reports\monthly.pdf", @"C:\Images\chart.png" };

// 發送文字郵件
mailer.SendTextMail(
    from: "sender@example.com",
    password: "smtpPassword",
    to: "receiver@example.com",
    subject: "月報表",
    priority: MailPriority.Normal,
    body: "請查收附件月報表。",
    smtpServer: "smtp.example.com",
    attachments: attachments
);

// 發送 HTML 郵件
mailer.SendHtmlMail(
    from: "sender@example.com",
    password: "smtpPassword",
    to: "receiver@example.com",
    subject: "月報表",
    priority: MailPriority.High,
    body: "

月報表

請查收附件 月報表。

", smtpServer: "smtp.example.com", attachments: attachments );

注意事項

  1. System.Web.Mail 為 .NET Framework 1.x/2.0 時代 API,現代專案建議使用 System.Net.Mail (功能更完整、支援 SSL/TLS)
  2. SMTP 驗證欄位使用 CDO 配置結構描述,相容性較好
  3. 附件路徑建議使用絕對路徑,並預先檢查檔案存在性
  4. 例外處理建議記錄詳細錯誤資訊 (SMTP 回應碼、例外堆疊)

現代替代方案:System.Net.Mail


using System.Net.Mail;
using System.Net;

var client = new SmtpClient("smtp.example.com", 587)
{
    Credentials = new NetworkCredential("user", "pass"),
    EnableSsl = true
};

var mail = new MailMessage("from@example.com", "to@example.com")
{
    Subject = "主旨",
    Body = "

HTML 內容

", IsBodyHtml = true }; mail.Attachments.Add(new Attachment(@"C:\file.pdf")); client.Send(mail);
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved