How to Send Email from localhost to any mail Id using gmail's email Id and password authentication and also its SMTP ADDRESS and PORT ADDRESS.

HOME                AJAX                  VALIDATION                 EMAIL                 ADO.NET               SILVERLIGHT                 CONTACT US  



First a little bit of basics: In order to send a mail, we need a server and its SMTP address , which with obvious reasons , not everyone can have it (not atleast without paying for it) . So we are going to use the SMTP server of gmail for mail outgoing.  SMTP ADDRESS and OUTGOING PORT NUMBER  are the two most important things we need.
The important namespaces one needs to add are-
System.Net ;
System.Net.Mail ;

Above namespaces are needed because we are going to use some components and classes that are the part of these namespaces .

So … Lets get started…

First of all, Design a Web form that at least has following things -
1. Sender’s Mail Id
2. Password
3. Receiver’s Mail Id
4. Subject
5. Message box
6. Send button.

Sample web form code is given below (paste it inside your page body):


<form id="form1" runat="server">
  
 <div>
   
        <asp:Label ID="Label1" runat="server" Text="COMPOSE MAIL..."></asp:Label>
   
    &nbsp;
   
    </div>
    <table bgcolor="#666766" class="style1">
        <tr>
            <td class="style2">
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Label ID="Label4" runat="server" Text="FROM"></asp:Label>
            </td>
            <td class="style3">
                <asp:TextBox ID="frombox" runat="server" Height="30px" Width="486px"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
           
                <asp:Label ID="Label7" runat="server" Text="PASSWORD"></asp:Label>
            </td>
            <td class="style3">
                <asp:TextBox ID="Passwordbox" runat="server" Height="30px" Width="486px"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
           
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
           
                <asp:Label ID="Label2" runat="server" Text="TO"></asp:Label>
            </td>
            <td class="style3">
                <asp:TextBox ID="tobox" runat="server" Height="30px" Width="486px"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Label ID="Label3" runat="server" Text="SUBJECT"></asp:Label>
            </td>
            <td class="style3">
                <asp:TextBox ID="subbox" runat="server" Height="30px" Width="486px"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Label ID="Label5" runat="server" Text="UPLOAD"></asp:Label>
            </td>
            <td class="style3">
                <asp:FileUpload ID="FileUpload1" runat="server" Height="30px" Width="239px" />
                <asp:Button ID="Button2" runat="server" Height="30px" onclick="Button2_Click"
                    Text="UPLOAD" />
&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Image ID="Image1" runat="server" Height="40px" Width="47px" />
&nbsp;&nbsp;
                <asp:Label ID="Label6" runat="server"></asp:Label>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                &nbsp;</td>
            <td class="style3">
                <asp:TextBox ID="msgbox" runat="server" Height="281px" TextMode="MultiLine"
                    Width="486px"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                &nbsp;</td>
            <td class="style3">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                &nbsp;</td>
            <td class="style3">
                <asp:Button ID="Button1" runat="server" Text="SEND" onclick="Button1_Click" />
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                &nbsp;</td>
            <td class="style3">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        </table>
    </form>



The web form may look somewhat this way-





Now, as soon as your mail window is ready, double click the send button in design window of your visual studio.
And on the click event of that button write the following code. (Hope you have  added the namespaces which I mentioned before )-

protected void Button2_Click(object sender, EventArgs e)
  
 {
       
   
try
        {
        
MailMessage Msg = new MailMessage();
             Msg.From = new MailAddress(frombox.Text);
            Msg.To.Add(new MailAddress(tobox.Text));
            Msg.Subject = subbox.Text;
            Msg.Body = msgbox.Text;


            Msg.Priority = System.Net.Mail.MailPriority.High;
            SmtpClient ob = new SmtpClient();
         
            ob.Port = 587;                
            ob.EnableSsl = true;
            ob.UseDefaultCredentials = false;
            ob.Host = "smtp.gmail.com";
            ob.Credentials = new NetworkCredential(‘"+frombox.Text+"’,‘"+Passwordbox.Text+"’);
            ob.Send(Msg);

        
        }
        catch (Exception ex)
        {
            Console.WriteLine("{0} Exception caught.", ex);
        }
     }

That’s it… I hope this will do it…   Enjoy the code and please leave a comment what ever you think .
It’s the driving force for me.  :D

– Vaibhav Patil.



       


HOME                 AJAX                   VALIDATION                  EMAIL                  ADO.NET                  SILVERLIGHT                  CONTACT US

No comments:

Post a Comment