How to send Email using asp.net


In this Article I'm going to explain how to send email from asp.net. Herewith i given one example with step by step explanations.

First we need to import System.Web.Mail namespace. The system.Web.Mail namespace contains classes that enable you to construct and send messages using the CDOSYS message component.

To Add this reference follow below steps

1)On the project menu click add reference

2)After That click .net Tab, locate System.Web.ddl, and then click Select.
3)Click ok in the add references.


Design Page


<?xml version="1.0"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true"/>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
</configuration>



Coding Page



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;
using System.Net;

public partial class _Default : System.Web.UI.Page
{
protected void btnSend_Click(object sender, EventArgs e)
{
try
{
MailMessage msg = new MailMessage(txtFromMailID.Text.ToString().Trim(), txtTomailid.Text.ToString().Trim(), txtsubject.Text.ToString(), txtbody.Text.ToString());
MailAddress SendCC = new MailAddress(txtCC.Text.ToString());
msg.CC.Add(SendCC);
SmtpClient mailClient = new SmtpClient(txtsmtpaddress.Text.ToString().Trim(), Convert.ToInt32(txtPortAddress.Text.ToString().Trim()));
NetworkCredential NetCrd = new NetworkCredential(txtFromMailID.Text.ToString(), txtPassword.Text.ToString());
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = NetCrd;
if (Convert.ToInt32(txtPortAddress.Text.ToString().Trim()) == 587)
{
mailClient.EnableSsl = true;
}
else
{
mailClient.EnableSsl = false;
}
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;

mailClient.Send(msg);
Page.ClientScript.RegisterStartupScript(this.GetType(), "alertScript", "Mail Sent", true);
}
catch (Exception ex)
{ Page.ClientScript.RegisterStartupScript(this.GetType(), "alertScript", ex.Message, true);
}
}
}


Comments

Guest Author: World Traveler09 Jul 2013

Send E-mail using ASP.NET, C#
The Microsoft .NET framework provides two namespaces, System.Net and System.Net.Sockets to send or receive data over the Internet. SMTP protocol is using for sending email from C#. C# use System.Net.Mail namespace for sending email.
To send e-mail you need to configure SMTP server. If you don't have any SMTP, you can use free SMTP server. You can also use your gmail account.

The following C# source code shows how to send an email using SMTP server.

Code is here :
http://cybarlab.blogspot.com/2013/03/send-e-mail-using-c-sharp.html



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: