Sending e-mail with CDO and SSL

Sending e-mail with CDO and SSL

Postby Rick Lipkin » Tue Mar 26, 2013 5:12 pm

To All Concerned

I found this thread in the FW Spanish forum and noticed in one of the links the use of SSL and CDO to send e-mail

viewtopic.php?f=6&t=13175

Within the thread was a link to a VIsual FoxPro forum which gives an example of using CDO and SSL..

http://www.portalfox.com/index.php?name ... me=Printer

I have not tried the above code, but I thought it was worth a look for those who use FWH to send e-mail thru their apps.

Let me know if anyone is successful using the FoxPro code.

Thanks
Rick Lipkin
User avatar
Rick Lipkin
 
Posts: 2666
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Sending e-mail with CDO and SSL

Postby benjiben143 » Wed Mar 27, 2013 4:57 pm

hi,,,

Code: Select all  Expand view  RUN

Function Config_eMail(cdest, cfiles )

  Local cUser,cPass,cRemt, cCC, cCCO, cTxt, cSubject
  local lRet := .f.
  local oCfg, oError
  local cServ :=alltrim(correo:smtp)     //                      aServs[nServ][2]  //--> SERVIDOR SMTP - "smtp.servidor.com.br"
  local nPort :=val(alltrim(correo:puerto))   //                         aServs[nServ][3]
  local lAut  :=.t.
  local lSSL  :=.f.             //                            aServs[nServ][4]
 
  public correo
  if cfiles=nil
     aAttach:={}
  else
     aAttach:=cfiles
  endif  
 
 
  cPass:=alltrim(correo:contra)
  cuser:=alltrim(correo:usuario)
  ctxt:= correo:MENSA  
  csubject:=correo:texto
  ccc:=""
  ccco:=""
  cRemt:=alltrim(correo:usuario)
 // cdest:="chino72vale@hotmail.com"   // ay que pasas esta informacion ok
  cCC:=""
  cCCO:=""
 // aAttach:=CFILES   // serian los archivos anexados we
 
  if Empty(cPass) .or. Empty(cRemt) .or. empty(cDest)
     ? "falta datos para mandar correo,,,, destino/ remitente / usuario checar "
     return .f.
  endif  
 
  TRY
    oCfg := CREATEOBJECT( "CDO.Configuration" )
      WITH OBJECT oCfg:Fields
           :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver"       ):Value := cServ
           :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport"   ):Value := nPort
           :Item( "http://schemas.microsoft.com/cdo/configuration/sendusing"        ):Value := 2
           :Item( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" ):Value := lAut
           :Item( "http://schemas.microsoft.com/cdo/configuration/smtpusessl"       ):Value := lSSL
           :Item( "http://schemas.microsoft.com/cdo/configuration/sendusername"     ):Value := cUser
           :Item( "http://schemas.microsoft.com/cdo/configuration/sendpassword"     ):Value := cPass
              :Update()
      END WITH
      lRet := .t.
  CATCH oError
    MsgInfo( "No se pudo enviar e-Mail!"  +CRLF+ ;
             "Error: "     + Transform(oError:GenCode,   nil) + ";" +CRLF+ ;
             "SubC: "      + Transform(oError:SubCode,   nil) + ";" +CRLF+ ;
             "OSCode: "    + Transform(oError:OsCode,    nil) + ";" +CRLF+ ;
             "SubSystem: " + Transform(oError:SubSystem, nil) + ";" +CRLF+ ;
             "Mensaje: "   + oError:Description, "Atención" )

  END
  //--> FIM DAS CONFIGURAÇOES.
  if lRet
     lRet := Envia_eMail(oCfg,cRemt,cDest, cCC, cCCO, cTxt, cSubject)   
  endif
   
Return lRet
 
********************************************************************************
Function Envia_eMail(oCfg,cFrom, cTo, cCC, cBCC, cMsg, cSubject)
 
  local cToken
  local lRet := .f. , Omsg , x, i, loBP
    
       TRY
         oMsg := CREATEOBJECT ( "CDO.Message" )
           WITH OBJECT oMsg
                :Configuration = oCfg
                :From = cFrom                
                :To = cTo
                :CC = cCC
                :BCC = cBCC
               
                :Subject = cSubject
                :TextBody = cMsg
               
               
                For x := 1 To Len( aAttach )
                    if aAttach[x] <> NIL
                   
                       :AddAttachment(AllTrim(aAttach[x]))
                    endif
                Next
           
                :HTMLBody = "<p><img src='cid:id_imagen10'></p>" +"<p>La imagen de arriba esta embebida en el mensaje.</p>"
                loBP:=:AddRelatedBodyPart("c:\faceleben\kasto2.bmp", "id_imagen10", 1)
                WITH OBJECT loBP:Fields
                    :Item("urn:schemas:mailheader:Content-ID") = "id_imagen10"
                    :Update()
                END WITH
                :CreateMHTMLBody("File://c:\faceleben\suave.html",0)
                :Send()
           END WITH
           lRet := .t.
       CATCH
      //     MsgInfo("No se pudo mandar la factura ")
           lRet := .f.
         END
 
   
Return lRet




 

**************************************************************
* Enviando emails *
* *
* Desenvolvedor: Ricardo de Moura Marques *
* email: ricardomouramarques@hotmail.com *
* *
* Agradecimentos ao Alessandro Seribeli Barreto - "Ale SB" *
* pelo código inicial, sem o qual, esse projeto *
* não seria possível *
* *
***************************************************************



sas
Benjamin Casarrubias Moreno
Cd. Obregon, Sonora, Mexico
chino72vale@hotmail.com
Fivewin 16.05, Harbour 3.2, xharbour 1.2.1, Fivelinux, visual estudio 2013
benjiben143
 
Posts: 116
Joined: Wed Mar 11, 2009 7:32 pm
Location: Obregon, Sonora, Mexico

Re: Sending e-mail with CDO and SSL *ERROR*

Postby TimStone » Thu Mar 28, 2013 7:59 pm

I am trying this out. Here is my code for the send function:

Code: Select all  Expand view  RUN


Function CDOSendMail( aTabMail )
    Local oEmailCfg,oEmailMsg

    TRY
       oEmailCfg := CREATEOBJECT( "CDO.Configuration" )
      WITH OBJECT  oEmailCfg:Fields
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ):Value :=   TRIM( aTabMail[01] ) //"mail.xxxxxxxx.com"
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ):Value :=  aTabMail[11] // 25
         :Item( "http://schemas.microsoft.com/cdo/configuration/sendusing" ):Value := 2   // Remote SMTP = 2, local = 1
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" ):Value :=  aTabMail[08] // .T.
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpusessl" ):Value :=  .T.
         :Item( "http://schemas.microsoft.com/cdo/configuration/sendusername" ):Value := TRIM( aTabMail[09] ) //  "xxanser@xxxxxxxx.com"
         :Item( "http://schemas.microsoft.com/cdo/configuration/sendpassword" ):Value := TRIM( aTabMail[10] ) // "xxxxxx"
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"):Value := 30
         :Update()
      END WITH
    CATCH oError
      MsgInfo( "Could not create message configuration" + ";"  + ;
             "Error: " + TRANSFORM(oError:GenCode, NIL) + ";" + ;
             "SubC: " + TRANSFORM(oError:SubCode, NIL) + ";" + ;
             "OSCode: " + TRANSFORM(oError:OsCode, NIL) + ";" + ;
             "SubSystem: " + TRANSFORM(oError:SubSystem, NIL) + ";" + ;
             "Message: " + oError:Description )
       Return .F.
    END
    oError:=NIL

    TRY
     oEmailMsg := CREATEOBJECT ( "CDO.Message" )
     WITH OBJECT oEmailMsg
        :Configuration =  oEmailCfg
        :From = aTabMail[02] //chr(34)+" Anser K.K. "+chr(34)+ "<anser@xxxxxxxx.com>" // This will be displayed in the From (The email id does not appear)
        :To = TRIM( aTabMail[03] ) // "xxanserkk@xxxxx.com"    // <-----   Place the TO email address
        :Subject = aTabMail[04] // "This is a Tst message"
        //:ReplyTo =  aTabMail[02] //"xxanser@xxxxxxxxx.com"
        //:Sender =  aTabMail[02] //"xxanser@xxxxxxxxx.com"  // Read Receipt message is send to this
        //:Organization =  TRIM( aTabMail[02] ) // "My xxxxxx Company"    // "My Company Name"
        :MDNRequested = .T.
        :HTMLBody =  "<HTML> " + TRIM( aTabMail[05] ) + " </HTML>"
        IF LEN( aTabMail[06] ) > 0
            For nEle := 1 To Len( aTabMail[06] )
             :AddAttachment( ALLTRIM(aTabMail[06][nEle] )) // := AllTrim( aAttach[ nEle ] )
            Next
        ENDIF
        :Send()
     END WITH
     SysRefresh()
    CATCH oError
       
       MsgInfo( "Could not send message" + ";"  + CRLF+ ;
         "Error: " + TRANSFORM(oError:GenCode, NIL) + ";" + CRLF+;
         "SubC: " + TRANSFORM(oError:SubCode, NIL) + ";" + CRLF+ ;
         "OSCode: " + TRANSFORM(oError:OsCode, NIL) + ";" + CRLF +;
         "SubSystem: " + TRANSFORM(oError:SubSystem, NIL) + ";" +CRLF+ ;
         "Message: " + oError:Description )
       Return .F.
   
    END

Return( .t. )

 


On my non-SSL service, it works fine. However, when on an email account requiring SSL, we see the following error from the Send( )

DISP_E_MEMBERNOTFOUND

I've tried a websearch and found there was a thread on this in the Five Win forum, but what worked for someone else didn't fix it here. The values in the TabMail array are accurate.

I hope someone can succeed with this problem.

Tim
Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
User avatar
TimStone
 
Posts: 2950
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: Sending e-mail with CDO and SSL

Postby benjiben143 » Thu Mar 28, 2013 8:20 pm

hi, tim

Code: Select all  Expand view  RUN

 ' Configura las opciones para el login en el SMTP  
    If Usar_Autentificacion Then  
 
    '
Id de usuario del servidor Smtp ( en el caso de gmail, debe ser la dirección de correro _  
     mas el @gmail.com )  
    Obj_Email.Configuration.Fields.Item _  
        ("http://schemas.microsoft.com/cdo/configuration/sendusername") = Usuario  
 
    ' Password de la cuenta  
    Obj_Email.Configuration.Fields.Item _  
        ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = Password  
 
    '
Indica si se usa SSL para el envío. En el caso de Gmail requiere que esté en True  
    Obj_Email.Configuration.Fields.Item _  
        ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = Usar_SSL  
       
    End If  
       
 

 


http://www.recursosvisualbasic.com.ar/htm/trucos-codigofuente-visual-basic/337-enviar-correo-en-vb-con-microsoft-cdo.htm



sas
Benjamin Casarrubias Moreno
Cd. Obregon, Sonora, Mexico
chino72vale@hotmail.com
Fivewin 16.05, Harbour 3.2, xharbour 1.2.1, Fivelinux, visual estudio 2013
benjiben143
 
Posts: 116
Joined: Wed Mar 11, 2009 7:32 pm
Location: Obregon, Sonora, Mexico

Re: Sending e-mail with CDO and SSL

Postby TimStone » Thu Mar 28, 2013 9:43 pm

In the code I supplied, all three of those items are referenced properly.

Tim
Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
User avatar
TimStone
 
Posts: 2950
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: Sending e-mail with CDO and SSL

Postby James Bott » Sun Mar 31, 2013 7:18 pm

Tim,

Are you using the code supplied here?

viewtopic.php?f=3&t=15843&p=82081&hilit=gmail#p82081

Have you checked to see if your firewall is blocking the port?

Have you tried increasing the timeout value?

Regards,
James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Sending e-mail with CDO and SSL

Postby TimStone » Mon Apr 01, 2013 7:44 pm

James,

Yes, that is where the code is from.

The settings are identical to Outlook, which is working, so the port is not blocked.

The timeout value is set to 30 but the error reports back much more quickly.

The question is, what element returns that error on :Send( )

Tim
Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
User avatar
TimStone
 
Posts: 2950
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: Sending e-mail with CDO and SSL

Postby James Bott » Mon Apr 01, 2013 9:59 pm

Tim,

I sent a test program to your email. Please let us know if it works for you.

Regards,
James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Sending e-mail with CDO and SSL

Postby James Bott » Mon Apr 01, 2013 11:09 pm

Tim,

>The settings are identical to Outlook, which is working, so the port is not blocked.

I think this is where the problem may be. You MUST use port 465. Is that the port the Outlook is using? Is that the port your sample code is using? If not, try port 465.

I tried port 25 and it errors out, but port 465 works fine.

Regards,
James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Sending e-mail with CDO and SSL

Postby James Bott » Tue Apr 02, 2013 12:15 am

Tim,

May I suggest using a mail object rather than an array--it would be much easier to read.

Instead of:

aTabMail[02]
aTabMail[03]
aTabMail[04]

You would have:

oTabMail:From
oTabMail:To
oTabMail:Subject

It is so much easier to work with.

Regards,
James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Sending e-mail with CDO and SSL

Postby James Bott » Wed Apr 03, 2013 3:40 pm

See this thread for a solution:

viewtopic.php?f=3&t=26019

James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Sending e-mail with CDO and SSL

Postby reinaldocrespo » Wed Apr 03, 2013 7:21 pm

To send emails using ssl, at least from xharbour, you must link the open ssl lib: tipssl.lib

On your app directory you need to have these .dlls present:
libeay32.dll
ssleay32.dll

There are a few threads on the xharbour forum explaining how to build the .libs and where to download the .dlls from.

A simpler way to test if you have the open ssl libs linked, is trying to http to an https address. Once everything is in place, you can use xharbour's native emailing funcs.

Hope that helps,


Reinaldo.
User avatar
reinaldocrespo
 
Posts: 979
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: Sending e-mail with CDO and SSL

Postby James Bott » Wed Apr 03, 2013 8:28 pm

reinaldocrespo wrote:There are a few threads on the xharbour forum explaining how to build the .libs and where to download the .dlls from.

A simpler way to test if you have the open ssl libs linked, is trying to http to an https address. Once everything is in place, you can use xharbour's native emailing funcs.


Can you provide a link to the location of the information and DLLs you mentioned?

I seem to remember, when I looked into this before, that you had to purchase some kind of security certificate? I expect this would be for each user?

I'm not sure how you can use FW's native email classes since they don't support SLL thus there are no native settings for SSL. At the least it would seem that you would have to subclass the TSMTP class to add the SLL settings.

Regards,
James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Sending e-mail with CDO and SSL

Postby reinaldocrespo » Wed Apr 03, 2013 8:55 pm

download open ssl from:
[url]
http://slproweb.com/download/Win32OpenSSL-0_9_8y.exe
http://www.microsoft.com/downloads/deta ... 4B9F2BC1BF
[/url]

Open ssl is an open source project. Nothing to purchase. The web server wishing to have an https address, needs to purchase a security certificate. Not so the user that connectes to it. Email server requiring ssl authentication, must have purchased their security certificate. Otherwise, they could not be offering/requiring ssl connections.

FW TSMTP native email does not send using ssl. You would have to change the class. To send emails using a server that requres ssl authentication, you would have to use xharbour native emaling funcs that inherit from xharbour's Tip class + have the ssl libs linked in. Again, there are a few users on the xharbour's ng that know a lot more than I do. I was helped by Luiz Rafael Culik from Brazil. It took me a while to understand, but since it has been working, I haven't looked at that again. It just works.



Reinaldo.
User avatar
reinaldocrespo
 
Posts: 979
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: Sending e-mail with CDO and SSL

Postby Rick Lipkin » Thu Apr 04, 2013 1:26 pm

Tim and friends ..

Since I started this thread .. I wanted to make sure that we end it on the solution ..

Tim .. I cut and pasted this solution from your ( other ) thread that spawned from this one .. Please verify this is your solution to implementing CDO and SSl ..

Many thanks!
Rick Lipkin

Code: Select all  Expand view  RUN

Function CDOSendMail( aTabMail )
    Local oEmailCfg,oEmailMsg

/* Tabmail
01 = MAILSERVER  // A correct mail server address
02 = MAILFROM  // A valid originator of the message
03 = MAILTO  // Who it is being sent to
04 = SUBJECT  // The subect
05 = BODY  // The body in text format
06 = ATTACHMENT  // Attachment(s)
07 = BCC  // Usually NIL  NOT USED
08 = LAUTHORIZATION  // T or F logical for authentication
09 = USERID pour MAILSERVER  // The mail server Username
10 = PW     pour MAILSERVER  // The mail server password
11 = Mail port   defaut = 25  // The port, default is 25, set to 465 for SSL
12 = Mail HTML page  // An HTML page, usually not used
13 = ssl authentification  // T or F logical for SSL
14 = Mail sender  // Mail sender name  NOT USED
15 = Organisation sender  // Organization sender  NOT USED
16 = Host  // Host if needed  NOT USED
*/

    TRY
       oEmailCfg := CREATEOBJECT( "CDO.Configuration" )
      WITH OBJECT  oEmailCfg:Fields
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ):Value :=   TRIM( aTabMail[01] ) //"mail.xxxxxxxx.com"
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ):Value :=  aTabMail[11] // 25
         :Item( "http://schemas.microsoft.com/cdo/configuration/sendusing" ):Value := 2   // Remote SMTP = 2, local = 1
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" ):Value :=  aTabMail[08] // .T.
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpusessl" ):Value :=  aTabMail[13]
         :Item( "http://schemas.microsoft.com/cdo/configuration/sendusername" ):Value := TRIM( aTabMail[09] ) //  "xxanser@xxxxxxxx.com"
         :Item( "http://schemas.microsoft.com/cdo/configuration/sendpassword" ):Value := TRIM( aTabMail[10] ) // "xxxxxx"
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"):Value := 30
         :Update()
      END WITH
    CATCH oError
      MsgInfo( "Could not create message configuration" + ";"  + ;
             "Error: " + TRANSFORM(oError:GenCode, NIL) + ";" + ;
             "SubC: " + TRANSFORM(oError:SubCode, NIL) + ";" + ;
             "OSCode: " + TRANSFORM(oError:OsCode, NIL) + ";" + ;
             "SubSystem: " + TRANSFORM(oError:SubSystem, NIL) + ";" + ;
             "Message: " + oError:Description )
       Return .F.
    END
    oError:=NIL

    TRY
     oEmailMsg := CREATEOBJECT ( "CDO.Message" )
     WITH OBJECT oEmailMsg
        :Configuration =  oEmailCfg
        :From = aTabMail[02] //chr(34)+" Anser K.K. "+chr(34)+ "<anser@xxxxxxxx.com>" // This will be displayed in the From (The email id does not appear)
        :To = TRIM( aTabMail[03] ) // "xxanserkk@xxxxx.com"    // <-----   Place the TO email address
        :Subject = aTabMail[04] // "This is a Tst message"
        :MDNRequested = .T.
        :TextBody =  TRIM( aTabMail[05] )
        IF LEN( aTabMail[06] ) > 0
            For nEle := 1 To Len( aTabMail[06] )
             :AddAttachment( ALLTRIM(aTabMail[06][nEle] )) // := AllTrim( aAttach[ nEle ] )
            Next
        ENDIF
     END WITH
     oEmailMsg:Send( )
    CATCH oError
       
       MsgInfo( "Could not send message" + ";"  + CRLF+ ;
         "Error: " + TRANSFORM(oError:GenCode, NIL) + ";" + CRLF+;
         "SubC: " + TRANSFORM(oError:SubCode, NIL) + ";" + CRLF+ ;
         "OSCode: " + TRANSFORM(oError:OsCode, NIL) + ";" + CRLF +;
         "SubSystem: " + TRANSFORM(oError:SubSystem, NIL) + ";" +CRLF+ ;
         "Message: " + oError:Description )
       Return .F.
   
    END

Return( .t. )
 
User avatar
Rick Lipkin
 
Posts: 2666
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 80 guests