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
);
System.Web.Mail 為 .NET Framework 1.x/2.0 時代 API,現代專案建議使用 System.Net.Mail (功能更完整、支援 SSL/TLS)
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);