Sending HTML Mail Using Gmail SMTP (using System.Net.Mail namespace)

INTRODUCTION: Sending emails requires SMTP server and hence usually you won’t be able to send emails from your local machine [In case you have SMTP server configured on your local machine, you can surely send emails using that!].

The other way out would be to use a third parties SMTP server to send Emails.

 

In this Article I am going to use Gmail’s SMTP to send Emails.

 

Using System.Net.Mail; //Include This NameSpace


MailMessage MyMailMessage = new MailMessage();

MyMailMessage.From = new MailAddress("cadbuaryguy@gmail.com");

MyMailMessage.To.Add("learninggeeks@yahoo.com");

MyMailMessage.Subject = "Feedback Form";

MyMailMessage.IsBodyHtml = true;

MyMailMessage.Body = "<table><tr><td>" + txtName.Text + txtEmail.Text + txtComments.Text + "</table></tr></td>";

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

SMTPServer.Port = 587;

SMTPServer.Credentials = new System.Net.NetworkCredential("cadbuaryguy@gmail.com", System.Configuration.ConfigurationSettings.AppSettings["pwd"].ToString());

SMTPServer.EnableSsl = true;

try

{

SMTPServer.Send(MyMailMessage);

Response.Redirect("Thankyou.aspx");

}

catch (Exception ex)

{



}

25 Comments

  • The other way out would be to use a third parties SMTP server to send Emails.

  • Why didn't you put all config in web.config ?

  • Well i stored the password field in web.config for security.

  • i want to send my form data into my server email id .pls guide me. i have a form in my website .when user fill the form all the content of the form is send to my server email id in which i host my site

  • I ame using same code but it give me error "Failure sending mail" it working on localhost but not working when i put on server..

  • Hi

    Its Great...!
    Work Perfectly...Done.
    Use it & Enjoy...!

  • Great working fine for me! thank a lot!!!

  • wc Nijith!

  • I couldn’t refrain from commenting. Perfectly written.

  • Hi there Dear, are you actually visiting this website on
    a regular basis, if so afterward you will without doubt obtain
    good know-how.

  • I have been exploring for a little for any high quality articles
    or weblog posts on this sort of space . Exploring in Yahoo I
    ultimately stumbled upon this site. Studying this information So i am satisfied to
    exhibit that I've a very good uncanny feeling I came upon just what I needed. I such a lot surely will make certain to don?t fail to remember this site and provides it a glance regularly.

  • obviously like your web-site however you need to check the spelling on several of your posts.
    A number of them are rife with spelling problems and I in finding it very troublesome to tell the truth on the other hand
    I'll certainly come again again.

  • Good day! Do you use Twitter? I'd like to follow you if that would be ok. I'm
    undoubtedly enjoying your blog and look forward to new updates.

  • great put up, very informative. I ponder why the opposite experts of this sector don't understand this. You should continue your writing. I'm confident, you've a great readers' base already!

  • Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something.
    I think that you could do with a few pics to drive the message home a little bit, but other than that,
    this is wonderful blog. A fantastic read. I will certainly be back.

  • I really like your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it for you?

    Plz respond as I'm looking to construct my own blog and would like to find out where u got this from. appreciate it

  • Your way of explaining everything in this paragraph is in fact nice, all be able to simply be aware of it, Thanks a lot.

  • Hello, yeah this post is in fact good and I have learned lot of things from it on the topic of blogging.
    thanks.

  • Zombies: Buckethead, conehead and regular zombies attack in two flags.
    Simple and easy to Use - Anybody can spin effortlessly as a result of your
    recent titles and websites straight within the home
    screen. Each plant has a different ability when super-powered with plant food, but the
    superpower only lasts for a few seconds.

  • Wandesr this magnificent store samplibg the finest the fashion world has to
    offer. Men and women who must wear only the best designer labels will be happy to
    find out thyat there are plenty of green clothing items made by expensive fashion designers.
    Any time you have used a 9ct rare metal pendant, or some kind
    of designer necklaces, then that would not discredit the significance off the
    jewellery bit.

  • As a fashion guide, it never lacks of fashion
    spot that will always attract public's attentions at the first sight.
    In the past you had little choice for your Uggs boot but they are now available in a wide range of colors and
    styles. But in the heat of summer, the natural fibers of the fleece
    actually cool your feet by wicking away perspiration.

  • You're going to end up having to go over it 3, 4, 5, 6, 7 times to cover up that darker color.
    On the other side of the field fired at pins, hit driver
    when they normally would have used 3-wood, and tried to
    accept the offer and there are a lot of opportunity.

    It's also a long term investment, you need at least one jackets with earphones in their wardrobe.
    The Blue Jackets are jackets with earphones in trend. The most
    popular color but now a day's white and blue color is also in fashion.

  • It's difficult to find well-informed people about
    this subject, however, you seem like you know what you're talking about!
    Thanks

  • ebook reader test
    I really love your website.. Pleasant colors & theme. Did you build this website yourself?
    Please reply back as I'm attempting to create my
    very own site and would love to learn where you got this from or just what the theme is named.

    Appreciate it! ebook reader test

  • Yoour stye is so unique comparded to other folks I've read stutf from.
    Than you for posting when you have the opportunity, Guess I will just book mark thiis page.

Comments have been disabled for this content.