Free Support Forum - aspose.com

How to properly handle email attachments from a MSG file and sent via outgoing SMTP email

I am reading large number of emails (6000+) extracted from a PST file and saved as individual MSG files in a folder. Majority of these individual MSG emails have numerous attachments of all file types (JPG, DOC, PDF, XSL, PNG, BMP etc).

I have tried both the methods file save as well as memory stream and have run in to issues.

When I use file save method as shown below, few emails and attachments are saved properly but duplicate attachment file names create problems and I am unable to delete these attachment files after email is sent. The exception is "Cannot delete as file is in use".

//Get all the email message attachments
int i = 0;
foreach (MapiAttachment _attachment in _message.Attachments)
{
_attachment.Save(@"C:\PST_Extractor\MSG_Attachment\" + i + "__" + _attachment.FileName);
Emsg.Attachments.Insert(i, new System.Net.Mail.Attachment(@"C:\PST_Extractor\MSG_Attachment\" + i + "__" + _attachment.FileName));
i++;
}

When I use the memory stream method as sown below, emails and their attachments go out properly. But when I try to open the received email attachments they do not open and I get an error saying that attached files are damaged. The error screen-shot in case of a PDF attachment is attached for your reference.

//Get all the email message attachments
foreach (MapiAttachment _attachment in _message.Attachments)
{
MemoryStream _ms = new MemoryStream();
_attachment.Save(_ms);
Emsg.Attachments.Add(new System.Net.Mail.Attachment(_ms, _attachment.DisplayName));
}

Can anyone help that what do I need to do in case of memory stream email attachment method so the attached files sent via email can be opened and viewed correctly?

I like to mention that most probable solution would require specifying message save type as shown below, but in case of memory stream method shown above I don't find any relevant method and/or properties to specify save type.

mailMsg.Save("TestMsg.msg", MailMessageSaveType.OutlookMessageFormatUnicode);

Hi Mobeen,


Thanks for writing to Aspose.Email support team.

I have reviewed the sample code and it seems that you are using Aspose.Email and .NET libraries at the same time.

Could you please give a try to the following sample code which iterates through the PST file and saves all the attachments on disc. It also takes care if attachment is itself an outlook message then its extracted and saved differently as compared to normal attachment.

private static void SaveAttachmentsInMsg()
{
PersonalStorage pst = PersonalStorage.FromFile(“Sample.pst”);
FolderInfo folderInfo = pst.RootFolder;
foreach (MapiMessage msg in folderInfo.EnumerateMapiMessages())
{
foreach (MapiAttachment attachment in msg.Attachments)
{
if (attachment.ObjectData != null && attachment.ObjectData.IsOutlookMessage)
{
// For nested email attachments - save as msg
MapiMessage msgMessage = MapiMessage.FromStream((new MemoryStream(attachment.ObjectData.Data)));
msgMessage.Save(attachment.FileName + “.msg”);
}
else
{
// For other attachments
attachment.Save(attachment.LongFileName);
}
}
}
}

If it does not fulfill your requirement, please send us the sample PST, complete sample code using AE and error message(s) so that we may assist you as soon as possible.

Thanks for your quick response.

Unfortunately the suggested code does not address what I am trying to do. I am reading large number of MSG files from a folder in a loop and sending them as an SMTP email with all their attachments and body contents.

I am attaching a sample code and a sample MSG file. With this code the email does goes out with all attachments but the problem is that attachments in the received email does not open properly as there seems to be an issue with the encoding. It will be great if you can propose a solution with Aspose or Aspose/.Net hybrid. Thanks!

Hi Mobeen,


Thank you for writing to us.

You can use Aspose alone to achieve this task as follow. I have tested it using the following code and the email is received at the receiving end with proper attachments. Please remove any references to System.Net.Mail in your application. Let us know if we can be of any additional help to you.

Code Sample:

private static void TestMessage()<o:p></o:p>

{

MapiMessage mapi = MapiMessage.FromFile("00001.msg");

MailMessageInterpretor mi = MailMessageInterpretorFactory.Instance.GetIntepretor(mapi.MessageClass);

MailMessage mailMsg = mi.Interpret(mapi);

mailMsg.From = "from@gmail.com";

mailMsg.To = "to@domain.com";

mailMsg.Subject = "Test Sending Attachments.";

SmtpClient client = GetSmtpClient();

client.Send(mailMsg);

}

private static SmtpClient GetSmtpClient()

{

SmtpClient client = new SmtpClient("smtp.gmail.com", 587, "username", "password");

client.SecurityMode = SmtpSslSecurityMode.Explicit;

client.EnableSsl = true;

return client;

}

Thanks for all your assistance. The code snippet you provided addressed my issue.

Hi Mobeen,


Thanks for the feedback and please feel free to write to us if you have any additional query/inquiry related to Aspose.Email. We’ll be glad to assist you further.