Drukowanie wiadomości e‑mail do XPS i TIFF
Ten Aspose.Email.Printing przestrzeń nazw udostępnia bogaty zestaw funkcji do drukowania wiadomości e‑mail w różnych formatach (XPS lub TIFF) oraz do konfigurowania układów stron. Ten artykuł je opisuje. Istnieje kilka opcji, w jaki sposób wiadomość e‑mail może być drukowana przy użyciu Aspose.Email:
- Drukowanie wyłącznie treści wiadomości.
- Drukowanie treści wiadomości i nagłówków.
- Drukowanie treści HTML.
- Ustawianie układu strony.
- Automatyczne dopasowanie TIFF do drukarki.
- Dostosowywanie docelowego DPI dla wyjściowego pliku TIFF.
Centralną klasą dla tych operacji jest MailPrinter. Jej Print metoda przyjmuje MailMessage, nazwę pliku wyjściowego oraz PrintFormat wartość (XPS lub Tiff).
Drukowanie treści wiadomości
Poniższy fragment kodu tworzy wiadomość i drukuje ją bez nagłówków, najpierw do pliku XPS, a potem do pliku TIFF. Ustaw FormattingFlags właściwość do MessageFormattingFlags.None na wyświetlanie tylko ciała wiadomości.
// 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);
Drukowanie nagłówków i treści wiadomości
Aby wyświetlić i wydrukować nagłówki wiadomości wraz z jej ciałem, zmień FormattingFlags do MessageFormattingFlags.MailInfo.
printer.FormattingFlags = Aspose.Email.Printing.MessageFormattingFlags.MailInfo;
Drukowanie wiadomości z ciałem HTML
Wiadomości z ciałem HTML również mogą być drukowane. Ustaw zawartość HTML przy użyciu HtmlBody właściwość przed drukowaniem.
// 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>";
Ustawianie układu strony do drukowania
MailPrinter udostępnia kontrolki do ustawiania następujących właściwości układu strony:
|Właściwość|Opis|Wartość domyślna| | :- | :- | :- | |FormattingFlags|Pokaż lub ukryj szczegóły wiadomości.|None [1]| |MarginTop|Uzyskaj lub ustaw górny margines.|0.5| |MarginLeft|Uzyskaj lub ustaw lewy margines.|0.5| |MarginBottom|Uzyskaj lub ustaw dolny margines.|0.5| |MarginRight|Uzyskaj lub ustaw prawy margines.|0.5| |PageUnit|Uzyskaj lub ustaw jednostki pomiaru.|Inch [2]| |PageHeight|Uzyskaj lub ustaw wysokość strony.|11.69| |PageWidth|Uzyskaj lub ustaw szerokość strony.|8.27|
- [1] Istnieją dwie flagi:
MailInfoiNone. - [2] Jednostki strony mogą być jedną z
Inch,Pixel,Point,Cm, lubMillimeter.
Fragment kodu poniżej używa dowolnych ustawień, aby zilustrować użycie tych właściwości. Ustawia stronę o wysokości 20 cm i szerokości 8 cm, z marginesami 2 cm.
// 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;
Automatyczne dopasowanie TIFF
Aspose.Email.Printing udostępnia MessageFormattingFlags.AutoFitWidth flaga, która pozwala automatycznie dopasować TIFF do drukarki. Poniższy fragment kodu pokazuje, jak używać 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);
Dostosuj docelową DPI dla wyjściowego pliku TIFF
Użyj DpiX i DpiY właściwości sterujące rozdzielczością wyjściowego pliku 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);