Articles in this section
Category / Section

How to create PDF dynamically and email as attachment ?

1 min 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.
//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(""))
MailMessage message = new MailMessage();
// end-user customization
message.From = new MailAddress("");
message.Subject = "message";
message.IsBodyHtml = false;

Sample link:

Did you find this information helpful?
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment