【问题标题】:Reference messages and access attachments参考消息和访问附件
【发布时间】:2015-07-30 13:46:16
【问题描述】:

我正在编写一个程序来跟踪项目的当前状态。

用户希望将相关文档保存到当前项目。我可以使用 FileSaveDialog 对位于文件夹中的文件执行此操作。但是,很多时候该文件是电子邮件消息或消息的附件。我想直接从 Outlook 中获取此邮件,然后将邮件另存为 MSG 或保存附件。

我有如下代码来引用来自 VB.NET 的 Outlook 邮件,但我不知道如何引用整个邮件以保存为 msg 或附件文件名。

Dim objOutlook As Outlook._Application
objOutlook = New Outlook.Application()
Dim objSelection As Outlook.Selection = objOutlook.ActiveExplorer.Selection
Dim iCount As Int16 = objSelection.Count
For i = iCount To 1 Step -1
    Console.WriteLine(objSelection.Item(i).Subject)
    Console.WriteLine(objSelection.Item(i).Attachments)
Next

【问题讨论】:

    标签: vb.net outlook


    【解决方案1】:

    为此使用 Outlook 对象库。 关于如何从未读邮件中下载附件的示例:

    Private Sub ThisAddIn_NewMail() Handles Application.NewMail
    Dim inBox As Outlook.MAPIFolder = Me.Application.ActiveExplorer() _
        .Session.GetDefaultFolder(Outlook. _
        OlDefaultFolders.olFolderInbox)
    Dim inBoxItems As Outlook.Items = inBox.Items
    Dim newEmail As Outlook.MailItem
    inBoxItems = inBoxItems.Restrict("[Unread] = true")
    Try 
        For Each collectionItem As Object In inBoxItems
            newEmail = TryCast(collectionItem, Outlook.MailItem)
            If newEmail IsNot Nothing Then 
                If newEmail.Attachments.Count > 0 Then 
                    For i As Integer = 1 To newEmail.Attachments.Count
                        Dim saveAttachment As Outlook.Attachment = _
                            newEmail.Attachments(i)
                        newEmail.Attachments(i).SaveAsFile _
                            ("C:\TestFileSave\" & (newEmail _
                            .Attachments(i).FileName))
                    Next i
                End If 
            End If 
        Next collectionItem
    Catch ex As Exception
        If Left(ex.Message, 11) = "Cannot save" Then
            MsgBox("Create Folder C:\TestFileSave")
        End If 
    End Try 
    

    结束子

    祝你好运!

    Source: msdn

    【讨论】:

      【解决方案2】:

      在保存电子邮件时遇到与您相同的问题,我最终得到了以下解决方案:

      Sub SaveEmail()
         'Save e-mail from Outlook
         Dim objOL As Outlook.Application
         Dim objMsg As Outlook.MailItem 'Object
         Dim objSelection As Outlook.Selection
         Dim strFile As String
      
         'Instantiate an Outlook Application object.
         objOL = CreateObject("Outlook.Application")
      
         'Get the collection of selected objects.
         objSelection = objOL.ActiveExplorer.Selection
      
         'Set the target folder
         Dim FilePath1 as String
         FilePath1 = "C:\tmp\"
      
         'Save each selected e-mail to disk
         For Each objMsg In objSelection
            'Save attachment before deleting from item.
            'Get the file name using "objMsg.Subject" and remove special characters.
            strFile = Regex.Replace(objMsg.Subject, "[^a-zA-Z0-9_ -]", "-",_ 
            RegexOptions.Compiled)
      
            'Combine with the path to the Temp folder.
            strFile = FilePath1 & strFile & ".msg"
      
            'Save the attachment as a file.
            objMsg.SaveAs(strFile, Outlook.OlSaveAsType.olMSG)
         Next
      End Sub
      

      有关 regex.replace 函数的一些输入,请参阅以下链接:

      https://www.regular-expressions.info/charclass.html

      https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.replace?view=netframework-4.7.2#System_Text_RegularExpressions_Regex_Replace_System_String_System_String_System_String_

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-25
        • 2016-02-25
        • 2019-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多