# Tuesday, July 12, 2005

CDOSYS sends the email directly to the SMTP server you designate.  Thus, it can come in handy when avoiding the Outlook security alert you may have noticed in my previous blog entries Email and VFP: Part 1a and Part 1b.  Special Note: CDOSYS (A.K.A CDO 2.0) first came out with the Windows 2000 OS.

*******************************
*!* Example of using SendViaCDOSYS
*******************************
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 lcFrom, lcTo, lcSubject, lcBody, lcCC, lcBCC, lcMailServer, lcUserName, lcPassword, llHTMLFormat, lcErrReturn

lcFrom = "someone@sommehost.com"
lcTo = "
someone@sommehost.com"
lcSubject = "Hey Have You Tried VFP Email?"
*!* Sending the body in HTML format
llHTMLFormat = .T. && change to .F. to send plain text message
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"
lcMailServer = "mail.myhost.com" && my SMTP Server
lcUserName = "
me@myhost.com" && my SMTP username
lcPassword = "My_PaSsWoRd" && my SMTP password

SendViaCDOSYS(@lcErrReturn, lcFrom, lcTo, lcSubject, lcBody, @aryAttach, lcCC, lcBCC, lcMailServer, lcUserName, lcPassword, llHTMLFormat)

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

*******************************************
PROCEDURE SendViaCDOSYS(tcReturn, tcFrom, tcTo, tcSubject, tcBody, taFiles, tcCC, tcBCC, tcMailServer, tcUserName, tcPassword, tlHTMLFormat)
*******************************************
 LOCAL lcSchema, loConfig, loMsg, loAtt, lnCountAttachments
 TRY
  lcSchema = "
http://schemas.microsoft.com/cdo/configuration/"

  loConfig = CREATEOBJECT("CDO.Configuration")

  WITH loConfig.FIELDS
   .ITEM(lcSchema + "smtpserverport") = 25 && SMTP Port
   .ITEM(lcSchema + "sendusing") = 2 && Send it using port
   .ITEM(lcSchema + "smtpserver") = tcMailServer && host of smtp server
   .ITEM(lcSchema + "smtpauthenticate") = 1 && Authenticate
   .ITEM(lcSchema + "sendusername") = tcUserName && Username
   .ITEM(lcSchema + "sendpassword") = tcPassword && Password
   .UPDATE
  ENDWITH

  loMsg = CREATEOBJECT ("CDO.Message")
  loMsg.Configuration = loConfig
  WITH loMsg
   .FROM = tcFrom
   .TO = tcTo
   IF TYPE("tcCC") = "C"
    .CC = tcCC
   ENDIF
   IF TYPE("tcBCC") = "C"
    .BCC = tcBCC
   ENDIF
   .Subject = tcSubject
   IF tlHTMLFormat
    .HTMLBody = tcBody
   ELSE
    .TextBody = tcBody
   ENDIF
   IF TYPE("tafiles",1) = "A"
    FOR lnCountAttachments = 1 TO ALEN(taFiles)
     loAtt=.AddAttachment(taFiles(lnCountAttachments))
    ENDFOR
   ENDIF
   .SEND()
  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 loConfig, loMsg
  STORE .NULL. TO loConfig, loMsg
 ENDTRY
ENDPROC

UPDATES
07-12-2005 Changed the title of this entry so it matched the previous ones and fixed lcBody assignment that got butchered when I changed to html mode before posting the entry, fixed attachment names, changed title so subject matter could be easily discerned

Tuesday, July 12, 2005 11:50:38 PM (GMT Daylight Time, UTC+01:00)  #    Comments [3]
Friday, May 19, 2006 6:28:12 PM (GMT Daylight Time, UTC+01:00)
I have been using COD.Message and COD.Configuration to send email from VFP for quite some time. It work quite well.

I have tried to set priority/importance, but have not been successful. Whatever command I try, I see normal priority in the mail received in Outlook. Has anyone been able to change priority using CDO.

Thanks.
Tuesday, July 24, 2007 9:18:38 PM (GMT Daylight Time, UTC+01:00)
Solo funciona si envío a una cuenta de correo del mismo smtp, cuando trato de enviar CC o BCC da error en el .send(). Necesito enviar a varias cuantas distintas y no logro que funcione, pero si lo hace bien cuando envio a una cuanta del mismo smtp que definido.
Espero que puedan indicarme como solucionar este problemita

Gracias
Mario
Thursday, December 27, 2007 12:48:57 PM (GMT Standard Time, UTC+00:00)
Thanks a Lot

Previously i am using the blat to send the email
it is not that much fast and it is not working in some of my client side.

And this provide a better alternative to it

Thanks!!! to sweetpotato

Muhammed Mohiudden M N
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

<August 2010>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234