c#SmtpClient非常適合您的需求。這裏的一些示例代碼(主機是一個IP地址或主機名,端口通常是25,但不一定):
public static void SendMail(
string host,
int port,
SmtpAuthentication authentication,
string userName,
string password,
string from,
string to,
string cc,
string subject,
string body,
string[] attachments)
{
// Create and configure the smtp client
SmtpClient smtpClient = new SmtpClient();
if (host != null && host.Length > 0)
{
smtpClient.Host = host;
}
smtpClient.Port = port;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
if (authentication == SmtpAuthentication.Basic)
{
// Perform basic authentication
smtpClient.Credentials = new System.Net.NetworkCredential(userName, password);
}
else if (authentication == SmtpAuthentication.Ntlm)
{
// Perform integrated authentication (NTLM)
smtpClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
}
MailMessage mailMessage = new MailMessage();
mailMessage.Body = body;
mailMessage.From = new MailAddress(from);
mailMessage.To.Add(to);
mailMessage.CC.Add(cc);
foreach (string attachement in attachments)
{
mailMessage.Attachments.Add(new Attachment(attachement));
}
mailMessage.Subject = subject;
mailMessage.Priority = MailPriority.Normal;
smtpClient.Send(mailMessage);
}
}
謝謝,我不知道主機字符串使用什麼。它是PC的IP地址嗎? 因此,要在每臺機器上工作,我應該使用CredentialCache.DefaultNetworkCredentials進行身份驗證? – Maggie 2009-10-13 20:22:38
嘗試詢問您的系統管理員的主機名。至於憑證,默認網絡憑證應該適用於所有機器。你應該只提供你的郵箱用戶名和密碼。 – geva30 2009-10-14 01:31:39