最一開始記得要先引用
using MailMessage=System.Net.Mail.MailMessage;
寄信相關寫法
{
//先取得設定寄件者MAIL與名稱 to 跟 From應該懂吧
MailAddress from = new MailAddress(要寄信的信箱, 寄件者顯示名稱);
MailAddress to = new MailAddress(mailAdd,"");
MailMessage message = new MailMessage(from, to);
//設定主題跟內文
message.Subject = "主旨";
message.Body = "內容";
//做UTF 8編碼 ,可做可不做啦
message.BodyEncoding = Encoding.UTF8;
message.SubjectEncoding = Encoding.UTF8;
//new 一個SMTP SERVER
SmtpClient client = new SmtpClient("你的SmtpServer"));
//設定驗證機制
NetworkCredential pa = new NetworkCredential("你要寄信的信箱帳號","密碼");
client.Credentials = pa;
//寄信
client.Send(message);
}
寄信寫法有多種,如要寄很多信建議用COPY 在CC的方式比較不會被認為大量寄信的垃圾郵件,相關寫法可參照微軟的MSDN,
連結如下:http://msdn.microsoft.com/zh-tw/library/system.net.mail.mailmessage(VS.80).aspx
另一種SMTP 簡單的寄信語法
using System.Net.Mail;
string CMailA = "king@gmail.com.tw"; //收件者
string mailContent = "*** 請注意︰此郵件是系統自動傳送,請勿直接回覆此郵件。***";
MailMessage messageA = new MailMessage("king@gmail.com.tw", CMailA, "【訊息】主旨", mailContent);
messageA.IsBodyHtml = true;
SmtpClient emailClientA = new SmtpClient("SMTP伺服器", 25);
NetworkCredential pa = new NetworkCredential("帳號", "密碼");
emailClientA.Credentials = pa;
emailClientA.Send(messageA);
沒有留言:
張貼留言