参考消息和访问附件

时间:2015-07-30 13:46:17

标签: vb.net outlook

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

用户希望将相关文档保存到当前项目中。我可以为驻留在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

2 个答案:

答案 0 :(得分: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 

End Sub

祝你好运!

Source: msdn

答案 1 :(得分:0)

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

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_