How to send mail using Asp.net
Source code :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 49%;
color: #00FFCC;
}
.style2
{
width: 187px;
}
.style3
{
color: #3399FF;
}
.style4
{
text-align: center;
}
.style5
{
width: 187px;
color: #000099;
}
.style6
{
color: #000099;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="style3">
<h1 class="style4">
<strong><em>Sending Mail
</em></strong>
</h1>
</div>
<div>
<div>
<h2>
<strong><em>Welcome
</em></strong>
</h2>
</div>
</div>
<div>
<table class="style1"
align="center"
bgcolor="#99CCFF">
<tr>
<td
class="style5">
Your Mail Address<strong>:
</strong>
</td>
<td
bgcolor="#99CCFF">
<asp:TextBox ID="txtMailAddress"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td
class="style2">
<span
class="style6"><strong>Your Mail
Password:</strong></span>
</td>
<td
bgcolor="#99CCFF">
<asp:TextBox ID="txtMailPassword"
runat="server"
TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td
class="style5">
<strong>To</strong></td>
<td
bgcolor="#99CCFF">
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td
class="style5">
<strong>Subject:
</strong>
</td>
<td
bgcolor="#99CCFF">
<asp:TextBox ID="txtSubject"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td
class="style5">
<strong>Attachments :</strong></td>
<td
bgcolor="#99CCFF">
<asp:FileUpload ID="fupAttach"
runat="server"
/>
</td>
</tr>
<tr>
<td
class="style5"
style="font-weight: 700">
Message
Body:
</td>
<td
bgcolor="#99CCFF">
</td>
</tr>
<tr>
<td
class="style2">
</td>
<td
bgcolor="#99CCFF">
<asp:TextBox ID="txtBody"
runat="server"
Height="113px"
TextMode="MultiLine"
Width="300px"></asp:TextBox>
</td>
</tr>
<tr>
<td
class="style2">
</td>
<td
bgcolor="#99CCFF">
<asp:Button ID="Btn_SendMail"
runat="server"
onclick="Btn_SendMail_Click"
Text="Send Email"
/>
</td>
</tr>
<tr>
<td
class="style2">
</td>
<td
bgcolor="#99CCFF">
<asp:Label ID="lblResult"
runat="server"
style="color: #FF3300"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Default.aspx.cs code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
public partial class Details :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
}
protected void
Btn_SendMail_Click(object sender, EventArgs e)
{
MailMessage msg = new
MailMessage();
msg.From = new MailAddress(txtMailAddress.Text,
"Sender's Name");
msg.To.Add(new MailAddress(txtTo.Text));
msg.Subject = txtSubject.Text;
msg.Body = txtBody.Text;
msg.IsBodyHtml = true;
if(fupAttach.HasFile)
{
msg.Attachments.Add(new Attachment(fupAttach.PostedFile.InputStream,
fupAttach.FileName));
}
SmtpClient smtp = new
SmtpClient();
smtp.Host="smtp.gmail.com";
smtp.Credentials= new NetworkCredential(txtMailAddress.Text,
txtMailPassword.Text);
lblResult.Visible=true;
smtp.EnableSsl=true;
try
{
smtp.Send(msg);
lblResult.Text="Mail Sent
Sucessfully";
}
catch(Exception
ex)
{
lblResult.Text=ex.Message;
}
}
}
OutPut :
nice article. if you are maintaining any template for email.. this may help you.
ReplyDeletehttp://aspnettutorialonline.blogspot.com/2012/05/email-template-from-flat-file-or.html