【问题标题】:I am unable to load the attachemnts through outlook add-in我无法通过 Outlook 加载项加载附件
【发布时间】:2020-10-05 22:10:19
【问题描述】:
                Microsoft.Office.Interop.Outlook.Application app = (Microsoft.Office.Interop.Outlook.Application)OutlookApp.Application;

                AttachmentList = new List<Microsoft.Office.Interop.Outlook.Attachment>();
                TotalAttachmentList = new List<Microsoft.Office.Interop.Outlook.Attachment>();
                object objMailItem = app.ActiveWindow();
                Controler.Utility.SenderEmailId = Sendername;
                object context = control.Context;
                if (context is Outlook._Inspector)
                {

                    Outlook.Inspector insp = context as Outlook.Inspector;
                    object item = insp.CurrentItem;
                    if (item is Outlook._MailItem)
                    {
                        Outlook._MailItem mail = item as Outlook._MailItem;
                        // do something with the email
                        MailSubject = mail.Subject;

                        Body = mail.Body;
                        for (int attchCount = 1; attchCount <= mail.Attachments.Count; attchCount++)
                        {
                            if (mail.Attachments.Count > 0)
                            {
                                for (int i = 1; i <= mail.Attachments.Count; i++)
                                {
                                    AttachmentList.Add(mail.Attachments)
                                }
                            }
                        }

                }
                Marshal.ReleaseComObject(item); item = null;
            }
            Marshal.ReleaseComObject(context); context = null;
            }

【问题讨论】:

  • 一个疯狂的猜测:不应该是AttachmentList.Add(mail.Attachments[i])吗?
  • 有什么建议吗?
  • 其他? - 可能不是。您是否应用了更改?

标签: c# .net outlook-addin add-in


【解决方案1】:

在代码中,您按以下方式定义附件列表:

AttachmentList = new List<Microsoft.Office.Interop.Outlook.Attachment>();

但是在您尝试添加附件集合而不是单个附件之后:

AttachmentList.Add(mail.Attachments)

相反,您需要获取Attachment 类的实例并将其传递/添加到列表中:


            TotalAttachmentList = new List<Microsoft.Office.Interop.Outlook.Attachment>();
            object objMailItem = app.ActiveWindow();
            Controler.Utility.SenderEmailId = Sendername;
            object context = control.Context;
            if (context is Outlook._Inspector)
            {

                Outlook.Inspector insp = context as Outlook.Inspector;
                object item = insp.CurrentItem;
                if (item is Outlook._MailItem)
                {
                    Outlook._MailItem mail = item as Outlook._MailItem;
                    // do something with the email
                    MailSubject = mail.Subject;

                    Body = mail.Body;
                    Outlook.Attachments attachments = mail.Attachments;
                    for (int attchCount = 1; attchCount <= attachments.Count; attchCount++)
                    {
                        if (attachments.Count > 0)
                        {
                            for (int i = 1; i <= attachments.Count; i++)
                            {
                                Outlook.Attachment attachment = attachments[i]; 

                                AttachmentList.Add(attachment)
                            }
                        }
                    }

            }
            Marshal.ReleaseComObject(item); item = null;
        }
        Marshal.ReleaseComObject(context); context = null;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 2022-11-11
    • 2012-10-05
    相关资源
    最近更新 更多