by noe aburto » Fri May 29, 2015 1:42 am
funcion:
Function fSendMail( cMailServer, cFrom, cTo, cSubject, cBody, cAttach, cBCC, cUser, cPass, nPort, oDlg )
Local oCfg, oMsg, oError, nEle, cToken, bMens, lAuth:=!Empty(cUser).and.!Empty(cPass)
Local aAttach:={}
Default nPort := 25, ;
cSubject := "", ;
cBody := ""
if !IsInternet()
aviso({'ATENCION','No existe conección a internet','Intente más tarde o verifique su problema'})
return .t.
endif
If "GMAIL.COM" $ Upper( cMailServer ) .and. ( Empty( cUser ) .or. Empty( cPass ) )
MsgStop( "Con GMail son requeridos nombre de usuario y contraseña", "Atención" )
Return .t.
EndIf
CursorWait()
bMens:=Mensaje('Destino: '+AllTrim(cTo),'Espere, enviando un Mail')
nEle := 1
While ! Empty( cToken := StrToken( cAttach, nEle++, "," ) )
AAdd( aAttach, cToken )
End
aviso(aAttach)
Try
oCfg := CreateObject( "CDO.Configuration" )
With Object oCfg:Fields
:Item( cdoSMTPServer ):Value := AllTrim( cMailServer )
:Item( cdoSMTPServerPort ):Value := nPort /////// es el 465 /////
:Item( cdoSendUsing ):Value := 2
If lAuth
:Item( cdoSMTPAuthenticate ):Value := .T.
:Item( cdoSendUserName ):Value := AllTrim( cUser )
:Item( cdoSendPassword ):Value := AllTrim( cPass )
:Item( cdoSMTPUseSSL ):Value := .T.
EndIf
:Item( cdoSMTPConnectionTimeout ):Value := 30
:Update()
End With
oMsg := CreateObject( "CDO.Message" )
With Object oMsg
:Configuration := oCfg
:From := AllTrim( cFrom )
:To := AllTrim( cTo )
:Subject := AllTrim( cSubject )
:TextBody := AllTrim( cBody )
For nEle := 1 To Len( aAttach )
if File( AllTrim( aAttach[ nEle ] ) )
:AddAttachment := AllTrim( aAttach[ nEle ] )
else
Aviso({'El archivo adjunto '+Upper(AllTrim( aAttach[ nEle ] )),;
'no será enviado, no existe'})
endif
Next
If ! Empty( cBCC )
:BCC := AllTrim( cBCC )
EndIf
:Send()
End With
CATCH oError
Eval(bMens)
CursorArrow()
MsgInfo ( "Error envio de EMail" + CRLF + ;
"Remitente: " + AllTrim( cFrom ) + CRLF + ;
"Destinatario: " + AllTrim( cTo ) + CRLF + ;
"Error: " + TRANSFORM ( oError:GenCode, NIL ) + ";" + CRLF + ;
"SubC: " + TRANSFORM ( oError:SubCode, NIL ) + ";" + CRLF + ;
"Codigo: " + TRANSFORM ( oError:OsCode, NIL ) + ";" + CRLF + ;
"SubSistema: " + TRANSFORM ( oError:SubSystem, NIL ) + ";" + CRLF + ;
"Mensaje: " + oError:Description, "Atención" )
oCfg := Nil
oMsg := Nil
Return ( .F. )
END
oCfg := Nil
oMsg := Nil
Eval(bMens)
SndPlaySound( GetWinDir() + "\media\Tada.wav", 0 )
CursorArrow()
if oDlg<>NIL
oDlg:End()
endif
Return ( .T. )