Printing Email Messages to XPS and TIFF
The Aspose.Email.Printing namespace provides a rich set of features for printing mail messages to different formats (XPS or TIFF) and for configuring page layouts. This article describes them. There are several options for how an email message can be printed with Aspose.Email:
- Printing the message body only.
- Printing the message body and headers.
- Printing an HTML body.
- Setting the page layout.
- Auto-fitting a TIFF to the printer.
- Adjusting the target DPI for the output TIFF.
The central class for these operations is MailPrinter. Its Print method accepts a MailMessage, an output file name, and a PrintFormat value (XPS or Tiff).
Printing the Message Body
The following code snippet creates a message and prints it without headers, first to an XPS file and then to a TIFF file. Set the FormattingFlags property to MessageFormattingFlags.None to display only the message body.
// Declare message as a 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 FormattingFlags 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
To display and print the message headers along with the message body, change the FormattingFlags to MessageFormattingFlags.MailInfo.
printer.FormattingFlags = Aspose.Email.Printing.MessageFormattingFlags.MailInfo;
Printing a Message with an HTML Body
Messages with an HTML body can also be printed. Set the HTML content using the HtmlBody property before printing.
// 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 the Page Layout for Printing
MailPrinter provides controls for setting the following page layout properties:
| 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 |
- [1] There are two flags:
MailInfoandNone. - [2] Page units can be one of
Inch,Pixel,Point,Cm, orMillimeter.
The code snippet below 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.
// 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 flag, which allows you to auto-fit the TIFF to the printer. The following code snippet shows how to use auto-fit.
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 the Target DPI for the Output TIFF
Use the DpiX and DpiY properties to control the resolution of the output TIFF.
// 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);