PDA

View Full Version : Gửi Mail trong ASP.NET (Gmail) - Send Mail in ASP.NET


Pham Long
02-06-2010, 12:47 PM
Đoạn code sau đây sẽ giúp các bạn gửi mail trong ASP.NET bằng tài khoản Gmail

SmtpClient client = new SmtpClient();

client.DeliveryMethod = SmtpDeliveryMethod.Network;

client.EnableSsl = true;

client.Host = "smtp.gmail.com";

client.Port = 587;



// setup Smtp authentication

System.Net.NetworkCredential credentials =
[Only registered and activated users can see links]

client.UseDefaultCredentials = false;

client.Credentials = credentials;



MailMessage msg = new MailMessage();

[Only registered and activated users can see links]

[Only registered and activated users can see links]



msg.Subject = "This is a test Email subject";

msg.IsBodyHtml = true;

msg.Body = string.Format("<html><head></head><body><b>Test HTML Email</b></body>");



try
{

client.Send(msg);

lblMsg.Text = "Your message has been successfully sent.";

}

catch (Exception ex)
{

//lblMsg.ForeColor = Color.Red;

lblMsg.Text = "Error occured while sending your message." + ex.Message;

}

khampha1
13-07-2010, 09:15 AM
Phải kèm cả mật khẩu của mình thì khi mình thay mk email thì website "toi" à? Lỡ web bị hack thì cũng mất luôn cả email!

vietcode
06-09-2010, 09:33 PM
Chơi kiểu này nó đỡ gửi vào spam bạn ạ !