Home > Tutorial > Asp.NET Mail using Gmail SMTP Tutorial

Asp.NET Mail using Gmail SMTP Tutorial

It’s very useful to learn how to set up to send mail using Asp.NET, both GMail and Asp.NET are widely used and trusted. One advantage is flexibility: If you’re a setting up a large email system for mass mail-outs to Foxy Bingo customers and other gaming fans, or if you’re a smaller, family business who is just mailing your local clientele, this system is up to the job. Enjoy the tutorial.

So here is another, even briefer, tutorial on sending mail in Asp.NET using GMail SMTP (means using a GMail Account doh!).

Alright, this is what you do:

  1. If you haven’t done so already, download a .NET development tool like Visual Web Developer 2008 Express Edition.
  2. Go to File>New Web Site, select new ASP.NET Web Site, and choose Visual C# for your language. (Ok, You’ve mostly probably already come up to this stage, but I’m just being complete. :)
Step 2 in action. If you are not seeing this, try harder.

Step 2 in action. If you are not seeing this, try harder.

  1. Now, by default you’ll see your Default.aspx file, where you can use the visual tools to drag and drop in your files. But you actually want the Default.aspx.cs file to edit. On the right you’ll see your Solution Explorer. Click on the plus and double click on Default.aspx.cs
Top right, Solution Explorer. Code in the middle.

Top right, Solution Explorer. Code in the middle.

  1. Here you’ll have the actual C# code that runs to generate your HTML output for the browser. So this is where you shall put in your C# Mail code. Be sure to edit out like the username (your entire GMail gmail), and password (you know, that one).
using System;
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.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        sendMail();
    }
    public void sendMail()
    {
        string from="vishalkumar@gmail.com";
        string to = "vishalkumar@gmail.com";
        string subject = "Hi there";
        string body = "Hey there <br /> Check out http://www.vishalkumar.in";
        sendMailall(from, to, subject, body);
    }
    public void sendMailall(string from, string to, string subject, string body)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(to);
        mail.From = new MailAddress(from);
        mail.Subject = subject;
        mail.Body = body;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Credentials =
             new System.Net.NetworkCredential("vishalkumar@gmail.com",
                                                                "thisisnotmypassword");
        smtp.EnableSsl = true;
        smtp.Send(mail);
     }
}
  1. Now just press F5 to debug, which basically means run the page. By simply running the above page, you’ll trigger the code to send an email to the address specified.
  2. And you’re done for now. You can now add extra features like creating a form, or sending automated emails, etc, for all you like. Enjoy.

And thats it. Alright I admit, you didn’t really need that many steps. Frankly, just copy the code and play with it.

If you get stuck (and you tried really really hard yes please), gmail me later. Or comment up.

Share and Enjoy:
  • email
  • PDF
  • Google Bookmarks
  • TwitThis
  • Facebook
  • IndianPad
  • Slashdot
  • StumbleUpon
  • Technorati
  • Yahoo! Buzz
  • del.icio.us
  • Live
Categories: Tutorial Tags: , ,
  1. janani
    March 22nd, 2010 at 14:34 | #1

    hhiiiiiii

    i did ur process ,,, i run the program
    but it is showing untitled page

    plzzzzz help me how to send mail with attachment file to mail id
    it will be moreeeeeee helpful for my project plzzzzzz

  2. janani
    March 22nd, 2010 at 14:42 | #2

    hiiiiiiiiiiiii

    ya i received mail yar i am very happyyyyyyyyyyyyyyy thank u soooooooooooooo muchhhhhhhhhhhhh

    plzzzzzzzzzzzzz tell me about the attachment file

    how to send mail with attachment file
    plzzzzzz teach me yar

    • May 10th, 2010 at 13:51 | #3

      Hey janani, sorry for the late reply.
      Please have a look athttp://msdn.microsoft.com/en-us/library/system.ne…

      Critical are these lines:

      // Create the file attachment for this e-mail message.
      Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
      // Add time stamp information for the file.
      ContentDisposition disposition = data.ContentDisposition;
      disposition.CreationDate = System.IO.File.GetCreationTime(file);
      disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
      disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
      // Add the file attachment to this e-mail message.
      message.Attachments.Add(data);

  3. Dhiren Shah
    April 25th, 2011 at 20:30 | #4

    Thank you

  4. ABHILASH
    July 26th, 2011 at 20:34 | #5

    hi in my project i used the following code…please check whether it is correct or not.

    —-SMTP Mailing in GMAIL—-

    i) take the source of web configuration file
    change the code as follows:-
    </system.web>
    <system.net>
    <mailsettings>
    <smtp from="yourname@gmail.com">
    <network host="smtp.gmail.com"port="587" username="yourusername"password="yourpassword"/>
    </smtp>
    </mailsettings>
    </system.net>

    ii)FORM CODE:-

    Linkbutton1_click
    {
    smtpclient s=new smtp client();
    e.enable ss1=true;
    s.send("youremail@gmail.com",textbox1.text,textbox2.text,textbox3.text);
    response.write("Succes");
    }
    }

  1. No trackbacks yet.
You must be logged in to post a comment.