標籤:
你要用QQ郵件發郵件就配置郵件伺服器為QQ郵箱的郵件伺服器mail.qq.com.
然後FromAddress那裡配置成你的郵件地址,
objSmtpClient.Credentials 需要你的郵箱和密碼。
方法 SendMail(string mailAddress, string mailTitle, string mailContent)
需要參數mailAddress為你要發送的郵箱地址,mailTitle為郵件主題,mailContent為郵件內容。
還需要引用:using System.Net.Mail;
public static void SendMail(string mailAddress, string mailTitle, string mailContent)
{
MailMessage objMailMessage = new MailMessage();
string fromAddress = ConfigurationManager.AppSettings["FromAddress"];//你在web.config中配置的寄件者地址,就是你的郵箱地址。
string mailHost = ConfigurationManager.AppSettings["MailHost"];//郵件伺服器,如mail.qq.com
objMailMessage.From = new MailAddress(fromAddress);//發送方地址
objMailMessage.To.Add(new MailAddress(mailAddress));//收信人地址
objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;//郵件編碼
objMailMessage.Subject = mailTitle;//郵件標題
objMailMessage.Body = mailContent;//郵件內容
objMailMessage.IsBodyHtml = true;//郵件內文是否為html格式
SmtpClient objSmtpClient = new SmtpClient();
objSmtpClient.Host = mailHost;//郵件伺服器地址
objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//通過網路發送到stmp郵件伺服器
objSmtpClient.Credentials = new System.Net.NetworkCredential();//發送方的郵件地址,密碼
//objSmtpClient.EnableSsl = true;//SMTP 伺服器要求安全連線需要設定此屬性
try
{
objSmtpClient.Send(objMailMessage);
}
catch (Exception ex)
{
LogHelper.WriteLog(LogFile.Error, ex.ToString());//記錄錯誤記錄檔
}
}
C#_發送郵件