Добавяне на прикачени файлове и HTML текст към Outlook MSG файл
Нашите съвети за миграция показват как продуктите на Aspose могат да се използват за подобряване на вашите приложения и да ви освободят от зависимостта от традиционната автоматизация.
Този съвет за миграция показва как да създадете MSG файл с HTML форматирано тяло и да добавите множество прикачени файлове към него:
- Раздел от VBA код, който използва Автоматизация на Microsoft Office за създаване на MSG файл с прикачени файлове и HTML тяло.
- Същото е постигнато чрез използване на Aspose.Email за Java.
Office Automation
При използване на този метод, Microsoft Outlook трябва да бъде инсталиран на машината, където се изпълнява VBA кодът. Следният кодов фрагмент създава Outlook MSG файл с прикачени файлове и HTML тяло.
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 за Java
Следният кодов фрагмент използва библиотеката Aspose.Email за Java за създаване на MSG файл, подобно на този, създаден по-горе, с множество прикачени файлове и HTML тяло. Тъй като Aspose.Email за Java е написан изцяло на Java, COM interop не е необходим. Също така Microsoft Outlook 2003/2007 не е нужно да бъде инсталиран на машината. Методът, описан по-долу, е подходящ, когато Microsoft Outlook не е инсталиран или когато желаете да генерирате MSG файлове на сървър.
Следните кодови фрагменти показват как да изпълните същата задача в Java с използване на Aspose.Email за Java:
// 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());