JustQn4
JustQn4

Reputation: 459

VB Script to change the from field of emails in Outbox

I have emails in my Outbox in Outlook and I'd like to run a vb script to change the from field of these emails to some other email

I managed to do the following but it doesn't work as I'd like to and therefore I am missing the main piece. I'd appreciate if someone could help.

intFolderOutbox = 4
msoFileDialogOpen = 1

' Load requied objects
Set WshShell = WScript.CreateObject("WScript.Shell")    ' Windows Shell
Set ObjOlApp = CreateObject("Outlook.Application")      ' Outlook
Set ns = ObjOlApp.GetNamespace("MAPI")                  ' Outlook
Set box = ns.GetDefaultFolder(intFolderOutbox)          ' Outlook    

For Each Item In box.Items        

    *** HERE IS WHAT I NEED TO REPLACE THE FROM FIELD ****
    Item.sender = "[email protected]"
    Item.Update 
    Item.Save

Next

Something like the following works adding a recipient but I couldn't find the equivalent to the from field.

Item.Recipients.Add "[email protected]"

Here is something that could help but it doesn't work in my case

Set oAddrEntry = CreateObject("MAPI.AddressEntry") 
oAddrEntry.Name = SYSTEM_ADDRESS 
oAddrEntry.resolve
Set oNewMsg.sender = oAddrEntry
oNewMsg.Update
oNewMsg.Send 

Thanks

Upvotes: 1

Views: 1956

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66286

Firstly, once a message is submitted (and moved to Outbox) it cannot be touched - it belongs to the spooler.

Secondly, you cannot send on behalf of an arbitrary user. In case of Exchange, set the MailItem.SentOnBehalfOfName property to the name of the Exchange mailbox on whose behalf the current user can send. In case of POP3/SMTP accounts, set the MailItem.SendUsingAccount property to one of the accounts from the Namespace.Accounts collection.

Upvotes: 1

Related Questions