How to send email with google gmail account in asp.net?

/ / 4 Comments
Send email using Gmail in Asp.net C#: Here in this article, we demonstrate how to send an email using your Google, Gmail account in Asp.net c#. Sending Email is a common task of a web application for many purposes like- Error logging, Reporting, Newsletters as well as many other reasons.

Gmail is a good solution for this purpose. Gmail provides SMTP and POP access which can be utilized for sending and receiving E-mails.


Code to send email in asp.net c# with your Gmail google account:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Net.Mail;
 
 
// system.net.mail namespace is used for emailing.
  protected void btnsubmit_Click(object sender, ImageClickEventArgs e)
 {  
     //Code for send Email
        MailMessage sendMailforSA = new MailMessage();
        SmtpClient smtpforSA = new SmtpClient();
        string subjectforSA = null;
        subjectforSA = "Thanks for apply";
        System.Net.NetworkCredential credforSA = new System.Net.NetworkCredential("Superman@gmail.com", "password");
        sendMailforSA.To.Add(txtemailid.Value);
        MailAddress cc = new MailAddress("info@superman.com");
        sendMailforSA.CC.Add(cc);

        // sendMailforSA.CC("abcxxx@gmail.com");
        sendMailforSA.From = new MailAddress("Rajnikant@gmail.com");
        sendMailforSA.Subject = subjectforSA.ToString();
        sendMailforSA.Body =" Thank you.... your message you can use html tag for style effect";
        sendMailforSA.IsBodyHtml = true;
        smtpforSA.Host = "smtp.gmail.com";
        smtpforSA.Port = 25;
        smtpforSA.EnableSsl = false;
        smtpforSA.UseDefaultCredentials = false;
        smtpforSA.Credentials = credforSA;
        smtpforSA.Send(sendMailforSA);
}

Thank you for reading, pls keep visiting this blog and share this in your network. Also, I would love to hear your opinions down in the comments.

PS: If you found this content valuable and want to thank me? 👳 Buy Me a Coffee

Subscribe to our newsletter

Get the latest and greatest from Codepedia delivered straight to your inbox.


Post Comment

Your email address will not be published. Required fields are marked *

4 Comments

  1. Mass Emails 05/25/2012 03:22:25
    Hello,

    Nice post. I like the way you start and then conclude your thoughts. Send Email offers an easy to use, free email encryption service with a simplified registration process. Thanks for this information.I really appreciate your work, keep it up...
  2. Lelala 06/02/2012 18:12:07
    Nice idea, but the problem is:
    Your website stops loading due the handshake with the foreign SMTP server is done, meaning the site will hang for one or two seconds.
    Better you start a simple background thread for actually sending the mail, so the user will have no delay.
  3. Satinder singh 06/14/2012 11:11:39
    Thanks for the feedBack