Page 1 of 2
Sending e-mail with CDO and SSL
Posted: Tue Mar 26, 2013 5:12 pm
by Rick Lipkin
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=13175Within 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=PrinterI 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
Re: Sending e-mail with CDO and SSL
Posted: Wed Mar 27, 2013 4:57 pm
by benjiben143
hi,,,
Code: Select all | Expand
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
Re: Sending e-mail with CDO and SSL *ERROR*
Posted: Thu Mar 28, 2013 7:59 pm
by TimStone
I am trying this out. Here is my code for the send function:
Code: Select all | Expand
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
Re: Sending e-mail with CDO and SSL
Posted: Thu Mar 28, 2013 8:20 pm
by benjiben143
hi, tim
Code: Select all | Expand
' 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
Re: Sending e-mail with CDO and SSL
Posted: Thu Mar 28, 2013 9:43 pm
by TimStone
In the code I supplied, all three of those items are referenced properly.
Tim
Re: Sending e-mail with CDO and SSL
Posted: Sun Mar 31, 2013 7:18 pm
by James Bott
Tim,
Are you using the code supplied here?
viewtopic.php?f=3&t=15843&p=82081&hilit=gmail#p82081Have you checked to see if your firewall is blocking the port?
Have you tried increasing the timeout value?
Regards,
James
Re: Sending e-mail with CDO and SSL
Posted: Mon Apr 01, 2013 7:44 pm
by TimStone
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
Re: Sending e-mail with CDO and SSL
Posted: Mon Apr 01, 2013 9:59 pm
by James Bott
Tim,
I sent a test program to your email. Please let us know if it works for you.
Regards,
James
Re: Sending e-mail with CDO and SSL
Posted: Mon Apr 01, 2013 11:09 pm
by James Bott
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
Re: Sending e-mail with CDO and SSL
Posted: Tue Apr 02, 2013 12:15 am
by James Bott
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
Re: Sending e-mail with CDO and SSL
Posted: Wed Apr 03, 2013 3:40 pm
by James Bott
See this thread for a solution:
viewtopic.php?f=3&t=26019James
Re: Sending e-mail with CDO and SSL
Posted: Wed Apr 03, 2013 7:21 pm
by reinaldocrespo
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.
Re: Sending e-mail with CDO and SSL
Posted: Wed Apr 03, 2013 8:28 pm
by James Bott
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
Re: Sending e-mail with CDO and SSL
Posted: Wed Apr 03, 2013 8:55 pm
by reinaldocrespo
download open ssl from:
[url]
http://slproweb.com/download/Win32OpenSSL-0_9_8y.exehttp://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.
Re: Sending e-mail with CDO and SSL
Posted: Thu Apr 04, 2013 1:26 pm
by Rick Lipkin
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
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. )