Sending e-mail via Asp.Net Form and C# using gMail SMTP

void Main()
string htmlContent = @”
I think this wouldn’t work. But if works would be wonderful – ignore guys testing email 🙂
Some HTML content here:

MailMessage message = new MailMessage();

message.From = new MailAddress(“”,);

message.To.Add(@”XXX at yyy <>; ‘XXX YYY’ <>”.Replace(‘;’,’,’));

//message.To.Add(new MailAddress(“”));
//message.CC.Add(new MailAddress(“”, “Mx”));
//message.Bcc.Add(new MailAddress(“”,”xcv At xyz”));
message.Subject = “Test smtp HTML email”;
message.Body = htmlContent;
message.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();

smtp.Credentials = new NetworkCredential(“”, ” ….. “);
smtp.EnableSsl = true;

//smtp.Send( message);

object userState = message; // UserState parameter can be anything

//wire up the event for when the Async send is completed
smtp.SendCompleted += new SendCompletedEventHandler(SmtpClient_OnCompleted);

smtp.SendAsync( message, userState );


public static void SmtpClient_OnCompleted(object sender, AsyncCompletedEventArgs e)
//Get the Original MailMessage object
MailMessage mail= (MailMessage)e.UserState;

//write out the subject
string subject = mail.Subject;

if (e.Cancelled)

Console.WriteLine(“Send canceled for mail with subject [{0}].”, subject);
if (e.Error != null)
Console.WriteLine(“Error {1} occurred when sending mail [{0}] “, subject, e.Error.ToString());

Console.WriteLine(“Message [{0}] sent.”, subject );


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s