Hinzufügen von Anhängen und HTML-Text zu einer Outlook‑MSG‑Datei
Unsere Migrationstipps zeigen, wie Aspose‑Produkte verwendet werden können, um Ihre Anwendungen zu verbessern und Sie von der Abhängigkeit von herkömmlicher Automatisierung zu befreien.
Dieser Migrationstipp zeigt, wie man eine MSG‑Datei mit HTML‑formatiertem Text erstellt und mehrere Anhänge hinzufügt:
- Ein Abschnitt VBA‑Code, der verwendet Microsoft Office Automatisierung zum Erstellen der MSG‑Datei mit Anhängen und HTML‑Inhalt.
- Dasselbe wird erreicht mit Aspose.Email für Java.
Office Automation
Bei dieser Methode muss Microsoft Outlook auf dem Rechner installiert sein, auf dem der VBA‑Code ausgeführt wird. Das untenstehende Code‑Snippet erstellt eine Outlook‑MSG‑Datei mit Anhängen und HTML‑Inhalt.
VBA
' Create an object of type Outlook.Application
Set objOutlookApplication = CreateObject("Outlook.Application")
' Create an object of type olMailItem
Set objMsg = objOutlookApplication.CreateItem(olMailItem)
' Set properties of the message file e.g. subject, body and to address
' Set subject
objMsg.Subject = "This MSG file is created using Office Automation."
' Set to (recipient) address
objMsg.To = "to@domain.com"
' Set body of the email message
objMsg.HTMLBody = "<html><p>This MSG file is created using VBA code.</p>"
' Add attachments to the message
objMsg.Attachments.Add "C:\test.bmp"
objMsg.Attachments.Add "C:\test2.jpg"
' Save as Outlook MSG file
objMsg.SaveAs ("c:\testvba.msg")
' Open the MSG file
objMsg.Display
Aspose.Email für Java
Das untenstehende Code‑Snippet verwendet die Aspose.Email‑für‑Java‑Bibliothek, um eine MSG‑Datei zu erstellen, ähnlich wie die oben erstellte, mit mehreren Anhängen und HTML‑Inhalt. Da Aspose.Email für Java vollständig in Java geschrieben ist, ist COM‑Interop nicht erforderlich. Außerdem muss Microsoft Outlook 2003/2007 nicht auf dem Rechner installiert sein. Die unten beschriebene Methode eignet sich, wenn Microsoft Outlook nicht installiert ist oder wenn MSG‑Dateien auf einem Server erzeugt werden sollen.
Die untenstehenden Code‑Snippets zeigen, wie dieselbe Aufgabe in Java mit Aspose.Email für Java durchgeführt wird:
// Create an instance of type MailMessage
MailMessage msg = new MailMessage();
// Set properties of message like subject, to and HTML body
// Set subject
msg.setSubject("This MSG file is created using Aspose.Email for .NET");
// Set from (sender) address
msg.setSender(new MailAddress("from@domain.com", "From Name"));
// Set to (recipient) address and name
msg.getTo().addItem(new MailAddress("to@domain.com", "To Name"));
// Set HTML body of the email message
msg.setHtmlBody("<html><p>This MSG file is created using Java code.</p>"
+ "<p>Microsoft Outlook does not need to be installed on the machine running this code.</p>"
+ "<p>This method is suitable for creating MSG files on the server side.</html>");
// Add attachments to the message file
msg.getAttachments().addItem(new Attachment("C:\\test.bmp"));
msg.getAttachments().addItem(new Attachment("C:\\test2.jpg"));
// Save as Outlook MSG file
String strSaveFile = "C:\\TestAspose.msg";
msg.save(strSaveFile, SaveOptions.getDefaultMsgUnicode());