MSG dosyasını Yükleme, Görüntüleme ve Ayrıştırma

Bu konu, bir Microsoft Outlook Mesaj dosyasını (*.msg) nasıl yükleyeceğinizi açıklar. ___ MapiMessage sınıf, MSG dosyalarını yüklemek için kullanılır ve farklı senaryolar için çeşitli statik yükleme işlevleri sunar. Aşağıdaki kod parçacığı, MSG dosyalarını bir dosyadan ya da bir akıştan nasıl yükleyeceğinizi gösterir.

MSG Dosyalarını Yükleme

Aşağıdaki kod parçacığı, MSG dosyalarını nasıl yükleyeceğinizi gösterir.

// 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 MapiMessage from file
MapiMessage msg = MapiMessage.load(dataDir + "message.msg");

// Get subject
System.out.println("Subject:" + msg.getSubject());

// Get from address
System.out.println("From:" + msg.getSenderEmailAddress());

// Get body
System.out.println("Body" + msg.getBody());

// Get recipients information
System.out.println("Recipient: " + msg.getRecipients());

// Get attachments
for (MapiAttachment att : msg.getAttachments())
{
    System.out.println("Attachment Name: " + att.getFileName());
    System.out.println("Attachment Display Name: " + att.getDisplayName());
}

Aşağıdaki kod örneği, MailMessage kullanarak MSG formatındaki bir mesajı nasıl yükleyeceğinizi gösterir.

MailMessage eml = MailMessage.load("message.msg");

Sonuçta oluşan mesajın, gömülü mesaj eklerini de içerecek şekilde EML formatına dönüştürüldüğüne dikkat edilmelidir. Orijinal mesajın bazı belirli msg format özelliklerini korumak istiyorsanız bu yükleme yöntemini kullanmayın.

Gömülü mesaj eklerinin orijinal formatını korumak için şunu kullanın: MsgLoadOptions.PreserveEmbeddedMessageFormat özellik.

MsgLoadOptions msgLoadOptions = new MsgLoadOptions();
msgLoadOptions.setPreserveEmbeddedMessageFormat(true);
MailMessage msg = MailMessage.load(stream, msgLoadOptions);

Akıştan Yükleme

Aşağıdaki kod parçacığı, bir dosyayı akıştan nasıl yükleyeceğinizi gösterir.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create an instance of MapiMessage from file
try (FileInputStream stream = new FileInputStream(dataDir + "message.msg"))
{
    // Create an instance of MapiMessage from file
    MapiMessage msg = MapiMessage.load(stream);

    // Get subject
    System.out.println("Subject:" + msg.getSubject());

    // Get from address
    System.out.println("From:" + msg.getSenderEmailAddress());

    // Get body
    System.out.println("Body" + msg.getBody());

}

MSG Dosyalarını Ek İçeriği Okumadan Yükleme

Varsayılan olarak, MapiMessage.load her ekin içeriğini mesajla birlikte belleğe okur. Büyük ekli mesajlar için, yalnızca başlıkları, alıcıları veya ek adlarını ihtiyacınız olsa dahi, mesajı açmak için gereken bellek eklerinin boyutu kadar artar.

Gecikmeli yükleme ile ek içeriği kaynakta kalır ve yalnızca ona eriştiğinizde okunur. Aşağıdaki özelliklerle etkinleştirin: MsgLoadOptions sınıf:

  • setDeferAttachmentContent(boolean) - ne zaman true, ek içeriği mesajla birlikte yüklenmek yerine talep üzerine kaynaktan okunur. Varsayılan false.
  • setDeferralThreshold(int) - gecikmeli yüklemenin uygulanacağı minimum içerik boyutu, bayt cinsinden. Daha küçük ekler her zaman mesajla birlikte yüklenir. Varsayılan 4096’dır.

Ertelenmiş yükleme ikili ek içeriğine uygulanır. OLE nesneleri veya gömülü mesajlar olarak saklanan ekler, mesaj yüklendiğinde çıkarılır.

Aşağıdaki kod parçacığı, bir mesajı eklerini okumadan inceler:

MsgLoadOptions options = new MsgLoadOptions();
options.setDeferAttachmentContent(true);

// The file stays open until the message is disposed
try (MapiMessage msg = MapiMessage.load("large.msg", options)) {
    System.out.println("Subject: " + msg.getSubject());
    System.out.println("Recipients: " + msg.getRecipients().size());

    for (MapiAttachment attachment : msg.getAttachments()) {
        // Names and sizes are available without reading the content
        System.out.println("  " + attachment.getLongFileName() + " : " + attachment.getContentLength() + " bytes");
    }
}

Aşağıdaki kod parçacığı, bir mesajın yalnızca PDF eklerini çıkarır. Diğer eklerin içeriği dosyadan hiç okunmaz:

MsgLoadOptions options = new MsgLoadOptions();
options.setDeferAttachmentContent(true);

try (MapiMessage msg = MapiMessage.load("large.msg", options)) {
    for (MapiAttachment attachment : msg.getAttachments()) {
        if (!attachment.getLongFileName().endsWith(".pdf")) {
            continue;
        }

        try (InputStream source = attachment.openRead()) {
            Files.copy(source, Paths.get("out", attachment.getLongFileName()), StandardCopyOption.REPLACE_EXISTING);
        }
    }
}

Aşağıdaki kod parçacığı, çağıran tarafından sahip olunmuş bir akıştan bir mesaj yükler:

MsgLoadOptions options = new MsgLoadOptions();
options.setDeferAttachmentContent(true);

// Keep the stream open while the message is in use
try (InputStream source = Files.newInputStream(Paths.get("large.msg"))) {
    MapiMessage msg = MapiMessage.load(source, options);
    byte[] data = msg.getAttachments().get(0).getBinaryData();
    System.out.println("Read " + data.length + " bytes");
}

Ek içeriğini bir akış olarak okumak ve büyük ekleri belleğe yüklemeden eklemek için bkz. Mesaj Ekleriyle Çalışma.

Gömülü EML Formatını Korumakla EML’yi MSG’ye Dönüştürme

EML dosyaları ___ içine yüklenebilir MapiMessage sınıfı, bir ___ örnekleyerek MailMessage nesnesini oluşturup ___‘a geçirerek MapiMessage.fromMailMessage metodu. EML dosyası gömülü EML dosyaları içeriyorsa, şunu kullanın: MapiConversionOptions.setPreserveEmbeddedMessageFormat gömülü EML dosyalarının formatını korumak için. Aşağıdaki kod parçacığı, EML dosyalarını ___ içine yüklemeyi gösterir. MapiMessage gömülü EML dosyalarının formatını korurken.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "email/";

MailMessage eml = MailMessage.load(dataDir + "sample.eml", new EmlLoadOptions());

MapiConversionOptions options = MapiConversionOptions.getUnicodeFormat();

//Preserve Embedded Message Format
options.setPreserveEmbeddedMessageFormat(true);

//Convert EML to MSG with Options
MapiMessage msg = MapiMessage.fromMailMessage(eml, options);