MSG 파일 만들기 및 저장
Aspose.Email은 Outlook 메시지(MSG) 파일 생성을 지원합니다. 이 문서에서는 다음 방법을 설명합니다:
- MSG 메시지를 생성합니다.
- 첨부 파일이 있는 MSG 메시지를 생성합니다.
- RTF 본문을 가진 MSG 메시지를 생성합니다.
- 메시지를 초안으로 저장합니다.
- 본문 압축 작업.
Outlook 메시지 만들기 및 저장
다음은 MailMessage 클래스에는 저장 디스크 또는 스트림에 Outlook MSG 파일을 저장할 수 있는 메서드. 아래 코드 조각은 다음 클래스의 인스턴스를 생성합니다. MailMessage 클래스이며, from, to, subject 및 body와 같은 속성을 설정합니다. 해당 저장 메서드는 파일 이름을 인수로 받습니다. 또한 Outlook 메시지는 다음과 함께 생성될 수 있습니다. 압축된 RTF 본문 다음 사용 MapiConversionOptions.
- 새 인스턴스를 생성합니다 MailMessage 클래스이며 From, To, Subject 및 Body 속성을 설정합니다.
- 다음을 호출합니다. MapiMessage 클래스 fromMailMessage 객체를 받아들이는 메서드 MailMessage 형식. 해당 fromMailMessage 메서드는 변환합니다 MailMessage 으로 MapiMessage (MSG).
- 다음을 호출합니다. MapiMessage.save MSG 파일을 저장하는 메서드.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";
// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();
// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setBody("This is test body");
// Create an instance of the MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.fromMailMessage(mailMsg);
// Save the message (MSG) file
String strMsgFile = "CreatingAndSavingOutlookMessages_out.msg";
outlookMsg.save(dataDir + strMsgFile);
첨부 파일이 포함된 MSG 파일 만들기
위 예제에서, 우리는 간단한 MSG 파일을 만들었습니다. Aspose.Email은 첨부 파일이 포함된 메시지 파일 저장도 지원합니다. 필요한 것은 첨부 파일을 다음에 추가하는 것입니다. MailMessage 인스턴스. addItem 메서드를 호출하여 첨부 파일을 추가합니다. MailMessage.Attachments 컬렉션.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";
String[] files = new String[2];
files[0] = "attachment.doc";
files[1] = "attachment.png";
// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();
// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setBody("This is test body");
// Add the attachments
for (String strFileName : files)
{
mailMsg.getAttachments().addItem(new Attachment(strFileName));
}
// Create an instance of MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.fromMailMessage(mailMsg);
String strMsgFile = "CreateMessagesWithAttachments.msg";
outlookMsg.save(dataDir + strMsgFile);
RTF 본문을 포함한 MSG 파일 만들기
Aspose.Email을 사용하면 서식이 풍부한 텍스트(RTF) 본문을 가진 Outlook 메시지(MSG) 파일도 만들 수 있습니다. RTF 본문은 텍스트 서식을 지원합니다. 이를 만들려면 다음을 설정하십시오. MailMessage.HtmlBody 속성. 변환할 때 MailMessage 인스턴스를 MapiMessage 인스턴스에서는 HTML 본문이 RTF로 변환됩니다. 이렇게 하면 이메일 본문의 서식이 보존됩니다.
다음 예제는 RTF 본문을 가진 MSG 파일을 생성합니다. HTML 본문에 하나의 헤딩과 굵게, 밑줄 서식이 적용되어 있습니다. 이 서식은 HTML이 RTF로 변환될 때 유지됩니다.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";
// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();
// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setHtmlBody("<h3>rtf example</h3><p>creating an <b><u>outlook message (msg)</u></b> file using Aspose.Email.</p>");
MapiMessage outlookMsg = MapiMessage.fromMailMessage(mailMsg);
outlookMsg.save(dataDir + "CreatingMSGFilesWithRTFBody_out.msg");
초안 상태로 메시지 저장
이메일은 작성 중이지만 나중에 계속 작업하려는 경우 초안으로 저장됩니다. Aspose.Email은 메시지 플래그를 설정하여 이메일 메시지를 초안 상태로 저장하는 것을 지원합니다. 아래는 Outlook 이메일 메시지(MSG)를 초안으로 저장하는 샘플 코드입니다.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";
// Change properties of an existing MSG file
String strExistingMsg = "message.msg";
// Load the existing file in MailMessage and Change the properties
MailMessage msg = MailMessage.load(dataDir + strExistingMsg, new MsgLoadOptions());
msg.setSubject(msg.getSubject() + " NEW SUBJECT (updated by Aspose.Email)");
msg.setHtmlBody(msg.getHtmlBody() + " NEW BODY (udpated by Aspose.Email)");
// Create an instance of type MapiMessage from MailMessage, Set message flag to un-sent (draft status) and Save it
MapiMessage mapiMsg = MapiMessage.fromMailMessage(msg);
mapiMsg.setMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT);
mapiMsg.save(dataDir + "SavingMessageInDraftStatus_out.msg");
본문 압축의 영향
RTF 본문 압축 방법을 사용하면 더 작은 크기의 MSG를 생성할 수 있습니다. 하지만 이 경우 생성 속도가 느려집니다. 속도를 개선하려면 플래그를 false 로 설정하십시오. 이 플래그는 생성된 PST에도 영향을 미칩니다: 작은 MSG 파일은 작은 PST를 만들고, 큰 MSG 파일은 PST 생성 속도를 늦춥니다.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String fileName = "outlook/test.msg";
MailMessage message = MailMessage.load(fileName);
MapiConversionOptions options = new MapiConversionOptions();
options.setUseBodyCompression(true);
MapiMessage ae_mapi = MapiMessage.fromMailMessage(message, options);