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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending email using Outlook with email account other than the default 3

Status
Not open for further replies.

David Higgs

Programmer
May 6, 2012
390
0
16
GB
I have been looking at the following thread by ‘Steve-vfp9user’

[link ][/url]

Code:
oOutlook = Createobject('Outlook.Application')
oNameSpace = oOutlook.getnamespace('MAPI')
oOutbox = oNameSpace.GetDefaultFolder(4)
oItems = oOutbox.Items
oMailItem = oItems.Add(0)

oMailItem.To = "email_address.com"
oMailItem.Subject = "Test message"
oMailItem.HtmlBody = "Message sent from VFP"

oMailItem.Send

and have tried the code which meets my needs except that I would like to be able to send an email from Outlook using an email account other than the default email account.

What do I need to do to achieve this?


Regards,

David

Recreational Developer and End User of VFP for my personal use in 'Amateur Radio' and 'British Railways' related Applications.
 
First, to send from the default account, you even don't need the namespace "MAPI" and its default folder.

On the other side this also hints at the answer, you have hands on a MAPI namespace, and this offers some options.
You can logon to another than the default profile:

Code:
oNameSpace.Logon('Accountname','Password')

See for the logon method.
Notice while all parameters are described optional, no parameters do what is usually automatic, you logon to the default profile, especially when it isn't password protected. And there is no need to protect outlook profiles with passwords., though it is one way to detect when code automates your mail client and then fails as it relies on a default account not needing a logon.

Chriss
 
David,
there's a .sendusingaccount property on the mailitem object.

so.. to find the account you want.

Code:
loOutlook = CREATEOBJECT("outlook.application")
loMailitem = loOutlook.createitem(0)
loSendingAccount = NULL
FOR EACH loAccount IN loOutlook.SESSION.Accounts
	IF loAccount.displayname = "me@here.now" OR loAccount.smtpaddress = "me@here.now" && account we're looking for
		loSendingAccount = loAccount
		EXIT
	ENDIF
NEXT

but... there seems to be a bug in recent versions of outlook and you can't set .sendusingaccount direct from VFP (from memory something to do with outlook requiring a SET rather than a simple assignment).

Code:
IF !ISNULL(loSendingAccount)
	TRY
		loScript = CREATEOBJECT([MSScriptcontrol.scriptcontrol.1])
		loScript.LANGUAGE = "VBScript"
		loScript.ADDOBJECT("mailitem",loMailitem)
		loScript.ADDOBJECT("accountitem",loSendingAccount)
		loScript.ExecuteStatement("set mailitem.sendusingaccount = accountitem")
	CATCH TO loErr
	ENDTRY
ENDIF

loMailItem.To = "email_address.com"
loMailItem.Subject = "Test message"
loMailItem.HtmlBody = "Message sent from VFP"

loMailItem.Send

hth

n
 
Chris,

I tried your suggested code but the Default Email Account was still used. This maybe due to what Nigel Gomm mentions in his post?

Nigel Gomm said:
there seems to be a bug in recent versions of outlook and you can't set .sendusingaccount direct from VFP (from memory something to do with outlook requiring a SET rather than a simple assignment).

Nigel,

Thank you for reply and your sample code, much appreciated.

I tried your code as suggested and I’m pleased to report that I was able to send a message from an alternative Email Account. For information, I am using Microsoft Home & Business 2016.

I can now experiment with a relatively fixed format HTML message that will include some VFP data fields, general text and a couple of URLs.


Regards,

David

Recreational Developer and End User of VFP for my personal use in 'Amateur Radio' and 'British Railways' related Applications.
 
David,

I don't know what you tried, but if you used an email address for the accountname, that would fail. The doc specifies it as MAPI profile name, my bad in calling it accountname. The usual outlook profile is just your name.

Also, you can have multiple mail servers configured into the same outlook profile, which in the interactive use of Outlook appear as a dropdown list choice of mail addresses to send from. That's indeed covered by the mail object, then.

You see, it depends whether you just add further mail accounts to your outlook profile or define several outlook profiles, one for each mail address.

Chriss
 
Chris,

Chris Miller said:
I don't know what you tried, but if you used an email address for the accountname, that would fail. The doc specifies it as MAPI profile name, my bad in calling it accountname. The usual outlook profile is just your name.

I was actually using an email address although I did try using the profile name with no password but that didn't work.

Also, you can have multiple mail servers configured into the same outlook profile, which in the interactive use of Outlook appear as a dropdown list choice of mail addresses to send from.

I am using multiple email addresses with the same Outlook profile.

Earlier in the thread Nigel Gomm mentioned:

there's a .sendusingaccount property on the mailitem object but... there seems to be a bug in recent versions of outlook and you can't set .sendusingaccount direct from VFP (from memory something to do with outlook requiring a SET rather than a simple assignment)..

Regards,

David

Recreational Developer and End User of VFP for my personal use in 'Amateur Radio' and 'British Railways' related Applications.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top