# Tuesday, July 12, 2005

If you can count on your users using Outlook for their email client (as opposed to Eudora or whatever), then the following code can come in handy.  Sending email via Outlook Automation gives us access to a number of features that come stock with the MailItem object.  I've touched on a few below, but for more information refer to the MailItem Object information out on the MSDN.

*******************************
*!* Example of using SendViaOutlook
*******************************
#DEFINE olImportanceLow 0
#DEFINE olImportanceNormal 1
#DEFINE olImportanceHigh 2

DIMENSION aryAttach(2)
aryAttach(1) = "C:\attachment1.txt" && change to an actual file that exists on your computer
aryAttach(2) = "C:\attachment2.zip" && change to an actual file that exists on your computer

LOCAL lcTo, lcSubject, lcBody, lcCC, lcBCC, llHTMLFormat, llOpenEmail, lcErrReturn

lcTo = "someone@sommehost.com"
lcSubject = "Hey Have You Tried VFP Email?"
*!* Sending the body in HTML format
llHTMLFormat = .T.
lcBody = "<a href='http://www.sweetpotatosoftware.com/SPSBlog/default.aspx'>" + ;
 "Hey Have You Tried VFP Email?" + ;
 "</a>"
lcCC = "
someoneelse@anotherhost.com"
lcBCC = "
myboss@bosshost.com"

*!* to automatically send email set llOpenEmail to .F.
llOpenEmail = .T. && Whether email is opened in Outlook or not

SendViaOutlook(@lcErrReturn, lcTo, lcSubject, lcBody, @aryAttach, lcCC, lcBCC, llHTMLFormat, olImportanceHigh, llOpenEmail)

IF EMPTY(lcErrReturn)
 MESSAGEBOX("'" + lcSubject + "'" + IIF(llOpenEmail, " opened ", " sent ") + "successfullly.", 64, "Send email via Outlook")
ELSE
 MESSAGEBOX("'" + lcSubject + "' failed to be sent.  Reason:" + CHR(13) + lcErrReturn, 64, "Send email via Outlook")
ENDIF

*******************************************
PROCEDURE SendViaOutlook(tcReturn, tcTo, tcSubject, tcBody, taFiles, tcCC, tcBCC, tlHTMLFormat, tnImportance, tlOpenEmail)
 *******************************************
 LOCAL loOutlook, loItem, lnCountAttachments, loMapi
 TRY
  loOutlook = CREATEOBJECT("outlook.application")
  loMapi = loOutLook.GetNameSpace("MAPI")
  loMapi.Logon()
  loItem = loOutlook.CreateItem(0)
  WITH loItem
   .Subject = tcSubject
   .TO = tcTo
   IF tlHTMLFormat
    .HTMLBody = tcBody
   ELSE
    .Body = tcBody
   ENDIF
   IF TYPE("tcCC") = "C"
    .CC = tcCC
   ENDIF
   IF TYPE("tcBCC") = "C"
    .BCC = tcBCC
   ENDIF
   IF TYPE("tnImportance") != "N"
    tnImportance = 1 && normal importance
   ENDIF
   .Importance = tnImportance
   IF TYPE("tafiles",1) = "A"
    FOR lnCountAttachments = 1 TO ALEN(taFiles)
     .Attachments.ADD(taFiles(lnCountAttachments))
    ENDFOR
   ENDIF
   IF tlOpenEmail
    .DISPLAY()
   ELSE
    .SEND()
   ENDIF
  ENDWITH
 CATCH TO loError
  tcReturn = [Error: ] + STR(loError.ERRORNO) + CHR(13) + ;
   [LineNo: ] + STR(loError.LINENO) + CHR(13) + ;
   [Message: ] + loError.MESSAGE + CHR(13) + ;
   [Procedure: ] + loError.PROCEDURE + CHR(13) + ;
   [Details: ] + loError.DETAILS + CHR(13) + ;
   [StackLevel: ] + STR(loError.STACKLEVEL) + CHR(13) + ;
   [LineContents: ] + loError.LINECONTENTS
 FINALLY
  RELEASE oOutlook, oItem
  STORE .NULL. TO oOutlook, oItem
 ENDTRY
ENDPROC

UPDATES
07/12/2005 - changed attachment names, changed PCOUNT() > 4 to use Type() instead, removed unused lnCount variable, centered table containing code, fixed lcBody variable assignment that had gotten butchered when I switched into HTML mode while posting blog entry, changed title so subject matter could be easily discerned

02/10/2006 - added lomapi.logon() code

Tuesday, July 12, 2005 9:05:44 PM (GMT Daylight Time, UTC+01:00)  #    Comments [3]
Monday, November 21, 2005 2:21:12 AM (GMT Standard Time, UTC+00:00)
Thank for articals. It is helpfull.
-------------------------------------
When i try to send mail with above solution, i have some idea, i want to send an email with attachment from vfp application to outlook express. after that the outlook express display and i click on the send/receip to send the mail.
help me soon.
thanks
Huynh Dang Thai
Wednesday, January 25, 2006 2:23:21 PM (GMT Standard Time, UTC+00:00)
When I try to send e-mails with this program I get a message from the Outlook that says:

A program is trying to automatically send e-mail on your behalf.
Do you want to allow this?
If this is unexpected, it may be a virus and you should choose “No”

Is there a way to avoid this?
Also I am trying to send an automatic e-mail every morning to the sales people in my behalf, but this e-mails will be sent from another computer where they run my software.
Is there any way to set the “From” when you send the e-mail so the return address is from me instead of the person using that specific computer.

Please Advice.
Thank you.

William C.
Saturday, January 06, 2007 11:00:11 PM (GMT Standard Time, UTC+00:00)
Works fine. And so happy not to use one of those multiple versions of CDO
Jacques
All comments require the approval of the site owner before being displayed.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, sup, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview

 

Archive

<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789