Pranav
Pranav

Reputation: 33

Fetching attachment from given .eml file and using that attachment to other mail

I am working on code in C#,but not going through on thing that is I have saved some .eml file on my disk now I am parsing each eml file and creating a new mail adding the eml file data to the new mail but I am unable to attach the attachments present in the .eml file to the new mail , can anybody please help?

Upvotes: 1

Views: 2084

Answers (3)

Jefin Mathew
Jefin Mathew

Reputation: 151

MailMessage message = new MailMessage();
MemoryStream ms = new MemoryStream(); //store the mail into this ms 'memory stream'
ms.Position = 0;
message.Attachments.Add(new Attachment(ms, attachmentName));

Upvotes: 0

Pranav
Pranav

Reputation: 33

I am using the follwing code but it shows the error ex = {"The process cannot access the file because it is being used by another process.\r\n":null}

                foreach (CDO.IBodyPart attach in msg.Attachments)
                {

                    i++;
                    string filenm = "C:\\mail_automation\\attachments\\xyz" + i +".eml";
                    if (File.Exists(filenm))
                    {


                        string fn = attach.FileName;
                        attach.SaveToFile("C:\\mail_automation\\attachments\\xyz" + i + ".eml");
                        Attachment data = new Attachment(filenm);
                        mailMessage.Attachments.Add(data);                    


                    }
                    else
                    {

                        File.Create(filenm);
                        string fn = attach.FileName;
                        attach.SaveToFile("C:\\mail_automation\\attachments\\xyz" + i + ".eml");
                        Attachment data = new Attachment(filenm);
                        mailMessage.Attachments.Add(data);
                    }

Upvotes: 1

Mauro Sampietro
Mauro Sampietro

Reputation: 2814

You have to extract the attachments and save them to disk first, then fetch it again into the new mail.

Code example here

Upvotes: 0

Related Questions