我无法通过Outlook加载项加载附件

问题描述 投票:0回答:1
                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;
            }
c# .net outlook-addin add-in office-addins
1个回答
0
投票

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

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;
        }
© www.soinside.com 2019 - 2024. All rights reserved.