More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 15-Dec-2017 06:42

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: intermediate    Code area: Basic Outlook Printer Friendly Version
Title: Forward message and retain sender with CDO for Windows
Description: Forward messages received from a specific account to a specific mail server, retaining the original message's sender address and subject. Used to forward subscribe / unsubscribe messages to a list server. Fire this macro from a rule.
Date: 13-Nov-2005  13:09
Code level: intermediate
Code area: Basic Outlook
Posted by: Jim Boyce
This message is displayed as VB.NET
 Sub ForwardSubscribes(MyMail As MailItem)
    ' Jim Boyce and Sue Mosher
    Dim strID As String
    Dim olNS As Outlook.NameSpace
    Dim olMail As Outlook.MailItem
    strID = MyMail.EntryID
    Set olNS = Application.GetNamespace("MAPI")
    Set itm = olNS.GetItemFromID(strID)
    ' do stuff with olMail, e.g.
    Set myInbox = olNS.GetDefaultFolder(olFolderInbox)
    ' Substitute your own destination folder for NL Processed
    Set destFolder = myInbox.Folders("NL Processed")
    Dim iMsg
    Dim iConf
    Dim Flds
    Dim szServer
    'Set MessageItems = MessageFolder.Items
    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
    Set Flds = iConf.Fields
    szServer = "http://schemas.microsoft.com/cdo/configuration/"
    With Flds
        .Item(szServer & "smtpserver") = "192.168.0.3"
        .Item(szServer & "smtpconnectiontimeout") = 100 ' quick timeout
        .Item(szServer & "smtpauthenticate") = 2
        .Item(szServer & "sendusername") = "user"
        .Item(szServer & "sendpassword") = "password"
        .Update
    End With
    Set iMsg.Configuration = iConf

    ' Build the message
    ' substitute the e-mail address as needed
    iMsg.To = "someone@somewhere.tld"
    iMsg.From = itm.SenderEmailAddress
    iMsg.Subject = itm.Subject
    iMsg.Send
            
    ' Mark as read and move the message to the NL Processed folder
    itm.UnRead = False
    itm.Move destFolder
            
End Sub
All 59comments
Page [ 1 2 3 4 5 6 Next >>  
  18-Nov-2005  18:51   
I'm looking for some code that redirects an email to a given address, and this seems to be fairly close to what I'm looking for. I'm just getting some errors and I'm not sure what I need to change, or what to change those values to.

I changed the "szserver = " to my smtp server... but then I get a error that says "Relaying not allowed, please use SMTP AUTH".

I know what that means but how do I authorise inside this bit of code??? Thanks.
  18-Nov-2005  19:55   
Hi, Daniel.

The value of smtpauthenticate determines the authentication method:

0 = no authentication (anonymous)
1 = Basic, clear-text authentication, use sendusername and sendpassword
2 = NTLM, authenticates in current process security context

So, I think my example should actually use a value of 1 for smtpauthenticate.

Also, the computer on which the macro runs must be authorized on the server to relay.

Jim
  20-Nov-2005  02:55   
Hi Jim!
Unfortunatly it still doesn't appear to be working... I'd like to just describe my situation and goals to see if I am using the right script, or if I am way off!!

I am reciving emails addressed to xyz@domain.com, however that email should go to myfriend@theirdomain.com

So I need to redirect the email from sender@originaldomain.com to myfriend@theirdomain.com

I will be using rules for this, basically if xyz@domain.com is in the recipients I want to call the script and send it to the relevant address, preserving the original sender, body, subject. I know the rule is working because I also "flag" the message, just as a debugging measure for now.

I am using Outlook 2003 Professional, but I am not on an exchange server or anything like that.

Will I be able to do this with your script? What do I need to change!!??

Thanks very much!! Kind regards, Daniel.
  24-Nov-2005  02:33   
Or if Jim isn't availble at the moment, does anybody else have any ideas about this issue? I'm just not quite sure if A) this is the right script, or if it is B) what parts I need to change to suit my situation (above). Any help greatly appreciated!!
  24-Nov-2005  11:31   
This one doesn't move the old message, so you'll need to do something with the message after the rule forwards the message. Note that you need to replace some values in the script with the host name of your mail server, your mail account, and account password - Jim

Sub ForwardSubscribes(MyMail As MailItem)
    ' Jim Boyce and Sue Mosher
    Dim strID As String
    Dim olNS As Outlook.NameSpace
    Dim olMail As Outlook.MailItem
    strID = MyMail.EntryID
    Set olNS = Application.GetNamespace("MAPI")
    Set itm = olNS.GetItemFromID(strID)
    ' do stuff with olMail, e.g.
    Set myInbox = olNS.GetDefaultFolder(olFolderInbox)
    Dim iMsg
    Dim iConf
    Dim Flds
    Dim szServer
    'Set MessageItems = MessageFolder.Items
    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
    Set Flds = iConf.Fields
    szServer = "http://schemas.microsoft.com/cdo/configuration/"
    With Flds
        .Item(szServer & "smtpserver") = "your_mail_server_here"
        .Item(szServer & "smtpconnectiontimeout") = 100 ' quick timeout
        .Item(szServer & "smtpauthenticate") = 1
        .Item(szServer & "sendusername") = "your_mail_account_name"
        .Item(szServer & "sendpassword") = "your_account_password"
        .Update
    End With
    Set iMsg.Configuration = iConf

    ' Build the message
    ' substitute the e-mail address as needed
    iMsg.To = "myfriend@theirdomain.com"
    iMsg.From = itm.SenderEmailAddress
    iMsg.Subject = itm.Subject
    iMsg.Send
          
End Sub
  24-Nov-2005  12:04   
Also, understand that if your outgoing SMTP server doesn't require authentication, you can set the value of smtpauthenticate to 0 and omit the sendusername and sendpassword lines. - Jim
  21-Feb-2006  18:33   
Hi
I would like to know if I can forward emails to another email account based on a time constraint. I need emails received between 9AM to 9PM to be forwarded to another email account, and the remaining time to be left in the inbox.

Thanks
ylnwwd
  21-Feb-2006  19:34   
You could add some code at the beginning of the sub to check the current time and if not between 9am and 9pm, exit the sub. You would naturally have to fire the rule for every message.
  22-Feb-2006  03:29   
Hi

Your code is really helpful for me, but unfortuanetly I am not able to use it. I am getting an error here "iMsg.From = itm.SenderEmailAddress". Please do rectify it. Thanks.
 
  27-Feb-2006  10:43   
Kaartz, what error? What version of Outlook?
Page [ 1 2 3 4 5 6 Next >>