INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Outlook Automation - send emails from a different account/profile

Outlook Automation - send emails from a different account/profile

Outlook Automation - send emails from a different account/profile

(OP)
Hello Tek-Tips Gurus

Please can anyone help me with Outlook automation - specifically with making use of a non-default secondary profile?

I'm still using VFP6 (yes!) and have Outlook 2007 for dev/tests - will be 2010 when live.

Initially my two accounts were set up in Outlook and I tried to set the MailItem.SendUsingAccount property to the account object I want - with
myOLApp.Session.Accounts.Item(i)

I know the second account is accessible because my test code puts the DisplayName into the Subject line, yet the emails are still sent from the default account.

So, I moved onto the NameSpace Logon method - but this didn't work for me either.

Next, I changed the email accounts to profiles set up via the control panel, rather than directly in Outlook and I now have IMAP/SMTP profile accounts. I can start Outlook and log in as either user from the dialog window but I still cannot automate the LogOn. Emails always send from the Outlook profile when I open without the dialog prompt, if I select the second profile then, of course, emails send as I want them to.

To further complicate things - when it comes to using the live app, users already have Outlook open on their own profiles. I guess it wouldn't be too much of a problem to sync, force send/recv then close and reopen Outlook if I had to, but that's not ideal.

The objective is for certain emails to be routed thru the 2nd profile, and for users to be able to see sent & recvd without having to log in/out to switch accounts.

Here's a much shortened version of the code

CODE

myOLApp = createobject("Outlook.Application")
oNamespace = myOLApp.GetNameSpace("MAPI")
oNamespace.LogOff()
oNamespace.LogOn('proname2',,.F.,.F.)
oMailFolder = oNamespace.GetDefaultFolder(6)			&& Inbox
oItem = myOLApp.CreateItem(olMailItem)

**.. usual bits for body, attachments etc
* i = cycle thru the accounts to match by parameters
oItem.Subject=myOLApp.Session.Accounts.Item(i).DisplayName	&& for testing
oItem.SendUsingAccount = myOLApp.Session.Accounts.Item(i)

**..
oItem.Send

**.. sync(?), null, close etc 


I am obviously missing something, but I'm chasing my tail now.

Any pointers please, oh wise ones?

Karen

RE: Outlook Automation - send emails from a different account/profile

not sure about outlook versions with this but try something like

	
FOR EACH loAccount IN myOLApp.SESSION.Accounts
    IF loAccount.displayname = m.lcYourParameter
	oItem.SendUsingAccount = loAccount
	EXIT
    ENDIF
NEXT

 


hth

n

RE: Outlook Automation - send emails from a different account/profile

(OP)
Hi nigelgomm

I do cycle thru the accounts to match to a parameter in a control file. Also set oAccount object instead of applying from Outlook object call - but it makes no difference. I just shortened the code sample for the thread.

Still, thank you all the same.

All the best
Karen

RE: Outlook Automation - send emails from a different account/profile

karen,

sorry i missed the .sendusingaccount in your example.

n

RE: Outlook Automation - send emails from a different account/profile

In general, it should work. I used the following code variant to verify with Outlook 2007.

First, make sure you know the correct account display names by listing them:

CODE -->

Local loOutlook
loOutlook = Createobject("Outlook.Application")
For Each loAccount In loOutlook.Session.Accounts
    ? loAccount.Displayname
Endfor 

And here's a function creating a mail item sending from some account:

CODE

#Define olMailItem 0
#Define olFolderInbox 6

LOCAL loMail
loMail = CreateMailItemSendingFrom("foo@bar.org")
loMail.Display()

Function CreateMailItemSendingFrom(tcAccountname As String)

   Local loOutlook, loNamespace, loMailFolder, loMailItem
   loOutlook = Createobject("Outlook.Application")
   loNamespace = loOutlook.GetNameSpace("MAPI")
   loNamespace.LogOff()
   loNamespace.LogOn(tcAccountname,,.T.,.F.)
   loMailFolder = loNamespace.GetDefaultFolder(olFolderInbox)
   loMailItem = loOutlook.CreateItem(olMailItem)

   For Each loAccount In loOutlook.Session.Accounts
      If loAccount.DisplayName == tcAccountname
         loMailItem.Subject = "sent from " + loAccount.DisplayName  && for testing
         loMailItem.SendUsingAccount = loAccount
         Exit
      Endif
   Endfor

   RETURN loMailItem 
ENDFUNC 
In the FOR loop I use the FOR EACH oVar IN Collection syntax instead of a counter variable, maybe that makes a difference.

I also made the test without setting SendUsingAccount to see, whether the LogOn already sets the sending account, but it doesn't. So setting SendUsingAccount has to be done and also works this way (for me).

Notice I changed the LogOn to show the logon dialog (third parameter .T.). When you do LogOn() without providing a password (the second parameter is skipped) and also tell the LogOn() to not show the log on dialog (when the third parameter is .F.), you may never really log on to a working Outlook session.

What happens with the ShowDialog=.T. option for me still is a silent log on, as there is no password protection of my PST files. IIRC from a past setup, this dialog would show, if a password is needed, and in that case, that'll be absolutely necessary to get a working session, so it's good to have ShowDialog=.T., even if no password is necessary now. If there will be, you can later decide to let this be interactive or let your mail automation know the necessary passwords.

Bye, Olaf.

RE: Outlook Automation - send emails from a different account/profile

(OP)
Hi Olaf

Thank you for your response. I must remember to use FOR EACH, as it does make a difference in many object collections

Using your code the subject line shows correctly but it still doesn't work on SendUsingAccount. I know this works for some.

I might just remove office and start again from scratch with the profiles.

Failing that - I'll test onsite next week

Will post my findings "when" I ever get there

All the best

Karen

RE: Outlook Automation - send emails from a different account/profile

Well, good luck on site, then.

Bye, Olaf.

RE: Outlook Automation - send emails from a different account/profile

(OP)
Good afternoon

I have a bit more info. The company has Microsoft Exchange and Outlook 2010 / Office 365

Their central email account is only set up on one computer but the other users now have the permissions to send from the central account. The address appears in the email message from / sender drop down

SendUsingAccount requires an account object, so does the Sender property - so I cannot use either of those

Has anyone successfully used SentOnBehalfOfName?

Thank you

Karen


RE: Outlook Automation - send emails from a different account/profile

(OP)
OK, quick update - SentOnBehalfOfName works




RE: Outlook Automation - send emails from a different account/profile

(OP)

So far, this seems to work for me in Outlook 2007, so I will try in 2010 with MS Exchange

CODE

** gcSendAccount is my chosen address
** TempMsg.DBF has the recipient email address(es) in a field called EmailBox
*  and another memo for the Msg_Body - can change to HTMLBody instead of just Body
** Could use Recipients.Add and ResolveAll instead of just mailtem.To

#Define olMailItem 0

loMail = CreateMailItemSentOnBehalf(gcSendAccount)

Function CreateMailItemSentOnBehalf
   PARAMETER tcAccountname
   Local loOutlook, loMailItem
   loOutlook = Createobject("Outlook.Application")
   loMailItem = loOutlook.CreateItem(olMailItem)
   loMailItem.To = TRIM(TempMsg.EmailBox)
   loMailItem.BCC = gcSendAccount
   loMailItem.Subject = "(2) SentOnBehalfOfName " + tcAccountname
   loMailItem.Body = TempMsg.Msg_Body

   ON ERROR loMailItem.Subject = loMailItem.Subject + "(Error!)"
   loMailItem.SentOnBehalfOfName = tcAccountname
   
   ON ERROR STORE .F. TO ruBothered
   For Each loReplyItem IN loMailItem.ReplyRecipients
	    loMailItem.ReplyRecipients.Remove(loReplyItem)
   EndFor
   loMailItem.ReplyRecipients.Add( tcAccountname )

   loMailItem.Send
      
ENDFUNC 

NB - Someone pointed out that multiple reply recipients will get picked up by spam filters - so I am removing any existing recipients first, before adding in the new one to match the account I wanted to send from and route replies to

Hope this is good enough to help someone else if they're stuck

All the best
Karen

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close