Forwarded messages

This sample takes a e-mail from POP3 server, detects forwarded messages (if any) and shows their bodies.

E-mail message "A" contains forwarded message "B" if the "B" message was forwarded as attachment of the "A" message.

Forwarded messages can contain another forwarded messages (e.g. some message was forwarded several times) so nesting level is potentially unlimited. This sample detects forwarded messages in recursive manner making it possible to detect every forwarded message at any nesting level.

The sample gets e-mail from POP3 server but you can use the same approach for IMAP4 and for messages taken from disk or database.

Visual Basic

' The sub displays bodies of any forwarded messages
' within specified message (including messages within
' forwarded messages themsleves).
Sub DisplayForwardedBody(objMsg)
  Dim objForwardedMsg, objAttach

  ' Forwarded messages reside in Attachments list
  For Each objAttach In objMsg.Attachments
    ' Does the attachment hold a message inside?
    If objAttach.IsMessage Then

      ' Load the forwarded message from the attachment
      Set objForwardedMsg = CreateObject("MailBee.Message")
      objForwardedMsg.RawBody = objAttach.Content

      ' Display the body of the forwarded message
      MsgBox objForwardedMsg.BodyText

      ' The forwared message may contain another
      ' forwarded messages. Thus we repeat the procedure
      ' for every message found in the root message.
      DisplayForwardedBody objForwardedMsg
    End If
  Next 
End Sub

Dim objPOP3, objMsg

' Create POP3 object
Set objPOP3 = CreateObject("MailBee.POP3")

' Unlock POP3 object
objPOP3.LicenseKey = "put your license key here"

' Set POP3 server name
objPOP3.ServerName = "mail.server.com"

' Set user credentials
objPOP3.UserName = "username"
objPOP3.Password = "password"

' Download a message
Set objMsg = objPOP3.RetrieveSingleMessage(1)

If Not objPOP3.IsError Then
  ' Display bodies of forwarded messages
  DisplayForwardedBody objMsg
End If

' Disconnect if needed
If objPOP3.Connected Then
  objPOP3.Disconnect
End If

ASP

<%
' The sub displays bodies of any forwarded messages
' within specified message (including messages within
' forwarded messages themsleves).
Sub DisplayForwardedBody(objMsg)
  Dim objForwardedMsg, objAttach

  ' Forwarded messages reside in Attachments list
  For Each objAttach In objMsg.Attachments
    ' Does the attachment hold a message inside?
    If objAttach.IsMessage Then

      ' Load the forwarded message from the attachment
      Set objForwardedMsg = Server.CreateObject("MailBee.Message")
      objForwardedMsg.RawBody = objAttach.Content

      ' Display the body of the forwarded message
      Response.Write objForwardedMsg.BodyText & "<br><br>"

      ' The forwared message may contain another
      ' forwarded messages. Thus we repeat the procedure
      ' for every message found in the root message.
      DisplayForwardedBody objForwardedMsg
    End If
  Next 
End Sub

Dim objPOP3, objMsg

' Create POP3 object
Set objPOP3 = Server.CreateObject("MailBee.POP3")

' Unlock POP3 object
objPOP3.LicenseKey = "put your license key here"

' Set POP3 server name
objPOP3.ServerName = "mail.server.com"

' Set user credentials
objPOP3.UserName = "username"
objPOP3.Password = "password"

' Download a message
Set objMsg = objPOP3.RetrieveSingleMessage(1)

If Not objPOP3.IsError Then
  ' Display bodies of forwarded messages
  DisplayForwardedBody objMsg
End If

' Disconnect if needed
If objPOP3.Connected Then
  objPOP3.Disconnect
End If
%>

See Also:

Attachment.IsMessage Property

Message.RawBody Property