Category / Section
How to create PDF dynamically and email as attachment ?
2 mins read
The PDF can be created dynamically and send email with an attachment, please find the code example and sample below for the same.
// Create a new instance of PdfDocument class. PdfDocument document = new PdfDocument(); // Add a page to the document. PdfPage page = document.Pages.Add(); // Create PDF graphics for the page. PdfGraphics g = page.Graphics; // Create a solid brush PdfBrush brush = new PdfSolidBrush(Color.Black); // Set the font. PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20f); // Draw the text. g.DrawString("Hello world!", font, brush, new PointF(20, 20)); MemoryStream ms = new MemoryStream(); // Save and close the document. document.Save(ms); document.Close(true); //Reset the memory stream position. ms.Position = 0; //Attach the file Attachment file = new Attachment(ms ,"Attachment.pdf", "application/pdf"); using (SmtpClient smtp = new SmtpClient("xxxx.co.in")) { MailMessage message = new MailMessage(); // end-user customization message.From = new MailAddress("yyyy@gmail.com"); message.To.Add("zzzz@gmail.com"); message.Subject = "message"; message.Attachments.Add(file); message.IsBodyHtml = false; smtp.Send(message); }
Sample link:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/PdfSample-1394236472