With help from Robert Smith [see below], sometime ago I wrote a VB routine to send e-mail using the Shell API. This month's tip shows how to use the Shell API to send e-mail. I wrote an e-mail demo program to show how to use Shell.
2002/05/23 - The Shell API cannot process strings longer than about 2000 bytes.
If you are going to send very long messages then you should not use this technique.
See: Other EMail Links below.
The demo program is a 'simple' e-mail system. The VB6 demo screen:
Fields and buttons on the demo program:
Name - Optional - Enter a name in the name field. The name appears in the salutation of the message.
E-Mail - Enter the e-mail address for the associated name.
Add Address - Click this button to add the name to the 'Address Book'.
Address Book - The address book is stored in the registry when the demo ends.
To - Names copied to the 'to' box appear in the 'to' list of the mail agent.
cc - Names copied to the 'cc' box appear in the 'copy to' list of the mail agent.
bcc - Names copied to the 'bcc' box appear in the 'blind copy to' list of the mail agent.
Subject - Enter the message's subject.
Attachments - Enter attachments by browsing for the attachment using the 'find' button. Does not work with Outlook Express. See below for Michael Cole's suggestion for attaching files with Outlook Express.
Body - Enter the text of the message.
Signature - Enter a signature to appear as the last entry in the body of the message. Default is network user name.
Send E-Mail using Shell - Click this button to send the e-mail data to the mail agent.
Send E-Mail using MAPI - A future tip.
Double-click the name in the address book to send the name to the 'hot' box. The hot 'to' box has a 'red' label. Click on a 'to' box to make it 'hot'. Or you can drag the address to the desired box. Use the 'move' buttons to move the selected entry out of the box. Use the delete button to delete an address book entry.
I have tested the program with Outlook Express. Note that Outlook Express does not support sending attachments. The sample Outlook Express screen created when the 'Send' button is clicked:
The code is being used in a commercial application. It is known to work with Outlook. Some of the code originated with Robert Smith, e-mail: firstname.lastname@example.org, web site: http://www.smithvoice.com/vbfun.htm.
Information is passed from the application to the e-mail routines in a 'udtEMail' structure [user defined type]. The structure:
Private Type udtEMail iNameCount As Integer This many names aryName() As String An array of names. iToCount As Integer This many 'To' addresses aryTo() as String An array of 'To' addresses iCCCount As Integer This many copy to addresses aryCC() As String An array of copy to addresses iBCCount As Integer This many blind copy to addresses aryBC() As String An array of blind copy to addresses sSubject As String The message's subject sBody As String, or
sText As String
The message text. Some versions of the
UDT use sBody and others use sText
iAttachmentCount As Integer The number of attachments aryAttachment() As String An array of attachments sSignature The message's signature End Type
The Demo Program Uses These Modules
Module - Click for More Info Description Used in the Demo To modEMailUsingShell.bas Send E-Mail Using Shell Code that sends e-mail.
Include in your application.
modConnectToWebSite.bas Connect to a web site Show this info when Help/Info is
selected from the demo.
Browse for file by Francesco Balena Browse for attachments. modLocationLocationLocation.bas Form Housekeeping Set form size and location.
Save form information in the registry.
TBRegistry.bas Read and Write to the Registry Save e-mail address book in registry.
Also used for form housekeeping.
clsNetUserName.cls Get the Network User and
Get the 'signature'. modAddBackSlash.bas Add a Backslash if Needed Add a backslash to App.Path.
HTMLBody Rather Than Body
2003/02/17 - Phil Weber pointed out that you can use property HTMLBody rather than Body to send 'rich text'. I have not tried it but, knowing Phil, I'm sure it works. You would need to modify the demo program to use this property.
2003/08/09 - Michael Cole [email@example.com] reports that:
You report that "Attachments - Enter attachments by browsing for the attachment using the 'find' button. Does not work with Outlook Express."
Whilst this is undocumented, (given that attachments are not part of the RFC 2368), for Outlook Express, the syntax is:
Download VB 6 Source Code
Click here to download the VB 6 source code [34k]. 2003/08/09 - Minor change made to the source code for e-mail address validation in the demo program - suggested by Michael Cole.
Previous Tip Tip Index Next Tip