×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

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.

Students Click Here

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

Sending email using Outlook with email account other than the default

Sending email using Outlook with email account other than the default

(OP)

I have been looking at the following thread by ‘Steve-vfp9user’

https://www.tek-tips.com/viewthread.cfm?qid=1813317

CODE --> Revised

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.

RE: Sending email using Outlook with email account other than the default

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 https://docs.microsoft.com/en-us/office/vba/api/ou... 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

RE: Sending email using Outlook with email account other than the default

2
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

RE: Sending email using Outlook with email account other than the default

(OP)
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?

Quote (Nigel Gomm)

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.

RE: Sending email using Outlook with email account other than the default

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

RE: Sending email using Outlook with email account other than the default

(OP)
Chris,

Quote (Chris Miller)

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.

Quote:

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:

Quote:

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.

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! Already a Member? Login

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