轉載自:http://blog.csdn.net/fhbcn/archive/2008/03/01/2137819.aspx
一、 發送郵件程式:(使用System.Web.Mail下的類)
public static bool SendMail(string mailhead, string mailbody, string emailadd)
...{
if (emailadd != "")
...{
//建立MailMessage對象
System.Web.Mail.MailMessage mailMsg = new System.Web.Mail.MailMessage();
//設定收件者的郵件地址
mailMsg.To = emailadd;
//設定寄件者的郵件地址
mailMsg.From = "wicresoft@126.com";
//設定郵件主題
mailMsg.Subject = mailhead;
//設定郵件內容
mailMsg.BodyFormat = System.Web.Mail.MailFormat.Html;
if (mailbody != "")
...{
mailMsg.Body = mailbody;
//設定支援伺服器驗證
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//設定使用者名稱
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "wicresoft");
//設定使用者密碼
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "wangwei");
try
...{
//設定發送郵件伺服器 202.108.5.142
System.Web.Mail.SmtpMail.SmtpServer = "202.108.5.142";
//發送郵件
System.Web.Mail.SmtpMail.Send(mailMsg);
return true;
}
catch
...{
return false;
}
}
else
return false;
}
else
return false;
}
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/fhbcn/archive/2008/03/01/2137819.aspx
二、Net2.0又提供了一個替代方法(使用System.Nw)
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
namespace MailSender
...{
class Program
...{
static string strHost = string.Empty;
static string strAccount = string.Empty;
static string strPwd = string.Empty;
static string strFrom = string.Empty;
static void Main(string[] args)
...{
strHost = "smtp.163.com"; //STMP伺服器位址
strAccount = "jailu"; //SMTP服務帳號
strPwd = "123456789"; //SMTP服務密碼
strFrom = "jailu@163.com"; //發送方郵件地址
Console.WriteLine(sendMail("jailu@qq.com", "這是一封測試郵件", "這是一封測試郵件的本文內容") ? "Success" : "Unsuccess");
Console.ReadLine();
}
/**//// <summary>
/// 發送郵件
/// </summary>
/// <param name="to">接收方郵件地址</param>
/// <param name="title">郵件標題</param>
/// <param name="content">郵件內文內容</param>
/// <returns></returns>
static bool sendMail(string to, string title, string content)
...{
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定電子郵件發送方式
smtpClient.Host = strHost; ;//指定SMTP伺服器
smtpClient.Credentials = new System.Net.NetworkCredential(strAccount, strPwd);//使用者名稱和密碼
MailMessage mailMessage = new MailMessage(strFrom,to);
mailMessage.Subject = title;//主題
mailMessage.Body = content;//內容
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//本文編碼
mailMessage.IsBodyHtml = true;//設定為HTML格式
mailMessage.Priority = MailPriority.High;//優先順序
try
...{
smtpClient.Send(mailMessage);
return true;
}
catch
...{
return false;
}
}
}
}
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/fhbcn/archive/2008/03/01/2137819.aspx