Aspose.Email.Printing Features

Printing Features

The Aspose.Email.Printing namespace provides a rich set of features for printing mail messages to different formats (XPS or TIFF) and configuring page layouts. This article describes them. There are several options for how an email message can be printed from Aspose.Email:

  1. Printing the message body only.
  2. Printing the message body and headers.
  3. Printing an HTML body.
  4. Setting the page layout.
  5. Auto-fit a TIFF to the printer.
  6. Adjust Target DPI for output TIFF.

Printing the Message Body

The following code snippet shows you how to creates a message and prints it without headers first to an XPS file and then to a TIFF file.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Declare message as an MailMessage instance
MailMessage message = new MailMessage();
message.From = "user1@domain.com";
message.To = "user2@domain.com";
message.Subject = "My First Mail";
message.Date = DateTime.Now;
message.Body = "Text is the Mail Message";
// Instantiate an instance of MailPrinter and Set the MessageFormattingFlags to none to display only the message body
var printer = new Aspose.Email.Printing.MailPrinter();
printer.FormattingFlags = Aspose.Email.Printing.MessageFormattingFlags.None;
// Print the email to an XPS and TIFF file
printer.Print(message, dataDir + "PrintXPS_out.xps", Aspose.Email.Printing.PrintFormat.XPS);
printer.Print(message, dataDir + "PrintTIFF_out.tiff", Aspose.Email.Printing.PrintFormat.Tiff);

Printing Message Headers and Body

The following code snippet shows you how to display the headers and print them as well as the message body, change the FormattingFlags to MailInfo.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
printer.FormattingFlags = Aspose.Email.Printing.MessageFormattingFlags.MailInfo;

Printing Message with HTML Body

Messages with an HTML body can also be printed. The following code snippet shows you how to print a message with HTML Body.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Body of the email message
message.IsBodyHtml = true;
message.HtmlBody = "<html><body><h1>Hello this is html body in Heading 1 format </h1></body></html>";

Setting Page Layout for Printing

Aspose.Email.Printing.MailPrinter provides controls for setting the following properties of the page layout:

Property Description Default Value
FormattingFlags Show or hide message details. None [1]
MarginTop Get or set the top margin. 0.5
MarginLeft Get or set the left margin. 0.5
MarginBottom Get or set the bottom margin. 0.5
MarginRight Get or set the right margin. 0.5
PageUnit Get or set measurement units. Inch [2]
PageHeight Get or set the page height. 11.69
PageWidth Get or set the page width. 8.27
  • There are two flags: MailInfo and None
  • Page units can be one of Inch, Pixel, Point, Cm, or Millimeter.

The code snippet that follows uses arbitrary settings to illustrate how these properties are used. It sets up a page 20 cm high and 8 cm wide, with 2 cm margins.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Instantiate an instance of MailPrinter
var printer = new Aspose.Email.Printing.MailPrinter();
// Just for testing, get the default property values
double width = printer.PageWidth;
double height = printer.PageHeight;
// Set page layout for printing
printer.PageUnit = Aspose.Email.Printing.PrinterUnit.Cm;
printer.MarginTop = 2;
printer.MarginBottom = 2;
printer.MarginLeft = 2;
printer.MarginRight = 2;
printer.PageWidth = 8;
printer.PageHeight = 20;

Auto-fit a TIFF

Aspose.Email.Printing provides the MessageFormattingFlags.AutoFitWidth property which allows you to auto-fit the TIFF to the printer. The following code snippet shows you how to use Auto-fit.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
string dataDir = RunExamples.GetDataDir_KnowledgeBase();
// Instantiate an instance of MailPrinter
Printing.MailPrinter printer = new Printing.MailPrinter();
printer.FormattingFlags = Printing.MessageFormattingFlags.AutoFitWidth;
MailMessage msg = MailMessage.Load(dataDir + "message3.msg", new MsgLoadOptions());
printer.Print(msg, dataDir + "AutoFit_out.tiff", Printing.PrintFormat.Tiff);

Adjust Target DPI for Output TIFF

The following code snippet shows you how to use DPI for Output TIFF.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// The path to the File directory and Instantiate an instance of MailPrinter
string dataDir = RunExamples.GetDataDir_KnowledgeBase();
Printing.MailPrinter printer = new Printing.MailPrinter();
printer.DpiX = 300;
printer.DpiY = 300;
MailMessage msg = MailMessage.Load(dataDir + "message3.msg", new MsgLoadOptions());
printer.Print(msg, dataDir + "AdjustTargetDPI_out.tiff", Printing.PrintFormat.Tiff);