TNEF Attachments Handling in Email Messages
Transport Neutral Encapsulation Format (TNEF) is a proprietary email attachment format used by Microsoft Outlook and Microsoft Exchange Server. The Aspose.Email API allows you to read email messages that have TNEF attachments and modify the contents of the attachment. The email can then be saved as a normal email or to the same format, preserving TNEF attachments. This article shows different code samples for working with messages containing TNEF attachments. This article also shows how to create TNEF EML files from Outlook MSG files.
Read a Message with TNEF Attachment
The following code snippet shows you how to read a message preserving TNEF attachments.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// The path to the File directory. | |
string dataDir = RunExamples.GetDataDir_Email(); | |
MsgLoadOptions options = new MsgLoadOptions(); | |
options.PreserveTnefAttachments = true; | |
MailMessage eml = MailMessage.Load(dataDir + "EmbeddedImage1.msg", options); | |
foreach (Attachment attachment in eml.Attachments) | |
{ | |
Console.WriteLine(attachment.Name); | |
} |
Read a Message without TNEF Attachment
The following code snippet shows you how to read a message without preserving TNEF attachments.
Update Resources in TNEF Attachment
The following code snippet shows you how to update resources in a TNEF attachment and preserve TNEF format.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
public static void TestUpdateResources(string dataDir) | |
{ | |
string fileName = dataDir + "tnefEML1.eml"; | |
string imgFileName = dataDir + "Untitled.jpg"; | |
string outFileName = dataDir + "UpdateTNEFAttachments_out.eml"; | |
MailMessage originalMailMessage = MailMessage.Load(fileName); | |
UpdateResources(originalMailMessage, imgFileName); | |
EmlSaveOptions emlSo = new EmlSaveOptions(MailMessageSaveType.EmlFormat); | |
emlSo.FileCompatibilityMode = FileCompatibilityMode.PreserveTnefAttachments; | |
originalMailMessage.Save(outFileName, emlSo); | |
} | |
private static void UpdateResources(MailMessage msg, string imgFileName) | |
{ | |
for (int i = 0; i < msg.Attachments.Count; i++) | |
{ | |
if ((msg.Attachments[i].ContentType.MediaType == "image/png") || (msg.Attachments[i].ContentType.MediaType == "application/octet-stream" && Path.GetExtension(msg.Attachments[i].ContentType.Name) == ".jpg")) | |
{ | |
msg.Attachments[i].ContentStream = new MemoryStream(File.ReadAllBytes(imgFileName)); | |
} | |
else if ((msg.Attachments[i].ContentType.MediaType == "message/rfc822") || (msg.Attachments[i].ContentType.MediaType == "application/octet-stream" && Path.GetExtension(msg.Attachments[i].ContentType.Name) == ".msg")) | |
{ | |
MemoryStream ms = new MemoryStream(); | |
msg.Attachments[i].Save(ms); | |
ms.Position = 0; | |
MailMessage embeddedMessage = MailMessage.Load(ms); | |
UpdateResources(embeddedMessage, imgFileName); | |
MemoryStream msProcessedEmbedded = new MemoryStream(); | |
embeddedMessage.Save(msProcessedEmbedded, SaveOptions.DefaultMsgUnicode); | |
msProcessedEmbedded.Position = 0; | |
msg.Attachments[i].ContentStream = msProcessedEmbedded; | |
} | |
} | |
foreach (LinkedResource att in msg.LinkedResources) | |
{ | |
if (att.ContentType.MediaType == "image/png") | |
att.ContentStream = new MemoryStream(File.ReadAllBytes(imgFileName)); | |
} | |
} |
Add Attachment to TNEF Message
The following code snippet shows you how to add new attachments to the main message containing TNEF.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
string fileName = "MainMessage.eml"; | |
string attachName = "s.png"; | |
string outFileName = "test.eml"; | |
MailMessage mailMessage = MailMessage.Load(fileName); | |
mailMessage.Attachments.Add(new Attachment(File.OpenRead(attachName), "s.png", "image/png")); | |
mailMessage.Save(outFileName, FileCompatibilityMode.PreserveTnefAttachments); |
Creating TNEF EML from MSG
Outlook MSGs sometimes contain information such as tables and text styles that may get disturbed if these are converted to EML. Creating TNEF messages from such MSG files allows to retain the formatting and even send such messages via the email clients retaining the formatting. The MailConversionOptions.ConvertAsTnef property is used to achieve this. The following code snippet shows you how to create TNEF EML from MSG.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
MapiMessage mapiMsg = MapiMessage.FromFile(dataDir + "Message.msg"); | |
MailConversionOptions mco = new MailConversionOptions(); | |
mco.ConvertAsTnef = true; | |
MailMessage message = mapiMsg.ToMailMessage(mco); |
For creating the TNEF, the following sample code can be used.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
MailMessageLoadOptions options = new MailMessageLoadOptions(); | |
options.MessageFormat = MessageFormat.Msg; | |
// The PreserveTnefAttachments option with MessageFormat.Msg will create the TNEF eml. | |
options.FileCompatibilityMode = FileCompatibilityMode.PreserveTnefAttachments; | |
MailMessage eml = MailMessage.Load(emlFileName, options); |
Identify TNEF Format Messages
The following code snippet shows you how to detect if a message is TNEF.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
MailMessage mail = MailMessage.Load(dataDir + "tnefEml1.eml"); | |
bool isTnef = mail.OriginalIsTnef; | |
Console.WriteLine("Is input EML originally TNEF? {0}", isTnef.ToString()); |
Identify TNEF Format Attachments
The Attachment.IsTnef property allows to detect whether the message attachment is TNEF formatted message.
var eml = MailMessage.Load(fileName);
foreach (attachment in eml.Attachments)
{
Console.WriteLine($"Is Attachment TNEF?: {attachment.IsTnef}");
}