Code
try
{
System.Net.Mail.MailMessage mailObj = new System.Net.Mail.MailMessage();
mailObj.IsBodyHtml = true;
mailObj.Subject = "郵件標題";
mailObj.Body = "郵件內文";
mailObj.To.Add("收信人地址");
System.Net.Mail.SmtpClient SmtpMail = new SmtpClient("smtp.gmail.com");
mailObj.From = new MailAddress("發信人@gmail.com", "你的名字", System.Text.Encoding.UTF8);
SmtpMail.Credentials = new System.Net.NetworkCredential("發信人@gmail.com", "密碼");
//gmail 專有配置 開始
SmtpMail.Port = 587;
SmtpMail.EnableSsl = true;
//gmail 專有配置 結束
SmtpMail.Send(mailObj);
Response.Write("郵件發送成功!");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
當然,通過在web.config中配置一下,更簡單
Code
<system.net>
<mailSettings>
<smtp>
<network host ="smtp.gmail.com" userName ="發信人@gmail.com" port ="587" password ="*****"/>
</smtp>
</mailSettings>
</system.net>
用戶端代碼如下:
Code
SmtpClient client = new SmtpClient();
MailMessage mm = new MailMessage("寄件者@gamil.com", "收件者@qq.com", "Ttile", "body");
client.Send(mm);
三行代碼搞定!(高手見笑了!)