Outlook MSG 파일에 첨부 파일과 HTML 텍스트 추가

Office 자동화

이 방법을 사용하려면 VBA 코드가 실행되는 머신에 Microsoft Outlook이 설치되어 있어야 합니다. 아래 코드 스니펫은 첨부 파일과 HTML 본문이 포함된 Outlook MSG 파일을 생성합니다.

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 for Java

아래 코드 스니펫은 Aspose.Email for Java 라이브러리를 사용하여 위와 유사하게 MSG 파일을 생성합니다. 위에서 만든 것, 여러 첨부 파일 및 HTML 본문 포함. Aspose.Email for Java는 순수 Java로 작성되었기 때문에 COM 인터옵이 필요 없습니다. 또한 Microsoft Outlook 2003/2007을 머신에 설치할 필요도 없습니다. 아래에 설명된 방법은 Microsoft Outlook이 설치되지 않았거나 서버에서 MSG 파일을 생성하려는 경우에 적합합니다.

아래 코드 스니펫은 Aspose.Email for Java를 사용하여 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());