2011年11月29日

ASP.NET C# 用Gmail SMTP寄信

利用C# Mailmessage方法

string smtpserver = "smtp.gmail.com"; //Gmail smtp 主機
string smtpaccount = "xxx@gmail.com"; //Gmail 帳號
string smtppassword = "password";     //Gmail 密碼
            
MailMessage msg = new MailMessage();
msg.From = new MailAddress("xxx@gmail.com", "老王");
msg.To.Add("ooo@yahoo.com");  //收件人
msg.Bcc.Add("xxx@yahoo.com"); //密件副本 
msg.Subject = "標題";     
msg.IsBodyHtml = true;       //啟用Html格式內文
msg.Body = "信件內容";
SmtpClient smtp = new SmtpClient(smtpserver);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Port = 587;         //Gmail smtp port
smtp.EnableSsl = true;   //開啟ssl模式
smtp.Credentials = new System.Net.NetworkCredential(smtpaccount, smtppassword);
smtp.Send(msg); 

0 意見:

張貼留言

##EasyReadMore##