Mail Send With Pdf

protected void MailSendWithCoponPdf(string Email,string Coupon)
    {
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                string companyName = "xxxxxxxx";
                string orderNo = "#2303";
                StringBuilder sb = new StringBuilder();
                sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>");
                sb.Append("<tr><td align='center' style='background-color: #18B5F0' colspan = '2'><b>Coupon</b></td></tr>");
                sb.Append("<tr><td align='center' style='background-color: #18B5F0' colspan = '2'><b>----------------------------------------------------------------</b></td></tr>");
                sb.Append("<tr><td colspan = '4'><center><img src='http://www.xxxxxx.com/images/logo.png' width='100px' height='100px'/></center></td></tr>");
                sb.Append("<tr><td colspan = '4'><b>Coupon No : </b>");
                sb.Append(orderNo);
                sb.Append("</td></tr><tr><td colspan = '4'><b>Date: </b>");
                sb.Append(DateTime.Now);
                sb.Append(" </td></tr>");
                sb.Append("<tr><td colspan = '4'><b>Office Address:</b> </td></tr><td colspan = '4'>");
                sb.Append(companyName);
                sb.Append("</td></tr>");
                sb.Append("<tr><td align='center' style='background-color: #18B5F0' colspan = '2'><b>----------------------------------------------------------------</b></td></tr>");
                sb.Append("<tr><td align='center' style='background-color: #18B5F0' colspan = '2'>Please come out at our office with this Coupon</td></tr>");
                sb.Append("</table>");




                StringReader sr = new StringReader(sb.ToString());

                Document pdfDoc = new Document(PageSize.POSTCARD, 10f, 10f, 10f, 0f);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
                    pdfDoc.Open();
                    htmlparser.Parse(sr);
                    pdfDoc.Close();
                    byte[] bytes = memoryStream.ToArray();
                    memoryStream.Close();



                    ////Response.Write(Doc);
                    ////Response.End();


                    Attachment at = new Attachment(new MemoryStream(bytes), "Coupon.pdf");
                    MailMessage Msg = new MailMessage();
                    string FromMail = "xxxx@xxxxxxxxx.com";
                    string Password = "xxxxx";  // "XXXXXX" is sender password…
                    Msg.From = new MailAddress(FromMail);
                    // Recipient e-mail address..
                    string ToMail = Email;   //Receiver email id…
                    Msg.To.Add(ToMail);
                    string MsgBody = "Thank you for Registration your Laptop.<br><br>Coupon No : #" + Coupon + " <br><br> Please come out at our office with Coupon no.";
                    AlternateView htmlMsgView = AlternateView.CreateAlternateViewFromString(MsgBody, null, MediaTypeNames.Text.Html);
                    Msg.AlternateViews.Add(htmlMsgView);
                    Msg.Subject = "Subi Technologies :: Coupon";
                    Msg.Body = MsgBody;

                    //send the message
                    SmtpClient smtp = new SmtpClient("mail.xxxxxxxx.com");


                    //to authenticate we set the username and password properites on the SmtpClient
                    smtp.Credentials = new NetworkCredential(FromMail, Password);
                    Msg.Attachments.Add(at);
                    smtp.Send(Msg);
                }
            }
        }
    }

Popular posts from this blog

How to protect your Drive/Flash USB Drive (Pen Drive) with Password without using any software