代码之家  ›  专栏  ›  技术社区  ›  Craig

从电子邮件(或几千封电子邮件)中提取数据[基于交换]

  •  5
  • Craig  · 技术社区  · 16 年前

    我的市场部,祝福他们,决定在人们通过网页进入的地方抽奖。这很好,但是信息不会存储到任何类型的数据库中,而是作为电子邮件发送到交换邮箱。伟大的。

    我的挑战是从这些电子邮件中提取条目(和营销信息),并将它们存储在更有用的地方,比如平面文件或csv。唯一的好处是电子邮件的格式非常一致。

    我相信我可以花时间把所有的电子邮件保存到文件中,然后写一个应用程序来仔细阅读它们,但我希望能找到一个更优雅的解决方案。我可以通过程序访问一个交换邮箱,阅读所有电子邮件,然后保存这些数据吗?

    2 回复  |  直到 9 年前
        1
  •  2
  •   U62    16 年前

    获取Exchange邮箱中的邮件有很多不同的方法,但由于这似乎是您只希望运行一次以提取数据的方法,因此我建议您编写一个vba宏以在Outlook内部运行(在Outlook中打开了有问题的Exchange邮箱)。迭代特定邮箱中的邮件并从中读取正文文本非常容易。然后你可以用你想要的东西写一个文本文件。

        2
  •  7
  •   Craig    16 年前

    这是我用的代码……

    Private Sub btnGo_Click()
      If ComboBox1.SelText <> "" Then
        Dim objOutlook As New Outlook.Application
        Dim objNameSpace As Outlook.NameSpace
        Dim objInbox As MAPIFolder
        Dim objMail As mailItem
    
        //Get the MAPI reference
        Set objNameSpace = objOutlook.GetNamespace("MAPI")
    
        //Pick up the Inbox
        Set objInbox = objNameSpace.GetDefaultFolder(olFolderInbox)
        For Each objFolder In objInbox.Folders
           If (objFolder.Name = ComboBox1.SelText) Then
              Set objInbox = objFolder
           End If
        Next objFolder
    
        //Loop through the items in the Inbox
        Dim count As Integer
        count = 1
    
        For Each objMail In objInbox.Items
           lblStatus.Caption = "Count: " + CStr(count)
           If (CheckBox1.Value = False Or objMail.UnRead = True) Then
              ProcessMailItem (objMail.Body)
              count = count + 1
              objMail.UnRead = False
           End If
        Next objMail
      End If
    End Sub
    
    Private Sub ProcessMailItem(strBody As String)
       Open "C:\file.txt" For Append As 1
    
       Dim strTmp As String
       strTmp = Replace(strBody, vbNewLine, " ")
       strTmp = Replace(strTmp, vbCrLf, " ")
       strTmp = Replace(strTmp, Chr(13) & Chr(10), " ")
       strTmp = Replace(strTmp, ",", "_")
    
       //Extra Processing went here (Deleted for brevity)
       Print #1, strTmp
       Close #1
    
    End Sub
    
    Private Function Strip(strStart As String, strEnd As String, strBody As String) As String
       Dim iStart As Integer
       Dim iEnd As Integer
    
       iStart = InStr(strBody, strStart) + Len(strStart)
       If (strEnd = "xxx") Then
          iEnd = Len(strBody)
       Else
          iEnd = InStr(strBody, strEnd) - 1
       End If
    
       Strip = LTrim(RTrim(Mid(strBody, iStart, iEnd - iStart)))
    End Function
    
    
    Private Sub UserForm_Initialize()
      Dim objOutlook As New Outlook.Application
      Dim objNameSpace As Outlook.NameSpace
      Dim objInbox As MAPIFolder
      Dim objFolder As MAPIFolder
    
      //Get the MAPI reference
      Set objNameSpace = objOutlook.GetNamespace("MAPI")
    
      //Pick up the Inbox
      Set objInbox = objNameSpace.GetDefaultFolder(olFolderInbox)
    
      //Loop through the folders under the Inbox
      For Each objFolder In objInbox.Folders
        ComboBox1.AddItem objFolder.Name
      Next objFolder
    End Sub