Introduction: In this article today I am going to explain how we can format the emails using Stringbulider
in asp.net
Description:
In the previous article i have explained Disable copy, cut, paste and drop options in asp.net textbox using Jquery, Explain cookies in asp.net OR Explain cookies with example in asp.net, Explain viewstate in asp.net OR Viewstate and its advantage or disadvantages and State Management in Asp.net.
Firstly check this article
Send email in asp.net
Add a wedform to project
and design the page as shown below:
<fieldset style="width:400px">
<legend>Format
the Emails using StringBuilder Example</legend>
<table>
<tr><td>Email Sent To
:</td><td> <asp:TextBox ID="txtemail"
runat="server"></asp:TextBox></td></tr>
<tr><td></td><td><asp:Button ID="btnsend" runat="server" Text="Send Email" onclick="Button1_Click" /></td></tr>
<tr><td></td><td><label id="lblmessage" runat="server"></label></td></tr>
</table>
</fieldset>
On button click write the
below given code (C#):
using System.Net.Mail;
protected
void Button1_Click(object
sender, EventArgs e)
{
sendemail();
}
public void sendemail()
{
try
{
System.Text.StringBuilder mailBody = new
System.Text.StringBuilder();
mailBody.Append("<table width=\"100%\"
border=\"0\" align=\"center\" cellpadding=\"4\"
cellspacing=\"2\" style=\"font-family:Arial,Verdana\">
");
mailBody.Append("<tr>");
mailBody.Append("<td style='background-color: #f4d687'>");
mailBody.Append("Welcome to Articlemirror,");
mailBody.Append("</tr>");
mailBody.Append("</td>");
mailBody.Append("<tr><td><img src=
\"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7ZcpCp-jZA2SFIxe2mG5IqeKsO21cBjDwpDB46jYKZZWEv8s73s8Fs0fEeOwBNubtvH0MReysO23uHDPGMo1r3mBTMPgjS9Q3x7VNfe-dwAPdOCFH70AkfCgUdkM3vZNct4n9aIuXHcVN/s1600/am3.png\"
/></td></tr>");
mailBody.Append("<tr><td>Thank you for joining us. You
are the Mebmer of Articlemirror.</td></tr>");
mailBody.Append("<tr><td><a href=\"http://articlemirror.blogspot.in\">Click
here to go Your Webiste</a></td></tr>");
mailBody.Append("<tr><td></td><td><b>Regards,</b></td></tr>");
mailBody.Append("<tr><td></td><td
style='background-color: #0696AF;color:#ffffff'><b>Article
Mirror</b></td></tr>");
mailBody.AppendLine("</table>");
string
Email = ConfigurationManager.AppSettings["Email"].ToString();
string
Emailto = txtemail.Text;
MailMessage
mail = new MailMessage(Email,
Emailto);
mail.Subject = "Article Mirror";
mail.Body =
mailBody.ToString();
mail.IsBodyHtml = true;
SmtpClient
smtp = new SmtpClient();
smtp.Send(mail);
lblmessage.InnerText ="Email send
successfully";
txtemail.Text = "";
}
catch
(Exception ex)
{
}
}
In VB:
Imports
System.Net.Mail
Protected
Sub Button1_Click(ByVal
sender As Object,
ByVal e As
EventArgs) Handles btnsend.Click
sendemail()
End Sub
Public Sub sendemail()
Try
Dim
mailBody As New
System.Text.StringBuilder()
mailBody.Append("<table width=""100%""
border=""0"" align=""center""
cellpadding=""4"" cellspacing=""2""
style=""font-family:Arial,Verdana""> ")
mailBody.Append("<tr>")
mailBody.Append("<td style='background-color: #f4d687'>")
mailBody.Append("Welcome to Articlemirror,")
mailBody.Append("</tr>")
mailBody.Append("</td>")
mailBody.Append("<tr><td><img src=
""https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7ZcpCp-jZA2SFIxe2mG5IqeKsO21cBjDwpDB46jYKZZWEv8s73s8Fs0fEeOwBNubtvH0MReysO23uHDPGMo1r3mBTMPgjS9Q3x7VNfe-dwAPdOCFH70AkfCgUdkM3vZNct4n9aIuXHcVN/s1600/am3.png""
/></td></tr>")
mailBody.Append("<tr><td>Thank you for joining us. You
are the Mebmer of Articlemirror.</td></tr>")
mailBody.Append("<tr><td><a
href=""http://articlemirror.blogspot.in"">Click here to
go Your Webiste</a></td></tr>")
mailBody.Append("<tr><td></td><td><b>Regards,</b></td></tr>")
mailBody.Append("<tr><td></td><td
style='background-color: #0696AF;color:#ffffff'><b>Article
Mirror</b></td></tr>")
mailBody.AppendLine("</table>")
Dim
Email As String
= ConfigurationManager.AppSettings("Email").ToString()
Dim
Emailto As String
= txtemail.Text
Dim
mail As New
MailMessage(Email, Emailto)
mail.Subject = "Article Mirror"
mail.Body = mailBody.ToString()
mail.IsBodyHtml = True
Dim
smtp As New
SmtpClient()
smtp.Send(mail)
lblmessage.InnerText = "Email send successfully"
Catch
ex As Exception
End Try
End Sub
No comments:
Post a Comment