Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automation VFP 8.0 to email through a gmail account

Status
Not open for further replies.

newtofoxpro

Programmer
Sep 16, 2007
301
IN
Is it possible with smtp.gmail.com pure vfp code? Not interested using outlook etc.,
 
Also my outlook configuration is set to use SSL, blat does not work out to gmail for me, but the code does work with the mail server of a site I maintain.

Lookou for something supporting SMTP via SSL tunnel or add SSL seperately. I didn't try myself but googling blat SSL I found a thread where openssl in conjunction with stunnel works out, haven't taken a close look, though.

At least you're now not having any Foxpro issues anymore, the Send() itself does return errors related to the server authentication.

Bye, Olaf.
 
No, I use Port 587 in outlook. So I used Port 587 with blat error returns # 2. Again one thing while googling I read blat does not support SSL connection. In outlook there is an option for "This server requires an encrypted connection (SSL) Please comment if you know about SSL and how use with blat.
 
Yes, this code works. I tried with another smtp server. thanks but still learning blat with ssl connection.
 
I'd need to learn that, too. And that's where I opt out of this. You should perhaps ask a seperate question, maybe not only here, in regard to SSL. From as fas as I read it's something you can on top, previous to the blat code, and blat will then connect through a SSL tunnel. This is then setup to make connections to the host smpt.gmail.com via SSL.

I don't need that and while it would be interesting in general, I'll not dig further. It's a seperate problem, your email code, blat or whtever else you'd use, would stay as is, on top of a SSL connection.

Just one thing: SSL stadard ports are as follows:
nsiiops 261/tcp # IIOP Name Service over TLS/SSL
https 443/tcp # http protocol over TLS/SSL
smtps 465/tcp # smtp protocol over TLS/SSL (was ssmtp)
nntps 563/tcp # nntp protocol over TLS/SSL (was snntp)
imap4-ssl 585/tcp # IMAP4+SSL (use 993 instead)
sshell 614/tcp # SSLshell
ldaps 636/tcp # ldap protocol over TLS/SSL (was sldap)
ftps-data 989/tcp # ftp protocol, data, over TLS/SSL
ftps 990/tcp # ftp protocol, control, over TLS/SSL
telnets 992/tcp # telnet protocol over TLS/SSL
imaps 993/tcp # imap4 protocol over TLS/SSL
ircs 994/tcp # irc protocol over TLS/SSL
pop3s 995/tcp # pop3 protocol over TLS/SSL (was spop3)
msft-gc-ssl 3269/tcp # Microsoft Global Catalog with LDAP/SSL

So SMTPS, which is SMPT over SSL. Your outlook config using port 587 is pointing toward imap4-ssl, but blat does not use IMAP, but SMTP. It's not hard wired to use these ports, eg a web server can server http via the ormal port80, but also via 81,82,8080 or any other port.

Still that points to you using an IMAP connection rather than SMTP in outlook.

Bye, Olaf.
 
@Olaf : I am not fan of blat I found this blat.dll and it's code is small. While googling I found CDO 2000 CLASS which sends email via gmail. Do you know CDO 2000 CLASS if yes then please post advantages & disadvantages. It works with vfp8.

Thanks & Best Regards.
 
Do you know CDO 2000 CLASS

That was the suggestion I gave you way back near the start of this thread (in my message dated 1 Sep 11 10:02). You asked for more details, and I gave you a reference to Mike Gagnon's FAQ, which has the full code.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
newtofoxpro,

you brought up blat, after I doubted CDO being present. Your concern at that time was to use something not needing any 3rd party component, I pointed to jrbbldr's answer, which is in turn pointing to faq184-3840, which in turn is pointing to
Besides that, I use blat for a website and am satisfied with it, as I don't need SMTPS, but simply SMTP. For our company and a customer I use Outlook automation, as it's there and much easier to use than anything else I tried.

Ther are very many ways to send mail,
You could also have looked up yourself, that gamil is using SSL and thus the mail solution you need must support SMTPS.

You're asking for a bit too much, if you want a full solution and don't follow pointers given, but instead come up with other partyl solutions we should fix. In the end Craig Boyds code you found about blat is working for normal SMTP without the need to support SSL connections.

And last not least HTTPS, FTPS, SMPTS etc. are all just the same protocol executed via an SSL connection or SSL tunnel, and I already pointed you toward stunnel...

I can't do everything for you, and I did quite a lot, for a weekend.

OK?

Bye, Olaf.
 
Thank you Olaf, Mike. I hope, you would not mind to read this code.

Best Regards.

loMail = NEWOBJECT("Cdo2000", "Cdo2000.fxp")
WITH loMail
.cServer = "smtp.gmail.com"
.nServerPort = 465
.lUseSSL = .T.
.nAuthenticate = 1
.cUserName = "myemailid@gmail.com"
.cPassword = "mypassword"
.cFrom = "myemailid@gmail.com"
.cTo = "youremailid@gmail.com"
.cSubject = "My Email Subject"
.cTextBody = "This is a text body."
.cAttachment = "c:\myattachment.txt"
ENDWITH
IF loMail.Send() <= 0
WAIT WINDOW NOWAIT 'Email Sent.'
ENDIF

*** Following code save as cdo2000.prg

#DEFINE cdoSendPassword "#DEFINE cdoSendUserName "#DEFINE cdoSendUsingMethod "#DEFINE cdoSMTPAuthenticate "#DEFINE cdoSMTPConnectionTimeout "#DEFINE cdoSMTPServer "#DEFINE cdoSMTPServerPort "#DEFINE cdoSMTPUseSSL "#DEFINE cdoURLGetLatestVersion "#DEFINE cdoAnonymous 0 && Perform no authentication (anonymous)
#DEFINE cdoBasic 1 && Use the basic (clear text) authentication mechanism.
#DEFINE cdoSendUsingPort 2 && Send the message using the SMTP protocol over the network.
#DEFINE cdoXMailer "urn:schemas:mailheader:x-mailer"
DEFINE CLASS cdo2000 AS Custom
PROTECTED aErrors[1], nErrorCount, oMsg, oCfg, cXMailer
nErrorCount = 0
oMsg = Null
cFrom = ""
cReplyTo = ""
cTo = ""
cCC = ""
cBCC = ""
cAttachment = ""
cSubject = ""
cHtmlBody = ""
cTextBody = ""
cHtmlBodyUrl = ""
cCharset = ""
cPriority = ""
oCfg = Null
cServer = ""
nServerPort = 25
lUseSSL = .F.
nConnectionTimeout = 30
nAuthenticate = cdoAnonymous
cUserName = ""
cPassword = ""
lURLGetLatestVersion = .T.
cXMailer = "VFP CDO 2000 mailer Ver 1.1.100 2010"
PROTECTED PROCEDURE Init
This.ClearErrors()
ENDPROC
PROCEDURE Send
IF This.GetErrorCount() > 0
RETURN This.GetErrorCount()
ENDIF
WITH This
.ClearErrors()
.oCfg = CREATEOBJECT("CDO.Configuration")
.oMsg = CREATEOBJECT("CDO.Message")
.oMsg.Configuration = This.oCfg
ENDWITH
LOCAL lnind, laList[1], loHeader, laDummy[1], lcMailHeader
IF This.SetConfiguration() > 0
RETURN This.GetErrorCount()
ENDIF
IF EMPTY(This.cFrom)
This.AddError("ERROR : From is empty.")
ENDIF
IF EMPTY(This.cSubject)
This.AddError("ERROR : Subject is empty.")
ENDIF
IF EMPTY(This.cTo) AND EMPTY(This.cCC) AND EMPTY(This.cBCC)
This.AddError("ERROR : To, CC and BCC are all empty.")
ENDIF
IF This.GetErrorCount() > 0
RETURN This.GetErrorCount()
ENDIF
This.SetHeader()
WITH This.oMsg
.From = This.cFrom
.ReplyTo = This.cReplyTo
.To = This.cTo
.CC = This.cCC
.BCC = This.cBCC
.Subject = This.cSubject
IF NOT EMPTY(This.cHtmlBodyUrl)
.CreateMHTMLBody(This.cHtmlBodyUrl)
ENDIF
IF NOT EMPTY(This.cHtmlBody)
.HtmlBody = This.cHtmlBody
ENDIF
IF NOT EMPTY(This.cTextBody)
.TextBody = This.cTextBody
ENDIF
IF NOT EMPTY(This.cCharset)
IF NOT EMPTY(.HtmlBody)
.HtmlBodyPart.Charset = This.cCharset
ENDIF

IF NOT EMPTY(.TextBody)
.TextBodyPart.Charset = This.cCharset
ENDIF
ENDIF
IF NOT EMPTY(This.cAttachment)
FOR lnind=1 TO ALINES(laList, CHRTRAN(This.cAttachment, [,;], CHR(13) + CHR(13)))
lcAttachment = ALLTRIM(laList[lnind])
IF EMPTY(laList[lnind])
LOOP
ENDIF
IF ADIR(laDummy, lcAttachment) = 0
This.AddError("ERROR: Attacment not Found - " + lcAttachment)
ELSE
IF UPPER(lcAttachment) <> UPPER(FULLPATH(lcAttachment))
lcAttachment = FULLPATH(lcAttachment)
ENDIF
.AddAttachment(lcAttachment)
ENDIF
ENDFOR
ENDIF
IF NOT EMPTY(This.cCharset)
.BodyPart.Charset = This.cCharset
ENDIF
IF NOT EMPTY(This.cPriority)
lcMailHeader = "urn:schemas:mailheader:"
.Fields(lcMailHeader + "Priority") = LOWER(This.cPriority)
.Fields(lcMailHeader + "Importance") = LOWER(This.cPriority)
DO CASE
CASE This.cPriority = "High"
.Fields(lcMailHeader + "X-Priority") = 1 && 5=Low, 3=Normal, 1=High
CASE This.cPriority = "Normal"
.Fields(lcMailHeader + "X-Priority") = 3 && 5=Low, 3=Normal, 1=High
CASE This.cPriority = "Low"
.Fields(lcMailHeader + "X-Priority") = 5 && 5=Low, 3=Normal, 1=High
ENDCASE
.Fields.Update()
ENDIF
ENDWITH
IF This.GetErrorCount() > 0
RETURN This.GetErrorCount()
ENDIF
This.oMsg.Send()
RETURN This.GetErrorCount()
ENDPROC
PROCEDURE ClearErrors()
This.nErrorCount = 0
DIMENSION This.aErrors[1]
This.aErrors[1] = Null
RETURN This.nErrorCount
ENDPROC
PROCEDURE GetErrorCount
RETURN This.nErrorCount
ENDPROC
PROCEDURE GetError
LPARAMETERS tnErrorno
IF tnErrorno <= This.GetErrorCount()
RETURN This.aErrors[tnErrorno]
ELSE
RETURN Null
ENDIF
ENDPROC
PROTECTED PROCEDURE SetConfiguration
IF EMPTY(This.cServer)
This.AddError("ERROR: SMTP Server isn't specified.")
ENDIF
IF NOT INLIST(This.nAuthenticate, cdoAnonymous, cdoBasic)
This.AddError("ERROR: Invalid Authentication protocol ")
ENDIF
IF This.nAuthenticate = cdoBasic ;
AND (EMPTY(This.cUserName) OR EMPTY(This.cPassword))
This.AddError("ERROR: User name/Password is required for basic authentication")
ENDIF
IF This.GetErrorCount() > 0
RETURN This.GetErrorCount()
ENDIF
WITH This.oCfg.Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = This.cServer
.Item(cdoSMTPServerPort) = This.nServerPort
.Item(cdoSMTPConnectionTimeout) = This.nConnectionTimeout
.Item(cdoSMTPAuthenticate) = This.nAuthenticate
IF This.nAuthenticate = cdoBasic
.Item(cdoSendUserName) = This.cUserName
.Item(cdoSendPassword) = This.cPassword
ENDIF
.Item(cdoURLGetLatestVersion) = This.lURLGetLatestVersion
.Item(cdoSMTPUseSSL) = This.lUseSSL
.Update()
ENDWITH
RETURN This.GetErrorCount()
ENDPROC
PROTECTED PROCEDURE AddError
LPARAMETERS tcErrorMsg
This.nErrorCount = This.nErrorCount + 1
DIMENSION This.aErrors[This.nErrorCount]
This.aErrors[This.nErrorCount] = tcErrorMsg
RETURN This.nErrorCount
ENDPROC
PROTECTED PROCEDURE AddOneError
LPARAMETERS tcPrefix, tnError, tcMethod, tnLine
LOCAL lcErrorMsg, laList[1]
IF INLIST(tnError, 1427,1429)
AERROR(laList)
lcErrorMsg = TRANSFORM(laList[7], "@0") + " " + laList[3]
ELSE
lcErrorMsg = MESSAGE()
ENDIF
This.AddError(tcPrefix + ":" + TRANSFORM(tnError) + " # " + ;
tcMethod + " # " + TRANSFORM(tnLine) + " # " + lcErrorMsg)
RETURN This.nErrorCount
ENDPROC
PROTECTED PROCEDURE Error
LPARAMETERS tnError, tcMethod, tnLine
This.AddOneError("ERROR: ", tnError, tcMethod, tnLine )
RETURN This.nErrorCount
ENDPROC
PROTECTED PROCEDURE SetHeader
LOCAL loHeader
IF NOT EMPTY(This.cXMailer)
loHeader = This.oMsg.Fields
WITH loHeader
.Item(cdoXMailer) = This.cXMailer
.Update()
ENDWITH
ENDIF
ENDPROC
PROTECTED PROCEDURE cPriority_assign(tvVal)
IF INLIST("~" + PROPER(tvVal) + "~", "~High~", "~Normal~", "~Low~") OR EMPTY(tvVal)
This.cPriority = PROPER(ALLTRIM(tvVal))
ELSE
This.AddError("ERROR: Invalid value for cPriority property.")
ENDIF
ENDPROC
ENDDEFINE
 
I hope, you would not mind to read this code.

Acutally, I do mind. It's a lot to read, especially if you simply want someone to check it for you (but I expect Olaf has more patience than me).

If you have a particular problem with the code, by all means let us know, and we will do our best to answer.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Acutally, I do mind.
I second what Mike as said...

If the code is working for you, then why should we have to read through it?

If the code does not work, then what is the error message and where is it occurring?

Otherwise - good luck,
JRB-Bldr

 
Code:
If the code does not work, then what is the error message and where is it occurring?

Exactly what I was suggesting to newtofoxpro already at least twice.

@newtofoxpro, to give you the opportunity to tell us exactly the location of the error, have a read on error handling:
Besides that positing lengthy code you copied from somewhere else is not a good idea, tell your source via a link and perhaps ask the original author about the code, if you have problems with it.

Code you find somewhere, does age, it's either untested (unlikely with such a long code) or written with a certain version of VFP or some DLL or OCX used, and notguaranteed to continue working forever.

Bye, Olaf.
 
If the code is working for you, then why should we have to read through it? "

In fact, my posted code is working for me. I am very sorry and I understood that unnecessary I am stretching this thread. I just wanted to share the code which worked for me.

Very sorry again. I have no more questions.
Thanks & Best Regards.
 
I just wanted to share the code which worked for me.

Well we appreciate sharing working code, but if it provided a unique solution to a problem, then the best place to put it would be in the FAQ's.

Good Luck,
JRB-Bldr


 
I also got blat working via Stunnel. Stunnel comes with a config file for gmail ssmtp, you just need to edit it and uncomment lines for gmail, then blat needs to send to localhost on port 465, which stunnel listens to and dispatches towards gmail ssl-encrypted.

This is likely not what you want, as stunnel needs to be installed and run as a service, so this is not simply deploying another dll.

I wonder why CDO.Message now works for you (as that is what the cdo2000 class is using). But it is not wrong using it, as it's meant for messaging including mail, including smpt nntp and obviously also supports ssl configurations. But it's minimum requirement is win2000 I think. No real limitation, as XP typically is the end users minimum.

I remember though, CDO didn't work out for me on Vista in the first place.

Bye, Olaf.
 
Newtofoxpro,

I also appreciate your sharing the code. But just to clarify: is this code that you wrote yourself? If so, that's fine. But if you copied it from somewhere else, you should at least give credit to the source - or better still, provide a link back to the source.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top