从特定发件人检索imap电子邮件

时间:2014-01-11 16:04:57

标签: vb.net parsing email vb6

我正在尝试将我在outlook的vb编辑器中编写的内容转换为vb6,以便它可以是一个独立的可执行文件。

它的作用是:

  1. 从特定发件人的新到达电子邮件中解析特定网址。该网址指向一个网页。我们称之为URL1
  2. 检索该网页的html内容,并从html内容中解析另一个网址。我们称之为URL2
  3. 从URL2下载pdf。
  4. 因此,在outlook vb环境中,它完全正常运行。但是,因为,我要离开Outlook,我需要替换一些代码,特别是:

    1. 如何从特定发件人检索电子邮件(imap)?现在我不能使用“Outlook.Items”,“。HTMLLody”等等或与outlook相关的任何东西。
    2. 将电子邮件的html输入我的html解析器
    3. 如何将程序限制为仅针对新电子邮件而不是已经解析过的电子邮件?
    4. 基本上,这是相关部分中的代码:

      strURL1 = GetBetween(1, itm.HTMLBody, "<a href=""https://", """>", vbTextCompare)
      
      Private Function GetBetween(ByVal Start As Long, Data As String, _
      StartString As String, EndString As String, _
      Optional ByVal CompareMethod As VbCompareMethod = vbBinaryCompare) As String
      
      Dim lonStart As Long, lonEnd As Long
      
      '1. Find start string.
      lonStart = InStr(Start, Data, StartString, CompareMethod)
      
      If lonStart > 0 Then
          '2. Move to end of start string.
          lonStart = lonStart + Len(StartString)
      
          '3. Find end string.
          lonEnd = InStr(lonStart, Data, EndString, CompareMethod)
      
          If lonEnd > 0 Then
              '4. Extract data between start and end strings.
              GetBetween = Mid$(Data, lonStart, lonEnd - lonStart)
          End If
      End If
      

      结束功能

      -------------------------下面是一些我必须消除的代码,我是新手,我不确定他们究竟做了什么,如果我需要替代vb6?-----

      Private Sub Application_Startup()
          Dim olApp As Outlook.Application
          Dim objNS As Outlook.NameSpace
          Set olApp = Outlook.Application
          Set objNS = olApp.GetNamespace("MAPI")
          Set myOlItems = objNS.GetDefaultFolder(olFolderInbox).Items
      End Sub
      
      
          Private Sub myOlItems_ItemAdd(ByVal item As Object)
      
          On Error GoTo ErrorHandler
      
          Dim Msg As Outlook.MailItem
      
              If TypeName(item) = "MailItem" Then
                  Set Msg = item
                  LaunchURL (item)
              End If
      
          ProgramExit:
            Exit Sub
          ErrorHandler:
            MsgBox Err.Number & " - " & Err.Description
            Resume ProgramExit
      
      End Sub
      
      
      Private WithEvents myOlItems As Outlook.Items
      

      提前致谢

1 个答案:

答案 0 :(得分:0)

您没有说出您感兴趣的Exchange版本......

我有一些VB6代码连接到Exchange 2003并读取邮箱。它使用CDO 1.21库(http://www.microsoft.com/en-gb/download/details.aspx?id=3671)。我在项目中引用了'\ Program Files \ ExchangeMapi \ cdo.dll'。然后我使用代码:

Dim s As mapi.Session
Dim Inbox As mapi.Folder
Dim Msgs As mapi.Messages
Dim Msg As mapi.Message
Dim Sender As AddressEntry
Dim i as Integer
Dim txt as String
Set s = CreateObject("MAPI.Session")
s.Logon "", "", False, True, 0, True, ServerName & vbLf & AccountName
Set Inbox = s.Inbox
Set Msgs = Inbox.Messages
For i = 1 To Msgs.Count
    Set Msg = Msgs.Item(i)
    Set Sender = Msg.Sender
    txt = Msg.Text
Next i

我在XP VM中运行此操作是因为我没有100%回忆,但可能是CDO和Windows 7彼此不喜欢。我记得从电子邮件中获取HTML正文可能存在问题。对不起,我不能更具体 - 它写了很长时间(尽管它仍然存在)。

CDO参考 http://msdn.microsoft.com/en-us/library/ms526914%28v=exchg.10%29.aspx