Wiki Home

Send Mapi Email


Namespace: Wiki
Many have posted questions about MAPI & VFP. There are two methods of dealing with this issue. You can use Outlook Express or Outlook and this reference deals with Outlook Express. In fact, you can use any MAPI-compliant mail client program, such as Eudora, with this method. AOL probably supports it too; Anyone know about AOL, Juno, Pegasus or any other Email client's support for MAPI?

This example is based around a form that can act as a sending or a receiving operation. This form will need to have the ActiveX controls Session and Message identifed in the Controls section of VFP Options. See elsewhere on how to do that.

Your Init on the form will creat the object for the Session:
public lcCurdir
lcCurdir = sys(5) + curdir()

Thisform.Session1.signon    && turns on OE if it is not already open.

You will use the lcCurdir to come back to, because these ActiveX controls will change your location on you!

I use two push buttons to allow the user different activities

READ MAIL:
Thisform.Message1.sessionid = thisform.Session1.sessionid    && Session #
Thisform.Message1.fetchunreadonly = .t.    && Type of Mail
Thisform.Message1.fetch    && Get it

m = 0
ms1 = thisform.Message1.msgcount    && How many pieces id we received?

*-- run through all the pieces looking for an attachment. XXXX.new
For ms = 1 to ms1
  *Wait wind Thisform.Message1.msgorigaddress*Wait wind thisform.Message1.attachmentname
    lcfile = thisform.Message1.attachmentname
    cd \  && lcCurdir for instance
    if lower(right(lcfile,3))='new'
      wait wind nowait ' Installing a new version of the program '
      Copy file (lcfile) to c:\
    endif
  Endif
  m = m + 1
  If m <> ms1
    *-- change the message underneath
    Thisform.Message1.msgindex = m
  Endif
Next


Send mail with preset Text and an attachment:
lnSessionID = thisform.session1.SessionID
If lnSessionID > 0
  If vartype(thisform.message1) = "O"
  with Thisform.message1
    .SessionID = lnSessionID
    .Compose()
    .AttachmentIndex =	0
    .AddressResolveUI = .f.
    .MsgSubject = "New version of your program"
    .MsgNoteText = " Message Text here "
    .RecipAddress = 
  endwith

  If thisform.message1.MsgIndex = -1.
    If file(your file name here)
      Thisform.message1.AttachmentPathName = 
      Thisform.message1.AttachmentName = 
    Endif
  Endif
  Thisform.message1.Send()
  Wait window nowait "Message sent..."
Endif && vartype(thisform.message1) = "O"

Thisform.session1.SignOff()
Endif  && lnSessionID > 0
Return


This is very useful and goes farther than I have attempted yet. All I've tried to do is use MAPI to send messages from my applications. The trouble is that on some user's systems (who DO have Outlook or Outlook Express installed) MAPI doesn't work. (I'm still trying to get a better explanation of what DOES happen... it seems that an error window appears (not my ON ERROR routine) and displays the message it's trying to send... Hmmmm...Maybe it's the Send Email confirmation and a stupid user?). Is there a way to check if MAPI is going to work first? (I know, that's a big and unfocused question.... but, what SHOULD be checked before trying to use it?) - ?wgcs

For Outlook Express you must set it's option to be the Simple MAPI client. In OE4 this was a checkbox. In OE5 I can't find the equivalent, unless setting it as the "Default Mail Handler" also sets it as the MAPI client. -- ?df

This'll work locally, but what about each of our users? (basically, MAPI was so easy to implement (it worked on the first try in development) that we wanted to use it for bug reporting... if the application hits an error it doesn't expect, a standard error handler collects LIST STATUS/LIST MEMORY/Call stack information and emails back to us... however it sometimes doesn't work. How can it (the program/error handler) tell if MAPI's going to work before trying it? - ?wgcs

Tom Koch (MS MVP Outlook Express) has some interesing OE stuff on his webpage. -- Cindy Winegarden
This looks great for what I want, but my users are not using Outlook or OE. Just one that uses simple MAPI. And I can not find the ActiveX controls Session and Message talked about above. I found one called Messageview, but nothing for session. Can someone point me to a class with these in them, or where the controls may be hiding. And how to get them on to the form? I suspect that you have to build the form after checking those controls and saving them. - Beth Walsh
"MAPI Bad... SMTP Good" from Ted Roche's "Email Without Outlook" 2002 presentation at http://www.tedroche.com/Present/2002/emailwooutlook.html
That is all fine and dandy. But my clients are using a email system that uses simple MAPI and they will not change just because I ask them to. Thanks for the info anyway. - Beth Walsh


Regarding MAPI clients, don't bother trying with Lotus Notes 5 or later. They don't support extended MAPI and you should use their own API. -- Alan Bourke

I've written a series of entries on my blog regarding email and VFP that includes sample code. The first entry deals with MAPI: Email and VFP: Part 1a (MAPI) -- Craig SBoyd
See Also: Automated Email

Category Coding Category Code Samples Category ActiveX
( Topic last updated: 2005.07.13 03:28:58 PM )