Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss. print.

Use this simple technique to help your users compose and send email messages

The most popular topic in our Advanced Visual FoxPro course covers the sending and receiving of emails automatically from within a VFP application. We show how to do this by using Microsoft Outlook as an ActiveX Automation server. Although this works very well, the technique is somewhat complicated, and you can only use it if the user has Outlook installed.

In this article, we will describe a much simpler approach - one that works with any MAPI-compliant email client (including Outlook Express and Netscape Messenger). It is not as flexible as the ActiveX Automation method, but it has the advantage of requiring only a couple of lines of code.

The trick is to "shell execute" a mailto link. If you haven't used the ShellExecute() function before, it's worth taking a moment to get to know it. It is not a VFP function, but is part of the Windows shell. Before you can use it, you must declare it, like so:

DECLARE INTEGER ShellExecute IN shell32.dll ; 
	INTEGER hndWin, STRING cAction, STRING cFileName, ; 
	STRING cParams, STRING cDir, INTEGER nShowWin

You can put this code anywhere in your application, provided it is executed before you create the email message. (Note that ShellExecute is case-sensitive in the above command.)

So what does it do?

Essentially, ShellExecute() lets you "execute" any pogram, document or shortcut. At its simplest, it can be used to launch an external application. For example, this command will launch Notepad:


Alternatively, you could use the following code to launch Notepad and have it open a file called Readme.txt:


And this will launch Word and open Disclaimer.RTF:


The beauty of this is that you don't have to know which application is registered to open a specific document type on the user's system. If the user didn't have Word installed, the above code would launch Wordpad instead, as this can also process RTF files. Of course, if the user had no application at all associated with RTFs, the command would fail.

The second parameter to ShellExecute() lets you specify the action that you want to execute. By changing it to "print", for example, you can print the document rather than open it for editing. Similarly, the following code will play a sound file (using either the Windows Media Player or whatever other application is associated with the WAV extension):


Another possibility:


This will launch the user's default web browser and navigate to the site specified.

For more examples of this versatile function, see our article, Introducing ShellExecute().

What about email?
So, back to our original requirement. To create an email message, all you have to do is ShellExecute a mailto link. At its simplest, the link looks something like this:

So the following code will open a message composing window in the user's default mail client, with the To line already filled in:

lcMail = "mailto:john@mycompany.com" 

Going further, you can specify the following parameters to the mailto link:

CC= Carbon copy
BCC= Blind carbon copy
SUBJECT= Subject text
BODY= Body text

Place a question mark before the first of these parameters (right after the email address). Use ampersands to separate further parameters, as in this example:

lcMail = "mailto:john@mycompany.com"+ ;
	"?CC= boss@mycompany.com&Subject= Meet for lunch"+ ;
	"&Body= Please join me for a sandwich at noon." 

This will produce the message composing window shown in Figure 1. As you can see, the To, CC and Subject lines are already filled in, as is the text of the message. All that the user has to do is to click the Send button.

Figure 1: Use ShellExecute() to pop up this message window.

If you want to include a line break in the body of the message, insert the new-line character in "URL format", that is, as "%0A" (without the quotes). For example:

lcMail = "mailto:john@mycompany.com"+ ;
	"?CC= boss@mycompany.com&Subject= Meet for lunch"+ ;
	"&Body= Please join me for sandwich.%0AWe can meet at noon." 

This technique has some obvious limitation. First, it is not suitable for creating very long messages, as the entire mailto link is subject to a maximum of 2,047 characters. It cannot be used to create formatted messages, nor is it possible to add attachments to the message. More seriously, you have to rely on the user to actually send the message. You can't do that programmatically, nor can you prevent the user from cancelling the message rather than sending it.

On the other hand, if all you need is a simple way of helping the user to compose and send an unformatted message without attachments, the technique will work extremely well. You could use it, for example, to create a message which confirms an order or one which chases a customer for payment. It is especially useful if you want to let the user edit the text before hitting the Send button.

Mike Lewis Consultants Ltd. February 2002. Revised April 2005.


Mike Lewis Mike Lewis is a Visual FoxPro developer and trainer. His company, Mike Lewis Consultants Ltd., runs training courses at clients' sites throughout Europe and North America. Please visit his company's web site at www.ml-consult.co.uk for company and contact information.
© Copyright Mike Lewis Consultants Ltd.


Vyas Kumar R @ 12/8/2006 2:28:11 AM
Very good and ineresting. At the same time, informative.

Request: please write a method to send an email with attachments from VFP ?

Rashid Malik @ 4/22/2008 7:32:17 AM
Thanks Mike, It is very useful to use in our application.

Best Regards,

Naushin Shaikh @ 6/19/2008 4:03:53 AM
Thanks! It was very helpful.

ljh @ 7/3/2008 6:50:59 AM
I think you are champion i was looking for new line character for shellexecute for hours, finally you have got the solution listed.

Thanks a million and God bless you.


STAMEN STOYCHEV @ 10/15/2008 5:19:28 PM

STAMEN STOYCHEV @ 10/15/2008 5:20:09 PM

radha @ 2/11/2010 8:32:09 AM
i am working in garment industry in hr department i need one help from u we have 150 employees in our company they worked ovtim 20hrs per employee in a month now i want enter the ovtime pls help me i want give a command for individual employee to update the ovtim for present days only pls help me wat is the command

Priten Shah @ 9/19/2010 12:58:22 PM
Your Information is very useful

Scott Johnstone @ 9/27/2011 6:22:54 PM
Thanks for the info!
If the length of the 'mailto' link is over 2047 characters, what happens? Does it not run at all or will it trunkate the reset of the recipiets?

Jerry Curry @ 11/22/2012 12:10:08 AM
I have used faxing within VFP for years to send vfp reports to clients. Now, my client wants to sent the same report via email.
This would involve multiple clients to send a specific report. i.e. each client would have a different report.
Any ideas or thoughts?

Thanks in advance.

Aaron @ 2/3/2014 4:18:36 PM
Is there no way to do a new line (CHR(13),
, anything!) with the body field?

Your Name: 
Your Feedback: 

Spam Protection:
Enter the code shown: