As we’ve seen in the previous entries, emailing from VFP can be done in a variety of ways.  In this entry, I explore the use of a freely avaliable ActiveX control, simply known as EsSmtp.  This control was once sold commercially by Eurosource, but now resides on SourceForge.  It’s not only free, it’s open source (you get the source code). There’s quite a few controls available, but the only one I am currently familiar with is EsSmtp.


You’ll need to download and register this control in order for this example to work.  You can Download it here.  And, additional information about the properties and methods of the control can be found here.


Okay, without taking up anymore precious time, here’s the code.







*******************************
*!* Example of using SendViaEsSmtp
*******************************
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 lcFromName, lcFromAddress, lcTo, lcSubject, lcBody, lcCC, lcBCC, lcSMTPServer, lcErrReturn


lcFromName = “My Name”
lcFromAddress = “
me@myhost.com
lcTo = “
someone@theirhost.com
lcSubject = “Hey Have You Tried VFP Email?”
lcBody = “Just wanted to let you know that VFP is pretty versatile and has a lot of ways to send email.”
lcCC = “
someoneelse@someotherhost.com
lcBCC = “
myboss@bosshost.com
lcSMTPServer = “mail.myhost.com”


SendViaEsSmtp(@lcErrReturn, lcFromName, lcFromAddress, lcTo, lcSubject, lcBody, @aryAttach, lcCC, lcBCC, lcSMTPServer)


IF EMPTY(lcErrReturn)
 MESSAGEBOX(“‘” + lcSubject + “‘ sent successfullly.”, 64, “Send email via EsSmtp”)
ELSE
 MESSAGEBOX(“‘” + lcSubject + “‘ failed to be sent.  Reason:” + CHR(13) + lcErrReturn, 64, “Send email via EsSmtp”)
ENDIF


*******************************************
PROCEDURE SendViaEsSmtp(tcReturn, tcFromName, tcFromAddress, tcTo, tcSubject, tcBody, taFiles, tcCC, tcBCC, tcSMTPSever)
*******************************************
 LOCAL loEsSmtp, lnCountAttachments, lnErrorNo
 TRY
  loEsSmtp = CREATEOBJECT(“ESSMTP.EsSmtpCtrl.1”)
  WITH loEsSmtp
   IF TYPE(“tcSMTPSever”) = “C”
    .SMTPServer = tcSMTPSever
   ENDIF   
   IF TYPE(“tcFromName”) = “C”
    .SourceName = tcFromName
   ENDIF
   IF TYPE(“tcFromAddress”) = “C”
    .SourceAddress = tcFromAddress
   ENDIF
   .DestinationAddress = tcTo
   IF TYPE(“tcCC”) = “C”
    .CCDestinationAddress = tcCC
   ENDIF
   IF TYPE(“tcBCC”) = “C”
    .BCCDestinationAddress = tcBCC
   ENDIF
   .Subject = tcSubject
   .MailData = tcBody
   IF TYPE(“taFiles”, 1) = “A”
    FOR lnCountAttachments = 1 TO ALEN(taFiles)
     .AddAttachment(taFiles(lnCountAttachments), 0) && 0 signifies base64 encoding when needed
    ENDFOR
   ENDIF
   IF .SendMail() != 1 && there was a problem
    lnErrorNo = .ErrorNo
    DO CASE
     CASE lnErrorNo = 421
      THROW “Service not available, closing transmission channel”
     CASE lnErrorNo = 450
      THROW “Requested mail action not taken: mailbox unavailable [E.g., mailbox busy]”
     CASE lnErrorNo = 451
      THROW “Requested action aborted: local error in processing”
     CASE lnErrorNo = 452
      THROW “Requested action not taken: insufficient system storage”
     CASE lnErrorNo = 500
      THROW “Syntax error, command unrecognized [This may include errors such as command line too long]”
     CASE lnErrorNo = 501
      THROW “Syntax error in parameters or arguments”
     CASE lnErrorNo = 502
      THROW “Command not implemented”
     CASE lnErrorNo = 503
      THROW “Bad sequence of commands”
     CASE lnErrorNo = 504
      THROW “Command parameter not implemented”
     CASE lnErrorNo = 550
      THROW “Requested action not taken: mailbox unavailable [E.g., mailbox not found, no access]”
     CASE lnErrorNo = 552
      THROW “Requested mail action aborted: exceeded storage allocation”
     CASE lnErrorNo = 553
      THROW “Requested action not taken: mailbox name not allowed [E.g., mailbox syntax incorrect]”
     CASE lnErrorNo = 554
      THROW “Transaction failed”
     OTHERWISE
      THROW “Unknown Error – Might be WinSock Related”
     ENDCASE
   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 loEsSmtp
  loEsSmtp = .NULL.
 ENDTRY
ENDPROC