Downloading entire message

This sample connects to IMAP4 server, logs in e-mail account, selects "Inbox" folder and entirely downloads first message in the folder.

"From:", "To:", "Subject:", body text, and filenames of attachments are displayed for the downloaded message.

Visual Basic

Dim objIMAP4, objMsg, objAttach, strEmailInfo

Set objIMAP4 = CreateObject("MailBee.IMAP4")

' Unlock IMAP4 object
objIMAP4.LicenseKey = "put your license key here"

' Set IMAP4 server name
objIMAP4.ServerName = "mail.server.com"

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

' Connect to the server and
' log in email account
If objIMAP4.Connect Then

  ' Select Inbox folder
  If objIMAP4.SelectMailbox("Inbox") Then

    ' Completely retrieve a message
    Set objMsg = objIMAP4.RetrieveSingleMessage(1, False)

    If Not objIMAP4.IsError Then

      ' Add message headers to message's info string
      strEmailInfo = _
        "From:      " & objMsg.FromAddr & vbCrLf & _
        "To:        " & objMsg.ToAddr & vbCrLf & _
        "Subject:   " & objMsg.Subject & vbCrLf
        "Attach(s): "

      ' Add attachments' names to message's info string
      For Each objAttach In objMsg.Attachments
        ' Skip inline attachments (embedded images and so on)
        If Not objAttach.IsInline Then
          strEmailInfo = strEmailInfo & " " & objAttach.Filename
        End If
      Next

      ' Add body text to message's info string
      strEmailInfo = strEmailInfo & vbCrLf & vbCrLf & objMsg.BodyText

      ' Display message's info
      MsgBox strEmailInfo
    Else
      ' Display error information
      MsgBox "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc
    End If
  Else
    ' Display error information
    MsgBox "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc
  End If

  ' Close the connection
  objIMAP4.Disconnect
Else
  ' Display error information
  MsgBox "Error #" & objIMAP4.ErrCode
  MsgBox "Server response: " & objIMAP4.ServerResponse
End If

ASP

<%
Dim objIMAP4, objMsg, objAttach, strEmailInfo

Set objIMAP4 = Server.CreateObject("MailBee.IMAP4")

' Unlock IMAP4 object
objIMAP4.LicenseKey = "put your license key here"

' Set IMAP4 server name
objIMAP4.ServerName = "mail.server.com"

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

' Connect to the server and
' log in email account
If objIMAP4.Connect Then

  ' Select Inbox folder
  If objIMAP4.SelectMailbox("Inbox") Then

    ' Completely retrieve a message
    Set objMsg = objIMAP4.RetrieveSingleMessage(1, False)

    If Not objIMAP4.IsError Then

      ' Add message headers to message's info string
      '
      ' We use Server.HTMLEncode to properly handle situation
      ' when HTML-unsafe chars (such as '<', '>', '&') appear
      ' somewehere in "From:", "To:" or "Subject:" fields
      strEmailInfo = _
        "From:      " & Server.HTMLEncode(objMsg.FromAddr) & "<br>" & _
        "To:        " & Server.HTMLEncode(objMsg.ToAddr) & "<br>" & _
        "Subject:   " & Server.HTMLEncode(objMsg.Subject) & "<br>" & _
        "Attach(s): "

      ' Add attachments' names to message's info string
      For Each objAttach In objMsg.Attachments
        ' Skip inline attachments (embedded images and so on)
        If Not objAttach.IsInline Then
          strEmailInfo = strEmailInfo & " " & objAttach.Filename
        End If
      Next

      ' Add body text to message's info string
      strEmailInfo = strEmailInfo & "<br><br>" & objMsg.BodyText

      ' Display message's info
      Response.Write strEmailInfo
    Else
      ' Display error information
      Response.Write "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc
    End If
  Else
    ' Display error information
    Response.Write "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc
  End If

  ' Close the connection
  objIMAP4.Disconnect
Else
  ' Display error information
  Response.Write "Error #" & objIMAP4.ErrCode & "<br>"
  Response.Write "Server response: " & objIMAP4.ServerResponse
End If
%>

See Also:

RetrieveSingleMessage Method

Message Object