Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook Automation - send emails from a different account/profile 1

Status
Not open for further replies.

KarenLloyd

Programmer
Nov 23, 2005
138
GB
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
 
not sure about outlook versions with this but try something like

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

[/pre]


hth

n
 
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
 
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,,[highlight #FCE94F].T.[/highlight],.F.)
   loMailFolder = loNamespace.GetDefaultFolder(olFolderInbox)
   loMailItem = loOutlook.CreateItem(olMailItem)

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

   RETURN loMailItem 
ENDFUNC
In the FOR loop I use the [tt]FOR EACH oVar IN Collection[/tt] 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.
 
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
 
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


 

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top