Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 2578 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET AJAX > How to Send an E-mail using Gmail in ASP.Net

How to Send an E-mail using Gmail in ASP.Net

2 vote(s)
Rating: 5 out of 5
Article posted by Vuyiswamb on 12/10/2009 | Views: 5817 | Category: ASP.NET AJAX | Level: Beginner red flag


There are many ways that an application can use to communicate with users. Some users are used to message box popping up and some users are annoyed because this affect the work flow. Some think it’s better to read these messages later. One more thing I saw is that most of the users hate the yellow screen of death. Maybe we need not to show them this message and trap this globally and send this messages t our self.

Introduction

There are many ways that an application can use to communicate with users. Some users are used to message box popping up and some users are annoyed because this affect the work flow. Some think it’s better to read these messages later. One more thing I saw is that most of the users hate the yellow screen of death. Maybe we need not to show them this message and trap this globally and send this messages t our self. One more last scenario is that sometimes users will like to contact you through your website to give you a feedback or complaining about something or send a nice message about how good is your website. All the above scenarios I have mentioned point to e-mail. E-mail is not important only in outlook. But we can use e-mail to make our applications and websites more responsive. We can know what our visitors feel about our work. You can send your send an error received by your client. Before a client report the Problem you might have fixed it. Clients want to hear that you already took care of the problem. Believe I practise what I preach.


Background

In this Article I will show you how to send an e-mail using Gmail smtp. You must have a Gmail Account before you can do this example. Almost all programmers have this account. If you don’t have one you can create it here www.Gmail.com and choose the Signup option.




Using the Code

We are going to user C# as our language.



Start

Open Visual Studio and Create a New Website. Automatically you will have an empty page defined for you like this

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

   

    </div>

    </form>

</body>

</html>

 

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

        <title>Untitled Page</title>

    </head>

    <body>

        <form id="form1" runat="server">

        <div>

            &nbsp;<br />

            <br />

            <strong>&nbsp;To : </strong>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<asp:TextBox ID="txtto"

                runat="server" Width="445px"></asp:TextBox><br />

            &nbsp; &nbsp; &nbsp;&nbsp;

            <br />

            <strong>from: &nbsp;</strong> &nbsp; &nbsp; &nbsp;<asp:TextBox ID="txtfrom" runat="server"

                Width="168px"></asp:TextBox><br />

            <br />

            <strong>Subject:&nbsp;</strong> &nbsp;

            <asp:TextBox ID="txtsubject" runat="server" Width="449px"></asp:TextBox><br />

            <br />

            <strong>Message:&nbsp;</strong><br />

            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

            <asp:TextBox ID="txtmessage" runat="server" Height="202px" Width="451px"></asp:TextBox>

            &nbsp;<br />

            &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

            <br />

            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

            <asp:Button ID="btnSend" runat="server" Text="Send" Width="62px" OnClick="btnSend_Click" />

            <asp:Button ID="btncancel" runat="server" Text="Cancel" /></div>

        </form>

    </body>

    </html>

 

 

The next step is to add a code on our server side. Let us go to our send Button and add the following code

 

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Net.Mail;

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void btnSend_Click(object sender, EventArgs e)

    {

        MailMessage mm = new MailMessage();

 

        SmtpClient smtp = new SmtpClient();

 

        mm.From = new MailAddress(txtfrom.Text);

 

        mm.To.Add(new MailAddress(txtto.Text));

 

        // mm.To.Add(new MailAddress("moreemail@vuyiswa.co.za"));

 

        mm.Subject = txtsubject.Text;

 

        mm.Body = txtmessage.Text;

 

        mm.IsBodyHtml = true;

 

        smtp.Host = "smtp.gmail.com"; //You can add this in the webconfig

 

        smtp.EnableSsl = true;

 

        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();

 

        NetworkCred.UserName = "vuyiswamb@gmail.com";

 

        NetworkCred.Password = "wowOops";

        smtp.UseDefaultCredentials = true;

 

        smtp.Credentials = NetworkCred;

 

        smtp.Port = 587; //this is Gmail port for e-mail

 

        smtp.Send(mm);//send an e-mail

    }

}

 

 

When the send button is clicked the e-mail will be send to the receipeint. You can add more e-mail. If the Textbox has e-mails separated by a semicolon then you can split the string and use the semicolon as a delimiter. 



Conclusion

This code can be coded in Exception handling code that is trapped in the global.asa and send an e-mail to yourself than showing the yellow screen of death.

Thank you for visiting DotnetFunda.

Vuyiswa Maseko



If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:8 year(s)
Home page:http://www.Dotnetfunda.com
Member since:Sunday, July 06, 2008
Level:NotApplicable
Status: [Member] [MVP] [Administrator]
Biography:Vuyiswa Junius Maseko is a programmer and a moderator in ".NetFunda. Vuyiswa has been developing for 8 years now. his major strength are C# 1.1,2.0,3.0,3.5 and sql and his interest are in Silverlight,WPF,C#. He has been doing a lot of Silverlight development. He has been using .net since the beta version of it. He is also an online Trainer at www.Itfunda.com. Thanks to people like Sheo Narayan (.Netfunda) , Chris Maunder (codeproject), Colin Angus Mackay (codeproject), Dave Kreskowiak (Codeproject),.They have made vuyiswa what he is today.
 Responses
Posted by: Chikul | Posted on: 24 Dec 2009 01:27:16 AM

Hi Vuyiswamb,

I tried this one. but its throwing error.
"The remote name could not be resolved: 'smtp.gmail.com' "

Is there any other modifications i need to do .

Even <%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %> also not required i think.

Regards,
Chikul


Posted by: Vuyiswamb | Posted on: 12 Feb 2010 09:50:30 AM

Hi Chikul

You are right that part id not required but its not a trainsmash. i was busy with other things while preparing the article. If you receive this error. It means that your username or Password is not correct. Because the Host and the Port are Correct and they dont change. Try to use your Gmail account. i hope you did not try to use my e-mail and my password because the password is not real.

Thank you for Posting at DotnetFunda

Vuyiswa Maseko


Posted by: Pann | Posted on: 27 Jul 2010 04:41:59 AM

hello
I've got error this message

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.127.109:587

plz reply me how to do this.
Best Regards.
Pann

Posted by: Vuyiswamb | Posted on: 28 Jul 2010 02:25:07 AM

Good Day Pann

Please show me your code and i will what i s wrong for you

>> Write Response - Respond to this post and get points
Related Posts

This article describes some ASP.NET Ajax commonly asked questions and answers.

In this example i am implementing the AutoComplete functionality to textbox in the EditItemTemaplate of GridView using AJAX autocomplete extender, for this we need to create a web service which calls the method to fetch data from database and display results as suggestions for textbox

Every Article I write is like a story. There are a lot of Companies that provide readymade controls that skinable and make out GUI (Graphical User interface) looks good. Spending time inventing a Wheel is not a Good idea. A lot of Developers or Development houses should look at Infragistics (www.Infragistics.com), ComponentArt and Telerik. There are more of them but these are Microsoft partners and they have made their mark in providing readymade Controls that you can use in your Visual Studio. But there are times where we don’t have a Budget and we been Controls like Date Picker. Well if you check the Visual Studio Toolbox, there is no Such Control.

In this example I create a validation control. It checks if the TextBox is Empty it shows the error message.

This article explains how to create Gmail like loading indicator.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 7:30:01 AM