Sending Email via Gmail using c#…

Sending Email via coding is useful in case of registration process where you can send welcome or confirmation message… Or it can be used to varify email address of the new user. You can send random code to that email-id and ask it to enter when user logins, if email id is valid he/she can get the random code mailed by you and proves that he/she has valid email id.

So there are lot of reasons where you need to send email via coding…
Let’s have a look…

To send Email BCL of .net framework provides us few inbuilt classes covered in System.Net.Mail namespace.
In the code we are using following classes :-
1. MailMessage
2. SmtpClient
3. Attachment (if you want to send attachment too)

Now create a Webform with .aspx extension.

In .aspx file put the following code in Body Part

<form id=”form1″ runat=”server”>
<div>
<pre>
To <asp:TextBox ID=”txtTO” runat=”server”></asp:TextBox>

Sub <asp:TextBox ID=”txtSUB” runat=”server”></asp:TextBox>

Body <asp:TextBox ID=”txtBODY” runat=”server” TextMode=”MultiLine”
Height=”150px” Width=”290px”></asp:TextBox>

<asp:Button ID=”Button1″ runat=”server” Text=”Send” onclick=”Button1_Click” />
</pre>
</div>
</form>


In the .cs file put the following code

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
 
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MailMessage  m = new MailMessage(“sender@gmail.com”,txtTO.Text);
        m.Subject = txtSUB.Text;
        m.Body = txtBODY.Text;
 
        SmtpClient s = new SmtpClient(“smtp.gmail.com”,587);
        s.Credentials = new System.Net.NetworkCredential(“sender@gmail.com”,”password”);
        s.EnableSsl = true;
        s.Send(m);
        Button1.Text = “Done”;
    }
}

Explanation of Code :- 

        MailMessage  m = new MailMessage(“sender@gmail.com”,txtTO.Text);
        m.Subject = txtSUB.Text;
        m.Body = txtBODY.Text;

MaiMessage class is used to create a email Message. It this we will give 4 values :- sender’s id, receiver’s id, Subject , Body. Replace your valid sender email id before run

…………………………………………………………………………………………………………………………………..

        SmtpClient s = new SmtpClient(“smtp.gmail.com”,587);

In SmtpClient class, “smtp.gmail.com” is host name and 587 is port number

…………………………………………………………………………………………………………………………………..

        s.Credentials = new System.Net.NetworkCredential(“sender@gmail.com”,”password”);

Here again replace sender’s valid email id with password

…………………………………………………………………………………………………………………………………..

       s.Send(m);

Send method is used to send the message that we have created with named ‘m’

…………………………………………………………………………………………………………………………………..

Note :-

1. please replace sender’s email id and password before running this code

2. if 587 port number doesn’t work, try same code with 25.

3. in this code we have used gmail as a host, so sender’s email id must be of gmail but receiver can be of any host

4 . for Attachment notes keep in touch with this blog… will post soon 🙂

Download Code

Advertisements

One thought on “Sending Email via Gmail using c#…

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