Link to home
Start Free TrialLog in
Avatar of markcharleton
markcharletonFlag for United States of America

asked on

Changing the Sender email in Outlook via VBA from Access

Hi guys, we have a vba programe in MS Access that sends emails to our customers from different accounts that we have in exchange (corresponding to different sales people etc). At the moment i can do this using the Outlook.MailItem.SentOnBehalfOfName field but the problem is that the mail created this way goes to Junk boxes of the customers if the From address is different from the locally logged in account. Is there a way to avoid this? I think we may be able to associate multiple  accounts when we login to outlook and then set the correct account into the From address. But i dont know how to do this, and i dont know if this will work and what our exchange person needs to do to make these associations. Help please.

I am using outlook 2010 and connecting to it via vba in access 2003. Here is a sample of code i have been playing with :



    Dim objOutlook As Outlook.Application
    Set objOutlook = CreateObject("Outlook.Application")
    Dim objOutlookMsg As Outlook.MailItem
    ' Create the message.
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
   
    With objOutlookMsg
    '## Add the From recipient(s) to the message.
   
'    Set objOutlookRecip = .Recipients.Add("KirkHarris@blueSolutions.co.uk")
'    objOutlookRecip.Type = olOriginator [****This doesnt work also]

'    .Sender = "KirkHarris@blueSolutions.co.uk" [*****This doesnt work because sender is Read only]
   
    .SentOnBehalfOfName = "KirkHarris@blueSolutions.co.uk" [******This works but gets filtered into junk in hotmail by microsoft smartscreen]
   
   
    objOutlookMsg.To = "alitwaij@hotmail.com;AliTwaij@bluesolutions.co.uk" [***Here the hotmail email goes to junk when using .SentOnBehalfOfName() ]
   
   
    .send
    '.Display
    'Set CurrentItem = Outlook.ActiveInspector.CurrentItem [me playing about here]
    'CurrentItem.Sender.EMail = "kirkharris@bluesolutions.co.uk"
   

   

    End With

many thanks
Avatar of David Lee
David Lee
Flag of United States of America image

Hi, markcharleton.

Outlook 2010 allows connecting up to 3 Exchange accounts to a single Outlook profile.  It'd then be possible to choose which account a message is sent from.  An alternative to this is to create multiple Outlook profiles, sign in to the correct profile, then send like normal.  Of course the user would have to have access to all the accounts they would send from.  Would one of these scenarios work for you?
Avatar of markcharleton

ASKER

Hi, this is better, but the process is an automated vba process and we have about 15 accounts that we may need to send with.

Another way i was thinking is if the email object had a ReplyFrom property but it doesnt seem to. Do you know if there is an equivalent ReplyFrom property please?
Another way i was thinking is if the email object had a ReplyFrom property but it doesnt seem to. Do you know if there is an equivalent ReplyFrom property please?
I'm not clear on what you mean.  Messages you receive have the address of the sender.  Messages you send can optionally specify an address you want replies to go to.
Yes i dont see how i can set this option using the outlook object model. With CDO there is a .ReplyTo attribute that can be set. But I cant find this in the outlook object model. Its not in the MailItem object as far as i can see. Can you help?
The attribute is there.  It's called ReplyRecipients.  You can add to it like this

objOutlookMsg.ReplyRecipients.Add "someone@company.com"
Thanks for that. This may be a solution. But ideally a solution that enables me to choose different '.From' addresses is best such that i dont use the SentOnBehalfOfName  attribute as this gets sent to the junk box. Any advice welcome on this please
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This helped me look at other ways of solving the problem.
Could you also please tell me how to log into a specific outlook account and use it. Assuming i know the username and password. I need a snippet of the code please. So that i can start sending from it.
Sure.  First, you don't log in to an account.  You log in to a profile.  Each profile can connect to multiple accounts, although only three of them can be Exchange accounts (i.e. three Exchange accounts per Outlook profile).  Something like this

Dim olkApp As Outlook.Application, olkSes As Outlook.NameSpace
'Create an instance of Outlook
Set olkApp = CreateObject("Outlook.Application")
'Create a new session
Set olkSes = olkApp.GetNamespace("MAPI")
'Log in to the a specific profile.  Change ProfileName to the name of the Outlook profile you want to log in to
olkSes.Logon "ProfileName"

Open in new window

Is it not possible to login with username and password so that we can mimick a login from a specific user (this is one that we created specifically for this purpose)? And if so , how can we send emails from this account/profile?

If we cant do that then how can we create a new profile in outlook? Is this what the admin guy should know?

And more importantly please what is the next bit of code that enables me to create the mail message object please