保存多个附件时,仅向MailItem添加一个类别

时间:2013-12-06 11:36:05

标签: c# outlook attachment categories mailitem

我目前正在开发一个Outlook加载项,可以将MailItems和附件保存到SQL数据库中。

当保存带附件的MailItem时,会向MailItem添加两个类别,说明该邮件已保存并且附件已保存。

添加仅包含1个附件的MailItem时,会正确添加类别,如下所示。RIGHT

但是当我保存一个包含2个或更多附件的MailItem时,它看起来像这样: WRONG

这是添加类别的代码:

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);
    }
}

任何人都可以帮我解决这个问题

1 个答案:

答案 0 :(得分:2)

您应该做的是添加一项检查以查看该类别是否已存在:

使用它:

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();
}