MSG Dosyaları Oluşturma ve Kaydetme

Aspose.Email, Outlook mesaj (MSG) dosyaları oluşturmayı destekler. Bu makale şunların nasıl yapılacağını açıklar:

  • MSG mesajları oluşturun.
  • Ekli MSG mesajları oluşturun.
  • RTF gövdesiyle bir MSG mesajı oluşturun.
  • Bir mesajı taslak olarak kaydedin.
  • Gövde sıkıştırması ile çalışın.

Outlook Mesajları Oluşturma ve Kaydetme

Bu MailMessage sınıf, … içerir kaydet Outlook MSG dosyalarını diske veya akışa kaydedebilen yöntem. Aşağıdaki kod parçacıkları … örneği oluşturur. MailMessage sınıf, from, to, subject ve body gibi özellikleri ayarlar. kaydet yöntem dosya adını argüman olarak alır. Ayrıca, Outlook Mesajları bir … ile oluşturulabilir. sıkıştırılmış RTF gövdesi kullanarak MapiConversionOptions.

  1. Yeni bir … örneği oluşturun MailMessage sınıfını ve From, To, Subject ve Body özelliklerini ayarlar.
  2. Şuğu çağırın MapiMessage sınıf fromMailMessage yöntem, … nesnesini kabul eder MailMessage tür. fromMailMessage yöntem … dönüştürür MailMessage bir MapiMessage (MSG).
  3. Şuğu çağırın MapiMessage.save MSG dosyasını kaydetmek için yöntem.
// 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);

Ekli MSG Dosyaları Oluşturma

Yukarıdaki örnekte, basit bir MSG dosyası oluşturduk. Aspose.Email ayrıca ekli mesaj dosyalarını kaydetmeyi destekler. Tek yapmanız gereken ekleri … eklemektir. MailMessage örnek. addItem metodunu çağırarak ekleri ekleyin MailMessage.Attachments koleksiyon.

// 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 Gövdesiyle MSG Dosyaları Oluşturma

Aspose.Email ile zengin metin (RTF) gövdeli Outlook Mesajı (MSG) dosyaları da oluşturabilirsiniz. RTF gövdesi metin biçimlendirmesini destekler. Bunu, … ayarlayarak oluşturun. MailMessage.HtmlBody özellik. Bir … dönüştürdüğünüzde MailMessage örnek bir MapiMessage örneğinde, HTML gövdesi RTF’ye dönüştürülür. Bu şekilde, e-posta gövdesinin biçimlendirmesi korunur.

Aşağıdaki örnek, RTF gövdesine sahip bir MSG dosyası oluşturur. HTML gövdesinde bir başlık, kalın ve altı çizili biçimlendirme uygulanmıştır. Bu biçimlendirme, HTML RTF’ye dönüştürüldüğünde korunur.

// 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");

Taslak Durumunda Mesaj Kaydetme

E-postalar, birisi düzenlemeye başladıktan sonra daha sonra tamamlamak üzere geri dönmek istediğinde taslak olarak kaydedilir. Aspose.Email, bir mesaj bayrağı ayarlanarak e-posta mesajlarını taslak durumunda kaydetmeyi destekler. Aşağıda bir Outlook e-posta mesajını (MSG) taslak olarak kaydetmek için örnek kod yer almaktadır.

// 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");

Gövde Sıkıştırmasının Sonuçları

RTF gövde sıkıştırma yöntemi daha küçük boyutlu bir MSG oluşturmak için kullanılabilir. Ancak bu, oluşturma hızını yavaşlatır. Hızı artırmak için bayrağı false olarak ayarlayın. Bu bayrak, oluşturulan PST’leri de etkiler: daha küçük MSG dosyaları daha küçük PST, büyük MSG dosyaları ise PST oluşturmayı yavaşlatır.

// 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);