Send E-Mail Using Shell - September 2001

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 E-Mail Demo

The demo program is a 'simple' e-mail system. The VB6 demo screen:

frmEMail

Using the Demo Program

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.

E-Mail Agent

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:

Outlook Express

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: smith@smithvoice.com, web site: http://www.smithvoice.com/vbfun.htm.

The Structure

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.
CDIALOG.CLS and
CDIALOG.BAS
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
Computer Name
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.

Undocumented Attachment for Outlook Express

2003/08/09 - Michael Cole [michael.cole@hansen.com] reports that:

Larry:

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:

   Att:filename=

rather than:

   Attach=

Regards,
Michael Cole

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

Home

Updated 2011/10/06