Sample 3 - ASP.Net form mail coding-General Print

  • 0

ASP.Net form mail coding

Please find the sample code for asp.net form mail coding.

Default.aspx:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
Email ID:<asp:TextBox ID="TextBox1" runat="server" Width="412px"></asp:TextBox>
<br />
<br />
<asp:Label ID="lblMessage" runat="server"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send Mail" />

</div>
</form>
</body>
</html>

Default.aspx.cs:

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

try
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(TextBox1.Text);
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
message.From = new System.Net.Mail.MailAddress("test@yourdomainname.com", "password");
message.Bcc.Add("test@yourdomainname.com");
message.Subject = "Test Mail";
message.Body = "Test";
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Host = "ServerIP";
client.Port = 25;
string userName = "test@yourdomainname.com";
string password = "Password";

client.Credentials = new NetworkCredential(userName, password);
client.Send(message);
lblMessage.Text = "Mail send Successfully";
}
catch (Exception ex) { lblMessage.Text = ex.Message; }

}

}


 


Was this answer helpful?

« Back