Forwarding 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 slightly touches received e-mail before resending: "FW:" is inserted to "Subject:" field and "To:" field is replaced with the e-mail address of the actual recipient.

Note: MailBee is also capable of resending messages without "touching" them (so original message is fully preserved). This requires that messages can be sent to recipients different from those specified in "To:" or "CC:" fields. See Redirecting message sample for details.

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
      
        ' Replace recipients with forwarded recipient
        objMsg.ToAddr = "bill@host.com"
        
        ' Do not send to original CC's
        objMsg.CCAddr = ""
        
        ' Alter "Subject:" field
        objMsg.Subject = "FW: " & objMsg.Subject
        
        ' Tell SMTP component which message to send
        Set objSMTP.Message = objMsg

        ' Try to send e-mail
        If Not objSMTP.Send 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
      
        ' Replace recipients with forwarded recipient
        objMsg.ToAddr = "bill@host.com"
        
        ' Do not send to original CC's
        objMsg.CCAddr = ""
        
        ' Alter "Subject:" field
        objMsg.Subject = "FW: " & objMsg.Subject
        
        ' Tell SMTP component which message to send
        Set objSMTP.Message = objMsg

        ' Try to send e-mail
        If Not objSMTP.Send 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.Message Property

 


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