| Next Tip?
Home » Asp.net, Tutorial

Send Email Using Asp.Net and C#

30 October 2008 536 views One Comment
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

b>Send Email Using Asp.Net and C#

The following is the Sample code which can be used to send emails through ASP.Net C#…. it is a working code, I have tested several times. and using it in many live sites….

This is the aspx page

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

<!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>Send Email --Meetu Choudhary</title> <style type="text/css">

<!--.style3 { font-size: 16px; font-weight: bold; font-family: Verdana, Arial, Helvetica, sans-serif; color: #FFFFFF; }
.style4 { font-weight: bold; font-size: 12px; }
.style8 {font-family: Verdana, Arial, Helvetica, sans-serif}
.style10 {font-size: 12px}
.style20 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold; }
.style23 {font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif;}
.style24 { font-size: 14px; color: #0000FF; } -->
</style>

</head>

<body >

<form id="form1" runat="server">
<div>
<table width="568" border="0" cellpadding="0">
<tr> <td align="right"> <strong>
<asp:Label ID="lblemailid" runat="server" Text="EmailID:">
</asp:Label> </strong> </td> <td>
<asp:TextBox ID="txtEmailId" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="" ControlToValidate="txtEmailId" Text="*Required Field"></asp:RequiredFieldValidator>
<%--<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Text="*Invalid EmailID">
</asp:RegularExpressionValidator>--%> </td> </tr> <tr>
<td align="right"> <strong> <asp:Label ID="Label1" runat="server" Text="Name:"></asp:Label> </strong> </td> <td>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="" ControlToValidate="txtname" Text="*Required Field"></asp:RequiredFieldValidator> </td> </tr> <tr>
<td align="right"> <strong>
<asp:Label ID="Label2" runat="server" Text="Address:"></asp:Label> </strong> </td> <td> <asp:TextBox ID="txtaddress" runat="server" TextMode="MultiLine"></asp:TextBox> </td> </tr> <tr>
<td align="right"> <strong> <asp:Label ID="Label3" runat="server" Text="City:"></asp:Label> </strong> </td>
<td> <asp:TextBox ID="txtcity" runat="server"></asp:TextBox> </td> </tr> <tr> <td align="right"> <strong> <asp:Label ID="Label4" runat="server" Text="State:"></asp:Label> </strong> </td>
<td> <asp:TextBox ID="txtstate" runat="server"></asp:TextBox> </td> </tr> <tr>
<td align="right"> <strong> <asp:Label ID="Label5" runat="server" Text="Country:"></asp:Label> </strong> </td>
<td> <asp:TextBox ID="txtcountry" runat="server"></asp:TextBox> </td> </tr> <tr>
<td align="right"> <strong> <asp:Label ID="Label6" runat="server" Text="Zip Code:"></asp:Label> </strong> </td>
<td> <asp:TextBox ID="txtzipcode" runat="server"></asp:TextBox> </td> </tr> <tr>
<td align="right"> <strong> <asp:Label ID="Label7" runat="server" Text="Telephone No:"></asp:Label> </strong> </td>
<td> <asp:TextBox ID="txtTelephoneNo" runat="server"></asp:TextBox> </td> </tr> <tr>
<td align="right"> <strong> <asp:Label ID="Label8" runat="server" Text="MobileNo(optional):"></asp:Label> </strong> </td>
<td> <asp:TextBox ID="txtMobileNo" runat="server"></asp:TextBox> </td> </tr> <tr> <td align="right"> <strong> <asp:Label ID="Label9" runat="server" Text="Your Query:"></asp:Label> </strong> </td>
<td> <asp:TextBox ID="txtYourQuery" runat="server" TextMode="MultiLine"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ErrorMessage="" ControlToValidate="txtYourQuery" Text="*Required Field"></asp:RequiredFieldValidator> </td> </tr> <tr>
<td align="right"> <strong> <asp:Label ID="Label10" runat="server" Text="Comments & Suggestions:"></asp:Label> </strong> </td>
<td> <asp:TextBox ID="txtComments" runat="server" TextMode="MultiLine"></asp:TextBox> </td> </tr> <tr>
<td colspan="2" align="center" valign="middle" class="style23"> <strong> <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" CausesValidation="true"/> <asp:Button ID="btnReset" runat="server" Text="Reset" OnClick="btnReset_Click" CausesValidation="false"/> </strong> </td> </tr>
</table>
</div>
</form>
</body>
</html>

In the cs page we have to write the following code

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Button1_Click(object sender, EventArgs e)
{
//Calling the function SendMail

Response.Write( SendMail("meetuchoudhary@gmail.com","meetudmeet@gmail.com","meetudmeet@yahoo.com","Test Mail","Test Mail Body"));
}

public string SendMail(string toList, string from, string ccList, string subject, string body)
{
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;

try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);

//checking whether the cc mail list empty or not if not then adding the list to the cc

if (ccList != null && ccList != string.Empty) message.CC.Add(ccList);

//adding subject to the mail
message.Subject = subject; // defining that the mail body is consist of html text and should be rendered as a html document

message.IsBodyHtml = true; //adding body to the mail

message.Body = body; //please provide valid live mil server for this a dummy e.g. is taken here

smtpClient.Host = "mail.server.com"; //default port is 25 of SMTP mail server you can change it if your network administrator has changed it for some reasons

smtpClient.Port = 25; //telling the SMTP that we will use Credntials smtpClient.UseDefaultCredentials = true; //defining a credential please provide vaild emil id and the password for the networkcredentials here again I have taken a dummy e.g.

smtpClient.Credentials = new System.Net.NetworkCredential("info@server.com", "password");

//finally send the mail
smtpClient.Send(message);

// if no error occurs in the follow of the program then set the msg to any string which you want to return to the user aftersending of mail

msg = "Successful";
}
catch (Exception ex)
{
//else an error message is set for this.
msg = ex.Message;
} //return the msg to the calling function

return msg;
}

}
Thanks and Regards
Meetu Choudhary

Popularity: 3%

Post to Twitter Tweet This Post

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Ads by Lake Quincy Media

One Comment »

  • admin said:

    Hi Meetu,
    MsDotNetHeaven family welcomes you.

    Its a very good code. Please keep it up

    Thanks Admin

Leave your response!

You must be logged in to post a comment.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes