Msg attachment in msg saved without extension

Hello,

i’m using this code block to read attachment names from an msg file.

            Aspose.Email.MailMessage mailMsg = Aspose.Email.MailMessage.Load(inputFile);
            int i = 1;
            foreach (Aspose.Email.Attachment attachment in mailMsg.Attachments)
            {
                string attachmentFilename = "";
                if (outputFolder.Length > 0)
                {
                    if (outputFolder.EndsWith("\\"))
                        outputFolder.Remove(outputFolder.Length - 1);
                    attachmentFilename = Path.Combine(outputFolder, attachmentPrefix + i.ToString());
                }
                else
                    attachmentFilename = attachmentPrefix + i.ToString();
                    attachment.Save(attachmentFilename);
                    attachments.Add(attachmentFilename);
                    attachmentsNames.Add(attachment.Name);
                i++;

When an attachment is type “.msg”, it is saved without the extension, creating a number of problems. Is this supposed to happen. Is there a way to track if an attachment is msg and maybe add the file extension?

Thank you!

@panosk

Can you please share the source file with us along with extracted attachment so that I may investigate and help you out.

τεστ.zip (84.2 KB)

here you are.

@panosk

There are some custom declarations in sample code that you have shared. Can you please share a workable sample reproducing the issue.

image.png (20.4 KB)

string inputFile, string attachmentPrefix, string outputFolder, List attachments, List attachmentsNames, List metadataNames, List metadataValues
Aspose.Email.MailMessage mailMsg = Aspose.Email.MailMessage.Load(inputFile);
int i = 1;
foreach (Aspose.Email.Attachment attachment in mailMsg.Attachments)
{
string attachmentFilename = “”;
if (outputFolder.Length > 0)
{
if (outputFolder.EndsWith("\"))
outputFolder.Remove(outputFolder.Length - 1);
attachmentFilename = Path.Combine(outputFolder, attachmentPrefix + i.ToString());
}
else
attachmentFilename = attachmentPrefix + i.ToString();

                    attachment.Save(attachmentFilename);
                    attachments.Add(attachmentFilename);
                    attachmentsNames.Add(attachment.Name);
                i++;
            }

@panosk

I have created an issue with ID EMAILNET-39953 in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

Hello,

do you have any update regarding this issue?

Thank you.

@panosk

In attached project we have shown how to save inner message with appropriate extension.
Note - by default inner message converted in format of parent message. If you want preserve format of inner message to use PreserveEmbeddedMessageFormat option.

ConsoleApp1.zip (114.9 KB)

Hello again,

my aspose version does not support the “DetectFileFormat” function, at line:
FileFormatInfo info = FileFormatUtil.DetectFileFormat(stream);

Is there i work around / alternative for earlier versions?

Thank you.

@panosk

I am afraid there is not any workaround that I may offer for this case. We have shared with you possible solution using latest API version.

Hello support @mudassir.fayyaz
Is this issue been fixed on the latest release? I’m still having the issue where aspose is saving email attachments without extension (.eml file). Also, can you share the sample project with the workaround with me?

Hello @rusty02,

Can you, please, share the eml file and code snippets with us?

Thank you.

Hello @margarita.samodurova

Yes, I’ve attached a sample email and the code. Notice that the attachment email did not have the .eml extension when saving it as a file.

asposeIssueSample.zip (1.5 MB)

Hello @rusty02,

Thank you for the provided information. In your sample file the message contains an embedded message which may not have an extension property, as it is a message in eml format. In order to save an attachment with extension, you should make sure that the attachment is an embedded message and has extension. In case there is no extension, add it while saving. Use the following code:

// Loop through attachments
foreach (var attachment in msg.Attachments)
{
    // Check if attachment is an embedded
    if (attachment.IsEmbeddedMessage)
    {
        // Check if attachment has eml extension
        var extension = Path.GetExtension(attachment.Name);
        // Add .eml extension if it is absent
        if (extension == "")
        {
             attachment.Name += ".eml";
        }
    }
  
    attachment.Save(path + "\\" + timestamp + attachment.Name);
    ...
}

Hello @margarita.samodurova,

That seems to be a workaround. Why would an embedded message not have an extension? Is that expected behavior or an issue with aspose?

@rusty02,

Yes, this is an expected behavior. Embedded messages may not have an extension, as their format is same to the parent message. This is the reason why Outlook doesn’t add extensions to embedded messages, as it can display an embedded message properly anyway.

Thank you for applying to Aspose support.