Translate
Sending Email with asp.net C#
Files:
1. Default.aspx
2. Default.aspx.cs
Source view: I created a webform named default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HowToSendEmail.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>Send Email on asp.net c#</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<center> <asp:Label ID="lblTitle" runat="server"
Text="How to send Email with asp.net c#" Font-Size="X-Large"></asp:Label>
</center>
<table>
<tr>
<td>
<asp:Label ID="lblfrom" runat="server" Text="From: " ToolTip="sender's email"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtfrom" runat="server" ToolTip="sender's email"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblto" runat="server" Text="To: "
ToolTip="email of the person you want to send the email to"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtto" runat="server"
ToolTip="email of the person you want to send the email to"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblsmtp" runat="server" Text="SMTP:"
ToolTip="findout about your email's smtp"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtsmtp" runat="server" ToolTip="findout about your email's smtp"></asp:TextBox></td>
</tr>
<tr>
<td>
<asp:Label ID="lblusername" runat="server" Text="UserName:"
ToolTip="just enter your email again but you can try your usename"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtusername" runat="server" ToolTip="just enter your email again but you can try your usename"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblpassword" runat="server" Text="Password:"
ToolTip="your email password"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtpassword" runat="server" ToolTip="your email password"
TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Label ID="lblsubject" runat="server" Text="Subject:"
ToolTip="subject of the email"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:TextBox ID="txtsubject" runat="server" ToolTip="subject of the email"
Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Label ID="lblbody" runat="server" ToolTip="Your message here">Body:</asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:TextBox ID="txtbody" runat="server" ToolTip="Your message here" Rows="20"
TextMode="MultiLine" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="btnsend" runat="server" Text="Send" onclick="btnsend_Click" />
<br />
<asp:Label ID="lblresult" runat="server"></asp:Label>
</td>
</tr>
</table>
<br />
</div>
</form>
</body>
</html>
code behind file: the code behind file is Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace HowToSendEmail
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsend_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage(txtfrom.Text, txtto.Text, txtsubject.Text, txtbody.Text);
SmtpClient client = new SmtpClient(txtsmtp.Text);
client.Port = 587;//you can also use 25 or 587 or 465
client.Credentials = new System.Net.NetworkCredential(txtusername.Text, txtpassword.Text);
client.EnableSsl = true;
client.Send(mail);
lblresult.Text = "Email sent successfully!";
}
catch (Exception ex)
{// if an error occours while sending email it will show
//below the send button
lblresult.Text = ex.Message;
}
}
}
}
Subscribe to:
Posts
(
Atom
)
No comments :
Post a Comment