This is featured post 1 title
Replace these every slider sentences with your featured post descriptions.Go to Blogger edit html and find these sentences.Now replace these with your own descriptions.This theme is
This is featured post 2 title
Replace these every slider sentences with your featured post descriptions.Go to Blogger edit html and find these sentences.Now replace these with your own descriptions.This theme is Bloggerized by Lasantha - Premiumbloggertemplates.com.
This is featured post 3 title
Replace these every slider sentences with your featured post descriptions.Go to Blogger edit html and find these sentences.Now replace these with your own descriptions.This theme is Bloggerized by Lasantha - Premiumbloggertemplates.com.
Showing posts with label Send Email. Show all posts
Showing posts with label Send Email. Show all posts
Friday, 6 July 2012
How to Send Email from ASP .NET
00:50
Unknown
Introduction
In ASP.NET, sending emails has become simpler. The classes required to send an email are contained in the System.Net.Mail. The steps required to send an email from ASP.NET are as follows: Below is sample code showing how to send email from ASP.Net using C#. With this code send main from Gmail SMTP server you can also configure your own SMTP server to change in web config file.
To configure SMTP configuration data for ASP.NET, you would add the following tags to your web.Config file.
<system.net>
<mailSettings>
<smtpfrom="YourEmailid@gmail.com" deliveryMethod="Network">
<networkhost="smtp.gmail.com" port="587"
userName="YourEmailid@gmail.com"
password="yourPassword"/>
smtp>
mailSettings>
system.net>
Here is the Default.aspx page:
<%@ 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 Pagetitle>
<style type="text/css">
table.hovertable
{
font-family: verdana,arial,sans-serif;
font-size: 11px;
color: #333333;
border-width: 1px;
border-color: #999999;
border-collapse: collapse;
}
table.hovertable th
{
background-color: #c3dde0;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
table.hovertabletr
{
background-color: #d4e3e5;
}
table.hovertabletd
{
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
style>
<script type="text/javascript">
functionEmailvalidation()
{
if(document.getElementById("txtBoxTo").value=="")
{
alert('Please Enter Email ID');
document.getElementById("txtBoxTo").focus();
returnfalse;
}
varemailPat = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
varemailid=document.getElementById("txtBoxTo").value;
varmatchArray = emailid.match(emailPat);
if(matchArray == null)
{
alert('Please Enter Correct Email ID');
document.getElementById("txtBoxTo").focus();
returnfalse;
}
if(document.getElementById("txtBoxCC").value=="")
{
alert('Please Enter Email ID');
document.getElementById("txtBoxCC").focus();
returnfalse;
}
varemailPat = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
varemailid=document.getElementById("txtBoxCC").value;
varmatchArray = emailid.match(emailPat);
if(matchArray == null)
{
alert('Please Enter Correct Email ID');
returnfalse;
}
}
script>
head>
<body>
<form id="form1" runat="server">
<div>
<table class="hovertable"align="center"width="44%">
<tr>
<td>
To
td>
<td>
<asp:TextBox ID="txtBoxTo"runat="server">asp:TextBox>
td>
tr>
<tr>
<td>
CC
td>
<td>
<asp:TextBox ID="txtBoxCC"runat="server">asp:TextBox>
td>
tr>
<tr>
<td>
Subject
td>
<td>
<asp:TextBox ID="txtBoxSubject"runat="server">asp:TextBox>
td>
tr>
<tr>
<td>
Message
td>
<td>
<asp:TextBox ID="txtareaMessage"runat="server" TextMode="MultiLine">asp:TextBox>
td>
tr>
<tr>
<td>
td>
<td>
td>
tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnSend"runat="server"Text="Send" OnClientClick="return Emailvalidation()"
OnClick="btnSend_Click" />
td>
tr>
<tr>
<td>
td>
<td>
td>
tr>
table>
div>
form>
body>
html>
Here is the source code of the Default.aspx.cs page:
using System;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
usingSystem.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(objectsender, EventArgs e)
{
}
protected void btnSend_Click(objectsender, EventArgs e)
{
SmtpClientsmtp = new SmtpClient();
MailMessagemsg = new MailMessage();
smtp.EnableSsl = true;
msg.IsBodyHtml = true;
msg.To.Add(newMailAddress(txtBoxTo.Text));
if(txtBoxCC.Text != "")
msg.CC.Add(newMailAddress(txtBoxCC.Text));
msg.Subject = txtBoxSubject.Text;
msg.Body = txtareaMessage.Text;
smtp.EnableSsl = true;
objectstate = msg;
smtp.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
smtp.Send(msg);
fnAlert("Email Sent Successfully.");
}
private static voidsmtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgse)
{
MailMessagemail = e.UserState as MailMessage;
if(!e.Cancelled && e.Error != null)
{ }
else
{ }
}
}
Conclusion:
In this article, we explored how easy it is to send Email’s using the classes in System.Net.Mail. This approach of sending mails will remain the same for web as well as windows application.
Downloads
You can download the complete source code in C# .
Sunday, 18 July 2010
Sending Email with ASP.NET
22:53
Unknown
Code For Default.aspx Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%" cellpadding="2" cellspacing="2" class="tdtable">
<tr>
<td class="tdbigblock">Your Name*:</td>
<td class="headerleft">
<asp:TextBox ID="txtName" runat="server" Width="250px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvName" runat="server"
ControlToValidate="txtName" ErrorMessage="name can not be empty"
Display="None" ValidationGroup="submit"></asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="vceName" runat="server"
TargetControlID="rfvName">
</cc1:ValidatorCalloutExtender>
</td>
</tr>
<tr>
<td colspan="2" class="tdtextwhite"> </td>
</tr>
<tr>
<td class="tdbigblock">Your Email*:</td>
<td class="headerleft">
<asp:TextBox ID="txtEmail" runat="server" Width="250px"></asp:TextBox>
<asp:RegularExpressionValidator ID="revEmail" runat="server"
ControlToValidate="txtEmail" Display="None"
ErrorMessage="Enter correct email address"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ValidationGroup="submit"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="rfvEmail" runat="server"
ControlToValidate="txtEmail" ErrorMessage="email can not be empty"
Display="None" ValidationGroup="submit"></asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="vceEmail" runat="server"
TargetControlID="rfvEmail">
</cc1:ValidatorCalloutExtender>
<cc1:ValidatorCalloutExtender ID="vceyouremail" runat="server"
TargetControlID="revEmail">
</cc1:ValidatorCalloutExtender>
</td>
</tr>
<tr>
<td colspan="2" class="tdtextwhite"> </td>
</tr>
<tr>
<td class="tdbigblock">Contact No.*:</td>
<td class="headerleft">
<asp:TextBox ID="txtContactNo" runat="server" Width="250px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvMobile" runat="server"
ControlToValidate="txtContactNo" Display="None"
ErrorMessage="Mobile number can not empty" ValidationGroup="\d{10}"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="rfvContactNo" runat="server"
ControlToValidate="txtContactNo"
ErrorMessage="contact no can not be empty" Display="None" ValidationGroup="submit"></asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="vceMobileno" runat="server"
TargetControlID="rfvContactNo">
</cc1:ValidatorCalloutExtender>
<cc1:ValidatorCalloutExtender ID="vceContactno" runat="server"
TargetControlID="rfvMobile">
</cc1:ValidatorCalloutExtender>
</td>
</tr>
<tr>
<td colspan="2" class="tdtextwhite"> </td>
</tr>
<tr>
<td class="tdbigblock">Subject*:</td>
<td class="headerleft">
<asp:TextBox ID="txtSubject" runat="server" Width="250px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvSubject" runat="server"
ControlToValidate="txtSubject" ErrorMessage="Subject can not be empty"
Display="None" ValidationGroup="submit"></asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="vceSubject" runat="server"
TargetControlID="rfvSubject">
</cc1:ValidatorCalloutExtender>
</td>
</tr>
<tr>
<td colspan="2" class="tdtextwhite"> </td>
</tr>
<tr>
<td class="tdbigblock">Message*:</td>
<td class="headerleft">
<asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine" Width="300px"></asp:TextBox>
<cc1:ValidatorCalloutExtender ID="vceMessage" runat="server"
TargetControlID="rfvMessage">
</cc1:ValidatorCalloutExtender>
<asp:RequiredFieldValidator ID="rfvMessage" runat="server"
ErrorMessage="message can not be empty" ControlToValidate="txtMessage"
Display="None" ValidationGroup="submit"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2" class="tdtextwhite"> </td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" ValidationGroup="submit" /> </td>
</tr>
<tr>
<td></td>
<td>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
public partial class Contactus : System.Web.UI.Page
{
#region "Variables"
#endregion
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// Method to clear vales of all control of the form
/// </summary>
public void ClearAllControl()
{
txtName.Text = "";
txtEmail.Text = "";
txtContactNo.Text = "";
txtSubject.Text = "";
txtMessage.Text = "";
}
/// <summary>
/// Method to send vales of all control of the form in email
/// </summary>
public void SendMailContactusDetail()
{
if (txtEmail.Text != "")
{
MailMessage msg = new MailMessage();
SmtpClient smtp = new SmtpClient("mail.yourSMTPSERVER.com ");// SmtpClient smtp = new SmtpClient("mail.gmail.com");
msg.To.Add("sunilgurjr@gmail.com");
msg.To.Add("sunilgurjr@gmail.com ");
msg.From = new MailAddress(this.txtEmail.Text);
msg.Subject = this.txtSubject.Text;
msg.IsBodyHtml = true;
System.Net.NetworkCredential Netcred = new System.Net.NetworkCredential();
Netcred.UserName = "your UserName Email ID";
Netcred.Password = "Your Password";
smtp.UseDefaultCredentials = true;
smtp.Credentials = Netcred;
msg.Priority = MailPriority.High;
msg.Body = "<html><body><table border=0 cellpadding=4 cellspacing=4 width=100%>" +
"<tr><td><b><font size=5>Requirement Send User</font></b></td></tr> " +
"<tr><td><b>Name</b></td><td>----</td><td>" + this.txtName.Text + " </td></tr> " +
"<tr><td><b>Email Address</b></td><td>----</td><td>" + this.txtEmail.Text + "</td></tr> " +
"<tr><td><b>Phone Number</b></td><td>---</td><td>" + this.txtContactNo.Text + " </td></tr> " +
"<tr><td><b>Subject</b></td><td>---</td><td>" + this.txtSubject.Text + " </td></tr> " +
"<tr><td><b>Message</b></td><td>---</td><td><p align=justify>" + this.txtMessage.Text + "</td></tr></table></body></html>";
try
{
smtp.Send(msg);
Page.RegisterStartupScript("Reminder1", "<script> alert(' Thanks for Sending your Enquiry ')</script>");
txtName.Text = "";
txtEmail.Text = "";
txtContactNo.Text = "";
txtSubject.Text = "";
txtMessage.Text = "";
}
catch (Exception ex)
{
}
}
else
{
Response.Write("<script>alert('Please Enter Email Address')</script>");
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
SendMailContactusDetail();
ClearAllControl();
}
}

