Outlook Mesajlarında Ekleri Yönetme
Bu Outlook Mesajı (MSG) Dosyaları Oluşturma ve Kaydetme makale, ekli MSG dosyaları dahil mesajların oluşturulması ve kaydedilmesi adımlarını açıkladı. Bu makale, Aspose.Email kullanarak Microsoft Outlook eklerinin yönetimine derinlemesine girer. Bir mesaj dosyasından ekleri erişme ve kaydetme konusunda, şunu kullanarak size rehberlik edecektir: attachments özellik MapiMessage sınıf. Bu özellik, şunun bir koleksiyonudur: MapiAttachmentCollection sınıf, eklerin verimli bir şekilde işlenmesini sağlar.
Satır İçi ve Normal Ekleri Kontrol Et
"Inline" ve "Regular" ekler, bir e-posta mesajına dahil edilme şekillerini ifade eder. Regular ekler, geleneksel şekilde eklenen dosyalardır. Genellikle e-posta istemcisinde bir listede gösterilir ve alıcı tarafından indirilebilir ve yerel bir depolamaya kaydedilebilir. Inline ekler, gömülü veya satır içi görseller olarak da bilinir, genellikle e-postanın gövdesine resim veya diğer medya eklemek için kullanılır. Ayrı bir listede gösterilmezler, bunun yerine e-postanın içeriğinde, örneğin gövdesinde doğrudan gösterilirler. Bu, mesajın içeriğinin bir parçası olan resim veya diğer medyaları eklemenizi sağlar. Aşağıdaki kod örneği, bir ekin satır içi mi yoksa normal mi olduğunu nasıl belirleyeceğinizi gösterir:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("message.msg")
for attachment in msg.attachments:
print(f"{attachment.display_name}:{attachment.is_inline}")
MAPI Mesajlarında Referans Eklerini Tanımlama
Aspose.Email API, şunları içerir: is_reference özellik MapiAttachment sınıf, geliştiricilerin bir mesajdaki referans eklerini tanımlamasını sağlar. Aşağıdaki kod örneğini deneyerek bir Python projesinde nasıl çalıştığını görebilirsiniz:
import aspose.email as ae
# Load or create a MapiMessage
msg = ae.mapi.MapiMessage.load("Message_With_Reference_Attachment.msg")
# Iterate through the attachments
for attachment in msg.attachments:
if attachment.is_reference:
# Process reference attachment
pass
MSG Dosyalarından Ekleri Kaydet
Bir MSG dosyasından ekleri kaydetmek için:
- İçinde döngü yap MapiAttachmentCollection koleksiyon ve bireysel ekleri alın.
- Ekleri kaydetmek için, şu yöntemi çağırın: MapiAttachment sınıf Save() metod.
Aşağıdaki kod parçacığı, ekleri yerel diske nasıl kaydedeceğinizi gösterir:
import aspose.email as ae
data_dir = "C://dataDir/"
file_name = "message.msg"
# Create an instance of MapiMessage from file
message = ae.mapi.MapiMessage.load(data_dir + file_name)
# Iterate through the attachments collection
for attachment in message.attachments:
# Save the individual attachment
attachment.save(data_dir + attachment.file_name)
TNEF Eklerini İşleme
TNEF Formatında Ekleri Yükleme ve Kaydetme
Aspose.Email, TNEF eklerini işlemek için yöntemler sağlar. Bu yöntemler, Outlook mesajlarında yaygın olarak kullanılan (winmail.dat) TNEF formatında ekleri kaydetmeye ve yüklemeye olanak tanır.
- static MapiAttachment MapiAttachment.load_from_tnef(string fileName) – Bir ek dosyasını TNEF dosyasından yükler.
- static MapiAttachment MapiAttachment.load_from_tnef(Stream stream) – Bir ek dosyasını TNEF akışından yükler.
- void MapiAttachment.save_to_tnef(string filename) – Bir ek dosyasını TNEF dosyasına kaydeder.
- void MapiAttachment.save_to_tnef(Stream stream) – Bir ek dosyasını TNEF akışına kaydeder.
Aşağıdaki kod örneği, TNEF formatında e-posta eklerini nasıl çıkarıp, kaydedip ve yeniden ekleyeceğinizi, bunları bellek akışları veya dosyalar aracılığıyla yöneterek gösterir:
import io
from aspose.email.mapi import MapiMessage, MapiAttachment
# Load the EML message (which contains a winmail.dat TNEF attachment)
msg = MapiMessage.load("message.eml")
# Save the first attachment as a winmail.dat file
msg.attachments[0].save_to_tnef("winmail.dat")
# Load the TNEF attachment from the file and add it back to the message
from_tnef_attachment = MapiAttachment.load_from_tnef("winmail.dat")
msg.attachments.add(from_tnef_attachment.long_file_name, from_tnef_attachment.binary_data)
# (Optional) If you also want to use a memory stream instead of the file
ms = io.BytesIO()
msg.attachments[0].save_to_tnef(ms)
# Reset the stream position to the beginning
ms.seek(0)
# Load the TNEF attachment directly from the memory stream and add it
from_tnef_attachment_from_stream = MapiAttachment.load_from_tnef(ms)
msg.attachments.add(from_tnef_attachment_from_stream.long_file_name, from_tnef_attachment_from_stream.binary_data)
İç İçe Mail Mesajı Eklerini Al
Gömülü OLE (Nesne Bağlantısı ve Gömülmesi) ekleri, ayrıca şu sınıfın ek koleksiyonunda da görünür: MapiMessage sınıf. Aşağıdaki kod örneği, bir mesaj dosyasını nasıl ayrıştırıp gömülü mesaj eklerini çıkaracağınızı ve ardından bunları ilerideki kullanım için diske kaydedeceğinizi gösterir. MapiMessage sınıf bir from_properties gömülü bir ekten yeni bir mesaj oluşturmak için statik yöntem. Aşağıdaki kod parçacığı, iç içe mail mesajı eklerini nasıl alacağınızı gösterir:
import aspose.email as ae
eml = ae.mapi.MapiMessage.load("my.msg")
# Create a MapiMessage object from the individual attachment
get_attachment = ae.mapi.MapiMessage.from_properties(eml.attachments[0].object_data.properties)
# Create an object of type MailMessageInterpreter from the above message and save the embedded message to a file on disk
mail_message = get_attachment.to_mail_message(ae.mapi.MailConversionOptions())
mail_message.save("NestedMailMessageAttachments_out.eml", ae.SaveOptions.default_eml)
Ekleri Kaldır
Aspose.Email kütüphanesi, Microsoft Outlook Message (.msg) dosyalarından ekleri kaldırma işlevini sağlar. Şunu kullanın: remove_attachments(path) bu yöntem, mesaj dosyasının yolunu parametre olarak gerektirir. Genel bir statik yöntem olduğundan, bir nesne oluşturmaya gerek kalmadan doğrudan çağırabilirsiniz; bu da süreci basit ve verimli kılar.
Aşağıdaki kod parçacığı, Aspose.Email kullanarak MSG dosyalarından ekleri nasıl kaldıracağınızı gösterir:
import aspose.email as ae
ae.mapi.MapiMessage.remove_attachments("AttachmentsToRemove_out.msg")
Ayrıca, statik destroy_attachments yöntemini şu sınıfın: MapiMessage sınıf. remove_attachments yönteminden daha hızlı çalışır çünkü remove_attachments yöntemi mesaj dosyasını ayrıştırır.
import aspose.email as ae
# Destroy attachments in the MapiMessage
ae.mapi.MapiMessage.destroy_attachments("AttachmentsToDestroy_out.msg")
MSG Ekleri Ekle
Bir Outlook mesajı, eklerde başka Microsoft Outlook mesajlarını normal ya da gömülü olarak içerebilir. MapiAttachmentCollection aşağıdakinin aşırı yüklü üyelerini sağlar: ekle her iki tür ek ile Outlook mesajları oluşturmak için yöntem.
Deneyin!
Ücretsiz olan ile çevrimiçi e-posta eklerini ekleyin veya kaldırın Aspose.Email Editor App.
MAPI Mesajlarına Referans Ekleri Ekle
Referans eki genellikle gerçek dosya yerine harici bir kaynağa referans veya bağlantı içeren bir eki ifade eder. Bu referanslar, genellikle HTML e-postalarında, uzak bir sunucuda barındırılan harici görsellere veya kaynaklara bağlanmak için kullanılır. Tüm dosyayı gömmek yerine, bir referans eki harici içeriğe bir URL veya referans içerir.
Bir MAPI mesajına referans eki eklemek için şunu kullanın: add metodu MapiAttachmentCollection class ve ReferenceAttachmentOptions belirtmek için sınıf:
-
İçerik URL’si (dosyaya doğrudan link)
-
Sağlayıcı URL’si (dosyanın konumuna ya da UI görünümüne link)
-
Ek sağlayıcı adı (örneğin, "GoogleDrive")
aşağıdaki kod örneğinde gösterildiği gibi.
import aspose.email as ae
# Load or create a MapiMessage
msg = ae.mapi.MapiMessage()
msg.subject = "Email with Reference Attachment"
msg.body = "This message contains a cloud-based reference attachment."
# Create ReferenceAttachmentOptions
options = ae.mapi.ReferenceAttachmentOptions(
"https://drive.google.com/file/d/1HJ-M3F2qq1oRrTZ2GZhUdErJNy2CT3DF/",
"https://drive.google.com/drive/my-drive",
"GoogleDrive"
)
# Add the reference attachment to the message
msg.attachments.add("Document.pdf", options)
# Save the message to MSG format
msg.save("Message_With_Reference_Attachment.msg")
Mesajları Ek Olarak Göm
Aşağıdaki kod parçacığı, Outlook MSG dosyalarını başka bir MSG dosyasına ek olarak nasıl gömeceğinizi gösterir. Gömülü bir MSG dosyası, türü "message" olan bir ek olduğunu gösteren 5 değerine sahip PR_ATTACH_METHOD özelliğini içerir.
import aspose.email as ae
# Create a new MapiMessage
message = ae.mapi.MapiMessage("from@test.com", "to@test.com", "Subj", "This is a message body")
# Load the attachment message
attach_msg = ae.mapi.MapiMessage.load("Message.msg")
# Add the attachment to the message
message.attachments.add("Weekly report.msg", attach_msg)
# Save the message with the embedded message attachment
message.save("WithEmbeddedMsg_out.msg")
Eklerden Gömülü Mesajları Okuma
Aşağıdaki kod parçacığı, eklerden gömülü mesajları nasıl okuyacağınızı gösterir.
import aspose.email as ae
file_name = "path/to/file.msg"
# Load the MapiMessage from file
message = ae.mapi.MapiMessage.load(file_name)
# Check if the first attachment is an Outlook message
if message.attachments[0].object_data.is_outlook_message:
# Get the embedded message as MapiMessage
embedded_message = message.attachments[0].object_data.to_mapi_message()
# Perform further operations with the embedded message
# ...
Ekleri Ekle ve Değiştir
Aspose.Email API, ekleri ana mesaj içinde belirli bir indexe ekleme işlevi sunar. Ayrıca, mevcut bir ekin içeriğini farklı bir mesaj ekiyle değiştirme yeteneği sağlar. Aşağıda, ek ekleme ve değiştirme işlemlerini gösteren kod parçacıkları bulunmaktadır.
Ekleri Belirli Konumlara Ekle
Aspose.Email API, Insert yöntemi ile bir MSG ekini ana MSG içine eklemenizi sağlar: MapiAttachmentCollection. Bu yöntem, MapiAttachmentCollection Insert(int index, string name, MapiMessage msg) imzasıyla kullanılır. Aşağıda, bir eki belirli bir konuma nasıl ekleyeceğinizi gösteren bir kod parçacığı yer almaktadır:
import aspose.email as ae
from io import BytesIO
file_name = "path/to/file.msg"
# Load the MapiMessage from file
message = ae.mapi.MapiMessage.load(file_name)
# Save the attachment to a memory stream
memory_stream = BytesIO()
message.attachments[2].save(memory_stream)
# Load the attachment from the memory stream
get_data = ae.mapi.MapiMessage.load(memory_stream)
# Insert the loaded attachment at index 1
message.attachments.insert(1, "new 11", get_data)
Ek İçeriklerini Değiştir
Şunu kullanabilirsiniz: replace gömülü bir ekin içeriğini yeni verilerle güncellemek için kullanılan yöntem. Ancak, bu yöntemin sadece 2 ek tutan bir koleksiyona PR_ATTACH_NUM = 4 olan bir ek eklemek için kullanılamayacağını unutmayın. Aşağıda, ek içeriğini nasıl değiştireceğinizi gösteren bir kod örneği bulunmaktadır:
import aspose.email as ae
from io import BytesIO
file_name = "path/to/file.msg"
# Load the MapiMessage from file
message = ae.mapi.MapiMessage.load(file_name)
# Save the attachment to a memory stream
memory_stream = BytesIO()
message.attachments[2].save(memory_stream)
# Load the attachment from the memory stream
get_data = ae.mapi.MapiMessage.load(memory_stream)
# Replace the attachment at index 1 with the loaded attachment
message.attachments.replace(1, "new 1", get_data)
MapiMessages’daki Ekleri Yeniden Adlandır
Aspose.Email ile bir dosyadan yüklenen e-posta mesajlarındaki eklerin görüntüleme adlarını kolayca değiştirebilirsiniz. Aşağıdaki kod örneği, bunu nasıl yapacağınızı göstermektedir:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("message.msg")
msg.attachments[0].display_name = "New display name 1"
msg.attachments[1].display_name = "New display name 2"