簡體   English   中英

如何從Winforms中的Outlook電子郵件中獲取附件?

[英]How to fetch attachment from outlook email in winforms?

Microsoft.Office.Interop.Outlook.Application Application = new Microsoft.Office.Interop.Outlook.Application();

                    Microsoft.Office.Interop.Outlook.MAPIFolder inBox = Application.ActiveExplorer().Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                    Microsoft.Office.Interop.Outlook.Items inBoxItems = inBox.Items;
                    Microsoft.Office.Interop.Outlook.MailItem newEmail = null;

                    foreach (object collectionItem in inBoxItems)
                    {
                        newEmail = collectionItem as Microsoft.Office.Interop.Outlook.MailItem;
                        if (newEmail != null)
                        {
                            if (newEmail.Attachments.Count > 0)
                            {
                                for (int i = 1; i <= newEmail
                                    .Attachments.Count; i++)
                                {
                                    newEmail.Attachments[i].SaveAsFile
                                        (@"C:\TestFileSave\" +
                                        newEmail.Attachments[i].FileName);
                                }
                            }
                        }
                    }

我已經從Outlook中復制了郵件並粘貼到window應用程序的flex-grid中,但是附件計數為零,但是郵件中有諸如excel,doc等文件的附件。

 int extractFileParentId = pkUniqueId;
        List<ZipExtracFile> extractFileList = lts;
        MsgReader.Outlook.Storage.Message message = new MsgReader.Outlook.Storage.Message(fileNames);

        foreach (var attachment in message.Attachments)
        {
            string fileName = string.Empty;
            pkUniqueId = pkUniqueId + 1;
            if (attachment.GetType() == typeof(MsgReader.Outlook.Storage.Attachment))
            {
                var attach = (MsgReader.Outlook.Storage.Attachment)attachment;
                fileName = Path.Combine(tempPath, (attach).FileName);

                File.WriteAllBytes(fileName, attach.Data);
                extractFileList.Add(new ZipExtracFile { pkUniqueId = pkUniqueId, fileName = fileName, parentId = extractFileParentId });

                if(Path.GetExtension(fileName).ToLower() == ".msg")
                {
                    ExtractMsgFile(fileName, ref pkUniqueId, ref tempPath, lts);
                }
            }
        }
        message.Dispose();

它用於從“ .msg”文件文檔中提取文檔以及多級“ .msg”文件,並且可以在特定位置下載。 為此,只需添加MsgReader.dll

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM