# Monday, July 18, 2005

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

Monday, July 18, 2005 10:24:02 AM (GMT Daylight Time, UTC+01:00)  #    Comments [3]
Monday, September 19, 2005 8:05:09 PM (GMT Daylight Time, UTC+01:00)
I did not work! Error 1230 line 71 too many argument!
if Type("tafiles",1) = 'A'
procedure senviaMapi!
oscar
Saturday, October 01, 2005 10:08:39 AM (GMT Daylight Time, UTC+01:00)
Hi Oscar,

I just re-ran the posted code and it works as expected. Can you post the code that is giving you that error? If you aren't sending any attachments then you will need to revamp the code slightly... it is just an example that developers would need to expand on or modify in order to fit with the given needs of their applications. However, that having been said, if there is a bug in the code above I would appreciate seeing the code that will reproduce it... be sure to scrub your email addresses and other sensitive information before posting it or if you prefer you can email it to me directly and I will take a look. Thanks for reporting this and I look forward to seeing the code.
Wednesday, March 22, 2006 3:41:08 AM (GMT Standard Time, UTC+00:00)
Hi, with this control i can send html format, bye

(i´m sorry my english)
Jorge
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

<April 2014>
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910