Redirecting message

Visual Basic Code | ASP Code

The sample demonstrates receiving e-mail from POP3 e-mail account and resending received e-mail to another recipient.

The code resends e-mail in its original state (as it came from POP3 server). Since e-mail is not altered, all original MIME information will be preserved.

To send e-mail to address different from the recipients in the e-mail itself, SMTP.SendEx method is used.

Note: Some SMTP servers do not allow sending e-mail to addresses different from specified in "To:", "CC:", "BCC:" headers.

For simplicity, logging SMTP/POP3 sessions into files and SMTP authentication are not used.

[Visual Basic]:

Dim objPOP3, objSMTP, objMsg

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

' Create SMTP mailer component
Set objSMTP = CreateObject("MailBee.SMTP")

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

' Unlock SMTP component
objSMTP.LicenseKey = "put your license key here"

' Specify name of SMTP server to resend e-mail to
objSMTP.ServerName = "smtp.server.com"

' Connect to POP3 server and log in e-mail account
If objPOP3.Connect("mail.domain1.com", 110, "user", "password") Then

   ' If the mailbox contains some messages,
   If objPOP3.MessageCount > 0 Then
      ' then grab the first e-mail.
      Set objMsg = objPOP3.RetrieveSingleMessage(1)
      
      ' Any errors?
      If objPOP3.IsError Then
        ' Notify user on e-mail retrieval error
        MsgBox "Error #" & objPOP3.ErrCode & ", " & objPOP3.ErrDesc
      Else
      
        ' Tell SMTP component which message to send
        Set objSMTP.Message = objMsg

        ' Try to send e-mail to required recipients
        If Not objSMTP.SendEx(, "bill@host.com") Then
          ' Notify user on send error
          MsgBox "Error #" & objSMTP.ErrCode & ", " & objSMTP.ErrDesc
        End If
      End If
   End If
   
   ' Although POP3 and SMTP objects automatically
   ' disconnect on object destruction, it's recommended
   ' to disconnect manually to free memory & resources
   ' as soon as they are no longer needed.
   If objSMTP.Connected Then objSMTP.Disconnect
   objPOP3.Disconnect
Else
  ' Notify user on POP3 connection error
  MsgBox "Error #" & objPOP3.ErrCode & ", " & objPOP3.ErrDesc
End If

[ASP]:

<%
Dim objPOP3, objSMTP, objMsg

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

' Create SMTP mailer component
Set objSMTP = CreateObject("MailBee.SMTP")

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

' Unlock SMTP component
objSMTP.LicenseKey = "put your license key here"

' Specify name of SMTP server to resend e-mail to
objSMTP.ServerName = "smtp.server.com"

' Connect to POP3 server and log in e-mail account
If objPOP3.Connect("mail.domain1.com", 110, "user", "password") Then

   ' If the mailbox contains some messages,
   If objPOP3.MessageCount > 0 Then
      ' then grab the first e-mail.
      Set objMsg = objPOP3.RetrieveSingleMessage(1)
      
      ' Any errors?
      If objPOP3.IsError Then
        ' Notify user on e-mail retrieval error
        Response.Write "Error #" & objPOP3.ErrCode & ", " & objPOP3.ErrDesc
      Else
      
        ' Tell SMTP component which message to send.
        Set objSMTP.Message = objMsg

        ' Try to send e-mail to required recipients
        If Not objSMTP.SendEx(, "bill@host.com") Then
          ' Notify user on send error
          Response.Write "Error #" & objSMTP.ErrCode & ", " & objSMTP.ErrDesc
        End If
      End If
   End If
   
   ' Although POP3 and SMTP objects automatically
   ' disconnect on object destruction, it's recommended
   ' to disconnect manually to free memory & resources
   ' as soon as they are no longer needed.
   If objSMTP.Connected Then objSMTP.Disconnect
   objPOP3.Disconnect
Else
  ' Notify user on POP3 connection error
  Response.Write "Error #" & objPOP3.ErrCode & ", " & objPOP3.ErrDesc
End If
%>
See Also:
SMTP.SendEx Method

 


Send feedback to AfterLogic
Copyright © 2002-2022, AfterLogic Corporation. All rights reserved.