Quantcast
Channel: Aspose.Email Product Family
Viewing all articles
Browse latest Browse all 1367

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

$
0
0

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?


Viewing all articles
Browse latest Browse all 1367

Trending Articles