Kailayla
Kailayla

Reputation: 323

Get XML file from email attachement to variable

So, I want to be able to get an XML file attachment from an email and store it in the database for later use.

I can retrieve the emails and store the SenderEmailAdress, sentOn and the body in the database. Now I want to add the XML file attachment to the database too, so I can use it later.

How can I get the attachment file (or the content) from the email and to a variable so I can add it to the DB?

Here's the code for the attachment I have now (so after I get the Mail Item):

//Check for attachments.
int AttachCnt = oMsg.Attachments.Count;

// Check if there are any attachments
if (AttachCnt > 0)
{
    for (int i = 1; i <= AttachCnt; i++)
    {
        System.Diagnostics.Debug.WriteLine(i.ToString() + " - FileName: " + oMsg.Attachments[i].FileName);
        String ext = Path.GetExtension(oMsg.Attachments[i].FileName);
        // check if the file extention is .xml
        if (ext == ".xml")
        {
            // Get the file ready to store in the DB. This is what I want to know!
            return;
        }                            
    }                            
}

Upvotes: 1

Views: 806

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66245

Save the attachment as a file (Attachment.SaveAsFile), then read the file data into a variable.

Upvotes: 1

Related Questions