Saturday, September 26, 2009

Send Mail through Gmail Accoun [ Asp.net C# ]

// Name Space

using System;

using System.Net;

using System.Net.Mail;

//Function

private void MailSendMessage()

{

try

{

NetworkCredential loginInfo = new NetworkCredential("username@domain.com", "password");

MailMessage msg = new MailMessage();

msg.From = new MailAddress("from@domain.com","DisplayName");

msg.To.Add(new MailAddress("to@domain.com"));

msg.Subject = "Test";

msg.Body = "Test";

msg.IsBodyHtml = true;

SmtpClient client = new SmtpClient("smtp.gmail.com");

client.Port = 587;

client.EnableSsl = true;

client.UseDefaultCredentials = false;

client.Credentials = loginInfo;

client.Send(msg);

}

catch (Exception ex)

{

Response.Write("
[1] Message :"
+ ex.Message.ToString());

Response.Write("
[2] Stack Trace :"
+ ex.StackTrace.ToString());

}

}

No comments:

Post a Comment