Tuesday, April 30, 2013

How to send a mail in asp.net

Title: How to send an email in asp.net using c#.net

Description:
In previous posts i have given send a mail with attachment in asp.net,Send mail using service,send a Email with html Format in Asp.Net.In the same way i just given an example on sending email concept in asp.net.The following example has two text boxes which are used to give the subject and name to mail message
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Send a mail in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtSub" runat="server"></asp:TextBox>
<asp:TextBox ID="txtbody" runat="server"></asp:TextBox>
<asp:Button ID="btnsendmail" runat="server" Text="Send" onclick="btnsendmail_Click" />
</form>
</body>
</html>
Codebehind:
using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Mail.Net;

public partial class _sendmail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsendmail_Click(object sender, EventArgs e)
{
try
{
SmtpClient SmtpmServer = new SmtpClient(SmtpserverName);
SmtpmServer.Credentials = new System.Net.NetworkCredential(Username, PWD);
MailMessage MlMsg = new MailMessage();
MlMsg.From = new MailAddress("bhaskar7698@gmail.com");
MlMsg.Body = txtbody.Text;;
MlMsg.IsBodyHtml = true;
MlMsg.To.Add(mulebhaskarareddy@gmail.com);
MlMsg.Subject = txtSub.Text;
SmtpmServer.Send(MlMsg);
Respose.Write("Mail sent successfully");
}
catch (Exception em)
{
Respose.Write(em.Message.Tostring());
}
}
}

No comments:

Bel