Add only 1 category to a MailItem when saving multiple attachments

340 views Asked by At

I'm currently developing an Outlook Add-in which can save MailItems and Attachments into a SQL Database.

When a MailItem with Attachment is saved 2 categories are added to the MailItem saying that the message is saved and that the attachment is saved.

When adding a MailItem with only 1 Attachment, the categories are added correctly as shown below.RIGHT

But when I save a MailItem with 2 or more Attachments it looks like this: WRONG

this is the code for adding the category:

foreach (Outlook.Attachment att in mailItem.Attachments)
{
    try
    {
        att.SaveAsFile(Path.GetTempPath() + att.FileName);

        var fi = new FileInfo(Path.GetTempPath() + att.FileName);

        var attachment = Attachment.NieuwAttachment(att.FileName,
                                                    SelectedMap.DossierNr.ToString(
                                                        CultureInfo.InvariantCulture), -1,
                                                    Convert.ToInt32(SelectedMap.Tag), fi);
        if (!Attachment.InlezenAttachment(attachment)) continue;

        //if attachment is being saved add "attachment saved" category to mailitem
        mailItem.Categories = string.Format("{0}, {1}", OutlookCategories.CategorieBijlage, mailItem.Categories);
        mailItem.Save();
    }
    catch (Exception ex)
    {
        var dmsEx = new DmsException("Er is een fout opgetreden bij het opslaan van een bijlage.",
                                     ex.Message, ex);
        ExceptionLogger.LogError(dmsEx);
    }
}

Anyone can help me out with this???

1

There are 1 answers

1
Fnat1c On BEST ANSWER

What you should do is add a check to see if the category already exists:

use this:

if (!mailItem.Categories.Contains(OutlookCategories.CategorieBijlage))
{
     //if attachment is being saved add "attachment saved" category to mailitem
     mailItem.Categories = string.Format("{0}, {1}", OutlookCategories.CategorieBijlage, mailItem.Categories);
     //Opslaan van MailItem.
     mailItem.Save();
}