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>
<br />
<br />
<strong> To : </strong> <asp:TextBox ID="txtto"
runat="server" Width="445px"></asp:TextBox><br />
<br />
<strong>from: </strong> <asp:TextBox ID="txtfrom" runat="server"
Width="168px"></asp:TextBox><br />
<br />
<strong>Subject: </strong>
<asp:TextBox ID="txtsubject" runat="server" Width="449px"></asp:TextBox><br />
<br />
<strong>Message: </strong><br />
<asp:TextBox ID="txtmessage" runat="server" Height="202px" Width="451px"></asp:TextBox>
<br />
<br />
<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