Tuesday, April 30, 2013

How to send a mail in asp.net

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

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>
<form id="form1" runat="server">
<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" />
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)
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.Subject = txtSub.Text;
Respose.Write("Mail sent successfully");
catch (Exception em)

No comments: