News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. *** 
  Dec 14 2014 8:29AM     Pragathi
  17 Comments    11536 Views  
Here Pragathi provided a short tutorial to send Email with HTML page as Body in ASP.Net using C#/VB
HTML
Below is the sample HTML code,

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <h2>
        Send HTML Template</h2>
    <div>
        <table> 
            <tr>
                <td>
                    <span>To:</span>
                </td>
                <td>
                    <asp:TextBox ID="txtReceiver" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <br >
                </td>
            </tr>
            <tr>
                <td>
                    <span>Subject:</span>
                </td>
                <td>
                    <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <br >
                </td>
            </tr>          
            <tr>
                <td>
                    <span>Attachment: </span>
                </td>
                <td>
                    <asp:FileUpload ID="fileAttachment" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    <br >
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" />
                </td>
                <td>
                    <asp:Label ID="lblMessage" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Namespaces
You will need to import the following namespace.
C#

using System.Net.Mail;
using System.IO;

VB

Imports System.Net.Mail
Imports System.IO

Code for SEND Button
Below is the sample code for SEND button "btnSend_Click" Click Event in ASP.Net C#/VB.
C#

protected void btnSend_Click(object sender, EventArgs e)
{
            bool result = sendmail_Template(txtReceiver.Text.Trim(), txtSubject.Text.Trim(), txtBody.Text.Trim());

            if (result)
                lblMessage.Text = "Email Sent successfully.";
            else
                lblMessage.Text = "Email Sending failed.";
}

VB

Protected Sub btnSend_Click(sender As Object, e As EventArgs)

         Dim result As Boolean = sendmail_Template(txtReceiver.Text.Trim(), txtSubject.Text.Trim(), txtBody.Text.Trim())

         If result Then
              lblMessage.Text = "Email Sent successfully."
         Else
              lblMessage.Text = "Email Sending failed."
         End If

End Sub

Method for Creating E-Mail Template
Below is the sample code for Creating E-Mail Template. This Email template is for Student Details.
EmailTemplate() method will return result as string.
C#

public string EmailTemplate()
{
            string EmailFormat = 
             "<div>" +
                "<span><h3>Student Details</h3></span><br />" +
                   "<table border=1 width=100% align=left cellpadding=3>" +     
                      "<th>Name</th>"+
                      "<th>Department</th>" +
                      "<th>Grade</th>" +
                          "<tr>" +
                          "<td style=width:50px><strong>Jercy</strong></td> " +
                          "<td><strong>MS</strong></td>" +
                          "<td><strong>C</strong></td> " +
                          "</tr>" +
                          "<tr>" +
                          "<td style=width:50px><strong>John</strong></td> " +
                          "<td><strong>CA</strong></td>" +
                          "<td><strong>A</strong></td> " +
                          "</tr>"+
                          "<tr>" +
                          "<td style=width:50px><strong>James</strong></td> " +
                          "<td><strong>MS</strong></td>" +
                          "<td><strong>B</strong></td> " +
                          "</tr>" +
                     "</table>" + 
               "</div>";

            return EmailFormat;
}

VB

Public Function EmailTemplate() As String
            String EmailFormat = 
             "<div>" +
                "<span><h3>Student Details</h3></span><br />" +
                   "<table border=1 width= Decimal.Remainder(100,  align)=left cellpadding=3>" +     
                      "<th>Name</th>"+
                      "<th>Department</th>" +
                      "<th>Grade</th>" +
                          "<tr>" +
                          "<td style=width:50px><strong>Jercy</strong></td> " +
                          "<td><strong>MS</strong></td>" +
                          "<td><strong>C</strong></td> " +
                          "</tr>" +
                          "<tr>" +
                          "<td style=width:50px><strong>John</strong></td> " +
                          "<td><strong>CA</strong></td>" +
                          "<td><strong>A</strong></td> " +
                          "</tr>"+
                          "<tr>" +
                          "<td style=width:50px><strong>James</strong></td> " +
                          "<td><strong>MS</strong></td>" +
                          "<td><strong>B</strong></td> " +
                          "</tr>" +
                     "</table>" + 
               "</div>"
 
            Return EmailFormat
End Function

Method for Sending E-Mail with HTML Template
Below is the sample code for Send E-Mail Method in ASP.Net C#/VB. This methods gets the parameter Receiver MailID, Subject and Body text.
Create a folder with name "attachments" in the project to save the uploaded file.
We have to set the Property IsBodyHtml is true. The code shown below,

mailMessage.Body = Template;
mailMessage.IsBodyHtml = true;

C#

public bool sendmail_Template(string recievermailid, string subject, string bodyText)
{
            try
            {
                string senderId = "********"// Sender EmailID
                string senderPassword = "********"// Sender Password      

                string Template = EmailTemplate();
                bool IsAttachment = false;
                string FileName = string.Empty;
                System.Net.Mail.Attachment attachment = null;

                System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
                mailMessage.To.Add(recievermailid);
                mailMessage.From = new MailAddress(senderId);

                mailMessage.Subject = subject;
                mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;

                mailMessage.Body = Template;
                mailMessage.IsBodyHtml = true;

                if (fileAttachment.HasFile)
                {
                    FileName = Path.GetFileName(fileAttachment.PostedFile.FileName);
                    FileName = "attachments/" + FileName;
                    fileAttachment.PostedFile.SaveAs(Server.MapPath(FileName));

                    attachment = new System.Net.Mail.Attachment(Server.MapPath(FileName));
                    mailMessage.Attachments.Add(attachment);

                    IsAttachment = true;
                }

                mailMessage.Priority = MailPriority.High;

                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Credentials = new System.Net.NetworkCredential(senderId, senderPassword);
                smtpClient.Port = 587;
                smtpClient.Host = "smtp.gmail.com";
                smtpClient.EnableSsl = true;

                object userState = mailMessage;

                try
                {
                    smtpClient.Send(mailMessage);
                     if (IsAttachment)
                    {
                        attachment.ContentStream.Close();
                        File.Delete(Server.MapPath(FileName));
                    }
                    return true;
                }
                catch (System.Net.Mail.SmtpException)
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
}

VB

Public Function sendmail_Template(recievermailid As String, subject As String, bodyText As StringAs Boolean

       Try
                Dim senderId As String = "*******"
                ' Sender EmailID
                Dim senderPassword As String = "*********"
                ' Sender Password      
                Dim Template As String = EmailTemplate()
                Dim IsAttachment As Boolean = False
                Dim FileName As String = String.Empty
                Dim attachment As System.Net.Mail.Attachment = Nothing

                Dim mailMessage As New System.Net.Mail.MailMessage()
                mailMessage.[To].Add(recievermailid)
                mailMessage.From = New (senderId)

                mailMessage.Subject = subject
                mailMessage.SubjectEncoding = System.Text.Encoding.UTF8

                mailMessage.Body = Template
                mailMessage.IsBodyHtml = True

                If fileAttachment.HasFile Then
                        FileName = Path.GetFileName(fileAttachment.PostedFile.FileName)
                        FileName = "attachments/" & FileName
                        fileAttachment.PostedFile.SaveAs(Server.MapPath(FileName))

                        attachment = New System.Net.Mail.Attachment(Server.MapPath(FileName))
                        mailMessage.Attachments.Add(attachment)

                        IsAttachment = True
                End If

                mailMessage.Priority = MailPriority.High

                Dim smtpClient As New SmtpClient()
                smtpClient.Credentials = New System.Net.NetworkCredential(senderId, senderPassword)
                smtpClient.Port = 587
                smtpClient.Host = "smtp.gmail.com"
                smtpClient.EnableSsl = True

                Dim userState As Object = mailMessage

                Try
                        smtpClient.Send(mailMessage)
                        If IsAttachment Then
                                attachment.ContentStream.Close()
                                File.Delete(Server.MapPath(FileName))
                        End If
                        Return True
                Catch generatedExceptionName As System.Net.Mail.SmtpException
                        Return False
                End Try
        Catch generatedExceptionName As Exception
                Return False
        End Try

End Function

Finally, if you are facing any issue like "The SMTP server requires a secure connection or the client was not authenticated". For that, you should enable application to access gmail account. Please Click Here to Solve.
Result
Result and Page design as Shown Below,

After sending the E-Mail, Email look like this,
I hope this page will helps to send HTML Page as E-Mail body in ASP.Net application. Thanks.
BackToTop
Comments


AnonymousUser
Jun 1 2016 7:00AM
There are some fascinating deadlines in this article however I don’t know if I see all of them center to heart. There's some validity but I'll take hold opinion until I look into it further. Good article , thanks and we want extra! Added to FeedBurner as well http://islotsonline24.ru
AnonymousUser
Jun 1 2016 12:12PM
Your home is valueble for me. Thanks!…
AnonymousUser
Jun 1 2016 11:34AM
This actually answered my problem, thanks!
AnonymousUser
Jun 1 2016 6:23PM
Nice post. I learn something tougher on completely different blogs everyday. It can all the time be stimulating to learn content material from other writers and practice a little something from their store. I’d desire to make use of some with the content on my weblog whether you don’t mind. Natually I’ll provide you with a link on your web blog. Thanks for sharing.
AnonymousUser
Jun 1 2016 6:56PM
You made some first rate points there. I seemed on the web for the problem and found most individuals will go along with together with your website.
AnonymousUser
Jun 1 2016 9:28PM
you've an amazing blog right here! would you like to make some invite posts on my blog?
AnonymousUser
Jun 1 2016 9:28PM
you've an amazing blog right here! would you like to make some invite posts on my blog?
AnonymousUser
Jun 2 2016 1:52AM
Aw, this was a really nice post. In thought I wish to put in writing like this moreover – taking time and actual effort to make a very good article… however what can I say… I procrastinate alot and in no way appear to get something done.
AnonymousUser
Jun 2 2016 5:24PM
I’m impressed, I need to say. Really not often do I encounter a weblog that’s each educative and entertaining, and let me tell you, you could have hit the nail on the head. Your thought is excellent; the difficulty is something that not sufficient persons are speaking intelligently about. I'm very completely satisfied that I stumbled throughout this in my seek for something referring to this.
AnonymousUser
Jun 2 2016 5:56PM
?I was more than happy to seek out this internet-site.I wished to thanks on your time for this glorious learn!! I undoubtedly enjoying every little little bit of it and I have you bookmarked to take a look at new stuff you blog post.
AnonymousUser
Jun 3 2016 9:16AM
I am typically to running a blog and i actually admire your content. The article has actually peaks my interest. I am going to bookmark your web site and maintain checking for brand spanking new information.
AnonymousUser
Jun 3 2016 6:52PM
WONDERFUL Post.thanks for share..more wait .. …
AnonymousUser
Jun 3 2016 9:56PM
Oh my goodness! an incredible article dude. Thanks Nevertheless I am experiencing problem with ur rss . Don’t know why Unable to subscribe to it. Is there anybody getting equivalent rss drawback? Anyone who knows kindly respond. Thnkx
AnonymousUser
Jul 10 2017 7:12PM
20zUya I truly appreciate this blog.Thanks Again. Awesome.
AnonymousUser
Sep 20 2017 6:44AM
Cfas6N Thanks for sharing this great piece. Very inspiring! (as always, btw)
AnonymousUser
Nov 8 2017 10:27AM
Oaj8Cs This is one awesome article post.Thanks Again. Really Great.
AnonymousUser
Sep 20 2018 5:36AM
awesome

 
Search
Recent Posts
Create Amazon ElasticCache Using Memcached in CSharp
Oct 29 2018 12:09PM Posted By Amose
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Oct 28 2018 12:09PM Posted By Pranav
Google URL Shortener in C#
Oct 20 2018 12:09PM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Oct 5 2018 12:09PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Sep 26 2018 12:09PM Posted By John
Gridview custom CSS in ASP.Net
Sep 14 2018 12:09PM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Sep 10 2018 12:09PM Posted By Micheal
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: