)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to create PDF dynamically and email as attachment ?

Platform: WinForms |
Control: PDF |
Published Date: January 22, 2016 |
Last Revised Date: January 24, 2020
Tags: pdf, attachment, email

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:

http://www.syncfusion.com/downloads/support/directtrac/general/ze/PdfSample-1394236472

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon