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

void Main()
{
string htmlContent = @”
<html><body>
I think this wouldn’t work. But if works would be wonderful – ignore guys testing email 🙂
</p>
Some HTML content here:
</hr>
“;

MailMessage message = new MailMessage();

message.From = new MailAddress(“XXX@gmail.com”,);

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

//message.To.Add(new MailAddress(“XXX.QQQ@AA.com”));
//message.CC.Add(new MailAddress(“WWW@ZZZ.com”, “Mx”));
//message.Bcc.Add(new MailAddress(“BBB@GGG.org”,”xcv At xyz”));
message.Subject = “Test smtp HTML email”;
message.Body = htmlContent;
message.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host=”smtp.gmail.com”;
smtp.Port=587;

smtp.Credentials = new NetworkCredential(“XXX@gmail.com”, ” ….. “);
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());
}
else

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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s