Search Amazon:

Send E-Mail & File Attachments Using MAPI

Using MAPI (Messaging Application Program Interface) you can easily mail enable your applications. This sample shows how to send e-mail to multiple recipients. You can address the e-mail TO a recipient, CC (Carbon Copy) or BCC (Blind Carbon Copy) recipients. You can also include one or more file attachments.

Send E-Mail that includes File Attachments Using MAPI
Download Source Code

Overview

VB includes the MAPISession and MAPIMessage controls so you can mail enable your applications. This sample, however, goes straight to the API and uses the MAPI commands directly.

The first step is to verify that the PC your app is running on has MAPI installed. In most cases it will. This sample looks at the Win.ini file and reads the MAPI value found in the [MAIL] section. Reading this is straight forward using the GetPrivateProfileString API. If MAPI=1, then MAPI is installed.

I looked in the Win.Ini file because it still exists in Windows 2000 and XP and I only had a few minutes to put this sample together. By right, you should read the Registry at HKLM\Software\Microsoft\Windows Messaging Subsystem\MAPI. Again, if MAPI="1", MAPI is installed.

Sending Mail

The sample program is very simple. It allows you to enter a few recipients, a subject, text for the body of the e-mail and the path for two files to attach. When you click the Send button the program populates a few MAPI structures, establishes a MAPI session then sends the e-mail.

The first structure to fill is the MapiRecip UDT. You must populate one of these structures for each e-mail recipient. Based on the number of recipients you enter into the program's main form, an array of this type is created. For each recipient a few pieces of information are set. The Address is the recipient's actual email address. To display a more user friendly name in the e-mail instead of the actual address, the Name field is used. Lastly, the RecipClass field specifies if the recipient is a TO, CC or BCC recipient.

The next structure is the MapiFile UDT. Similarly, you must populate one of these structures for each e-mail attachment. Based on the number of files indicated on the program's main form, an array of this type is created. For each file the UDT's Path, user friendly FileName and Position values are set.

To send attachments via MAPI, you must specify a position for the attachment in the message body. Therefore, the body must not be empty. The attachment replaces the character at the specified location in the message text.

The last structure is the MAPIMessage UDT which pulls everything together. This is where you specify the number of recipients in the MapiRecip array, the number of files in the MapiFile array and the text for the e-mail's subject and body.

All that remains is to send the message. A call to MAPILogon is made to establish a MAPI session and return its ID. To send the message, MAPISendMail is invoked. This command uses the session ID and addresses of the recipient and file arrays as parameters. To send the mail without any dialogs, pass a zero in the sixth or Flags parameter. To display the e-mail prior to sending it, use MAPI_DIALOG instead.

That's it. Just call MAPILogoff to terminate the MAPI session and you are done.

Instructions

Download this project and run it. Enter a few recipients with and without display names. Do the same for file attachments. Type in your subject and body then click Send.

Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Comments: 2 Items to Show:     

     
Id:  2 Posted:  8/5/2008 12:47:42 PM By:  chewee
Digging around on MSDN I found this page (http://msdn.microsoft.com/en-us/library/ms529097 (EXCHG.10).aspx) which says that the Address property of the MapiRecip needs to be prefixed with SMTP:

Change line 280 from:
aryRecipient(lngIndex).Address = Trim$(txtRecipient(l).Text)

to this:
aryRecipient(lngIndex).Address = "SMTP:" & Trim$(txtRecipient(l).Text)

and it works.

Thanks for the great sample code!
Id:  1 Posted:  8/4/2008 3:09:45 PM By:  chewee
I've tried this code and I'm not able to actually send any messages.  I always end up with this error which comes back in a separate email from Outlook's System Administrator:
The message could not be delivered because the recipient's destination email system is unknown or invalid. Please check the address and try again, or contact your system administrator to verify connectivity to the email system of the recipient.

Any ideas?

Thanks in advance.
 
You must log in to post feedback.
Comment:    
 

If you use this code, please mention "www.TheScarms.com"

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms