Friday, November 14, 2014

Send email content as attachment in PDF format in asp.net

Introduction: In this article today i will explain how we can send email content as attachment in PDF format in asp.net.

Description:

Firstly check this article Send email in asp.net

To generate the PDF file of email content i am using iTextsharp DLL. To download the DLL Click Here.
Add the reference of DLL to your Project/website.
HTML markup:
<fieldset style="width:400px">
    <legend>Send Email content as PDF attachment</legend>
    <table>
    <tr><td>Email Sent To :</td><td>  <asp:TextBox ID="txtemail" runat="server"></asp:TextBox></td></tr>
    <tr><td></td><td></td></tr>
    <tr><td><strong>Fill below given information to send</strong></td><td></td></tr>
    <tr><td>Name</td><td>  <asp:TextBox ID="txtname" runat="server"></asp:TextBox></td></tr>
    <tr><td>Subject</td><td>  <asp:TextBox ID="txtsubject" runat="server"></asp:TextBox></td></tr>
    <tr><td>Email</td><td>  <asp:TextBox ID="txtemailid" runat="server"></asp:TextBox></td></tr>
    <tr><td>Message:</td><td>  <asp:TextBox ID="txtmessage" runat="server"
            TextMode="MultiLine"></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 iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.IO;
using System.Net.Mail;

protected void Button1_Click(object sender, EventArgs e)
    {      
        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><td><img src= \"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7ZcpCp-jZA2SFIxe2mG5IqeKsO21cBjDwpDB46jYKZZWEv8s73s8Fs0fEeOwBNubtvH0MReysO23uHDPGMo1r3mBTMPgjS9Q3x7VNfe-dwAPdOCFH70AkfCgUdkM3vZNct4n9aIuXHcVN/s1600/am3.png\" /></td></tr>");
            mailBody.Append("<tr>");
            mailBody.Append("<td>");
            mailBody.Append("<table width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"4\" cellspacing=\"2\" style=\"font-family:Arial,Verdana\"> ");
            mailBody.Append("<tr style='background-color: #f4d687'>");
            mailBody.Append("<td>");
            mailBody.Append("Name");
            mailBody.Append("</td>");    
            mailBody.Append("<td>");
            mailBody.Append("" + txtname.Text + "");         
            mailBody.Append("</td>");
            mailBody.Append("</tr>");
            mailBody.Append("<tr style='background-color: #f4d687'>");
            mailBody.Append("<td>");
            mailBody.Append("Subject");
            mailBody.Append("</td>");
            mailBody.Append("<td>");
            mailBody.Append("" + txtsubject.Text + "");
            mailBody.Append("</td>");
            mailBody.Append("</tr>");
            mailBody.Append("<tr style='background-color: #f4d687'>");
            mailBody.Append("<td>");
            mailBody.Append("Email");
            mailBody.Append("</td>");
            mailBody.Append("<td>");
            mailBody.Append("" + txtemailid.Text + "");
            mailBody.Append("</td>");
            mailBody.Append("</tr>");
            mailBody.Append("<tr style='background-color: #f4d687'>");
            mailBody.Append("<td>");
            mailBody.Append("Message ");
            mailBody.Append("</td>");
            mailBody.Append("<td>");
            mailBody.Append("" + txtmessage.Text + "");
            mailBody.Append("</td>");
            mailBody.Append("</tr>");
            mailBody.AppendLine("</table>");           
            mailBody.Append("</td>");
            mailBody.Append("</tr>");
            mailBody.Append("<tr><td>Thanks for contacting us we will get in touch with you shortly.</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><b>Regards,</b></td></tr>");
            mailBody.Append("<tr><td style='background-color: #0696AF;color:#ffffff'><b>Article Mirror Team</b></td></tr>");
            mailBody.AppendLine("</table>");
            StringReader sr = new StringReader(mailBody.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            MemoryStream memoryStream = new MemoryStream();           
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
                byte[] bytes = memoryStream.ToArray();
                memoryStream.Close();
                string Email = ConfigurationManager.AppSettings["Email"].ToString();
                string Emailto = txtemail.Text;
                MailMessage mail = new MailMessage(Email, Emailto);
                mail.Subject = "Contact us page Enquiry";
                mail.Body = mailBody.ToString();
                mail.Attachments.Add(new Attachment(new MemoryStream(bytes), "conatctPDF.pdf"));
                mail.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Send(mail);
                lblmessage.InnerText = "Email send successfully";
                Clear();             
        }
        catch (Exception ex)
        {
        }        
    }
    public void Clear()
    {
        txtemail.Text = "";
        txtname.Text = string.Empty;
        txtmessage.Text = string.Empty;
        txtemailid.Text = string.Empty;
        txtsubject.Text = string.Empty;
    }

In VB:

Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html.simpleparser
Imports System.IO
Imports System.Net.Mail

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles btnsend.Click
        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><td><img src= ""https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7ZcpCp-jZA2SFIxe2mG5IqeKsO21cBjDwpDB46jYKZZWEv8s73s8Fs0fEeOwBNubtvH0MReysO23uHDPGMo1r3mBTMPgjS9Q3x7VNfe-dwAPdOCFH70AkfCgUdkM3vZNct4n9aIuXHcVN/s1600/am3.png"" /></td></tr>")
            mailBody.Append("<tr>")
            mailBody.Append("<td>")
            mailBody.Append("<table width=""100%"" border=""0"" align=""center"" cellpadding=""4"" cellspacing=""2"" style=""font-family:Arial,Verdana""> ")
            mailBody.Append("<tr style='background-color: #f4d687'>")
            mailBody.Append("<td>")
            mailBody.Append("Name")
            mailBody.Append("</td>")
            mailBody.Append("<td>")
            mailBody.Append("" + txtname.Text & "")
            mailBody.Append("</td>")
            mailBody.Append("</tr>")
            mailBody.Append("<tr style='background-color: #f4d687'>")
            mailBody.Append("<td>")
            mailBody.Append("Subject")
            mailBody.Append("</td>")
            mailBody.Append("<td>")
            mailBody.Append("" + txtsubject.Text & "")
            mailBody.Append("</td>")
            mailBody.Append("</tr>")
            mailBody.Append("<tr style='background-color: #f4d687'>")
            mailBody.Append("<td>")
            mailBody.Append("Email")
            mailBody.Append("</td>")
            mailBody.Append("<td>")
            mailBody.Append("" + txtemailid.Text & "")
            mailBody.Append("</td>")
            mailBody.Append("</tr>")
            mailBody.Append("<tr style='background-color: #f4d687'>")
            mailBody.Append("<td>")
            mailBody.Append("Message ")
            mailBody.Append("</td>")
            mailBody.Append("<td>")
            mailBody.Append("" + txtmessage.Text & "")
            mailBody.Append("</td>")
            mailBody.Append("</tr>")
            mailBody.AppendLine("</table>")
            mailBody.Append("</td>")
            mailBody.Append("</tr>")
            mailBody.Append("<tr><td>Thanks for contacting us we will get in touch with you shortly.
</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><b>Regards,</b></td></tr>")
            mailBody.Append("<tr><td style='background-color: #0696AF;color:#ffffff'><b>Article Mirror</b></td></tr>")
            mailBody.AppendLine("</table>")
            Dim sr As New StringReader(mailBody.ToString())
            Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
            Dim htmlparser As New HTMLWorker(pdfDoc)
            Dim memoryStream As New MemoryStream()
            Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, memoryStream)
            pdfDoc.Open()
            htmlparser.Parse(sr)
            pdfDoc.Close()
            Dim bytes As Byte() = memoryStream.ToArray()
            memoryStream.Close()
            Dim Email As String = ConfigurationManager.AppSettings("Email").ToString()
            Dim Emailto As String = txtemail.Text
            Dim mail As New MailMessage(Email, Emailto)
            mail.Subject = "Contact us page Enquiry"
            mail.Attachments.Add(New Attachment(New MemoryStream(bytes), "conatctPDF.pdf"))
            mail.IsBodyHtml = True
            Dim smtp As New SmtpClient()
            smtp.Send(mail)
            lblmessage.InnerText = "Email send successfully"
            Clear()
        Catch ex As Exception
        End Try
    End Sub
    Public Sub Clear()
        txtemail.Text = ""
        txtname.Text = String.Empty
        txtmessage.Text = String.Empty
        txtemailid.Text = String.Empty
        txtsubject.Text = String.Empty
    End Sub

Build the project/website, run and check out the result.

Output :
Send email content as attachment in PDF format in asp.net
Click here to enlarge

Is this article helpful for you?
If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment