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.,
 
Thank you for quick answer

"Certainly. Just set the server name to smtp.gmail.com, and the username to your usual Gmail name "

Can you post Example ?

Regards.
 
Have you looked at the FAQ's in this forum concerning Email?

One in particular that might be of interest might be:
How to send email using SMTP and no third party OCX?
faq184-3840


Good Luck,
JRB-Bldr

 
Or have you done a Search for the keyword "EMAIL" in this forum?

If you had you might have already come across a number of previous postings which might answer your question.

Again a notable one might be:
Emailling from within VFP
thread184-797973


Good Luck,
JRB-Bldr
 
Thank you

I would like to follow last post first.

@Mike,

I just copy following code and its generate an error.

oMSG = CREATEOBJECT('cdo.message')
oMSG.To = 'me@nowhere.com'
oMSG.From = 'me'
oMSG.Subject = 'Hello Email'
oMSG.TextBody = 'This is an easy way to create an email'
oMSG.Send()

" Note #1 : This requires SMTP services installed and running on the local computer. "

I follow NOTE # 1 as under



I am completely new for this e-mail project. Is it possible to you to post step-by-step?

Thank You,
Best Regards.
 
As I understand it, you don't need to install anything on the client computer. The only pre-requisite is CDO, which is already present in all versions of Windows that support VFP.

I've used Mike Gagnon's code in an application which has dozens of users on all kinds of computers and networks, and don't recall having a problem with it. I suggest you give it a try.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
You said you want to use the gmail mail server, so the CDO.Message is no option for you.

Take look at jrbbldr answer referencing the FAQ "How to send email using SMTP and no third party OCX?"

Only this is answering your inital question. If you don't want to depend on some external component being intalled, you better use code that does give you excatly that: Only using VFP and nothing else.

Mike, I don't see CDO.Message installed on any OS supporting VFP. Also newtofoxpro already stated he had an error using it.

@newtofoxpro#: It would help veeery much, if you tell us, what error. Sorry, I still can't read anyones mind.

Bye, Olaf.
 
Thanks, What I tried is BLAT.DLL as under. But this code gives ERROR.. I have installed BLAT.DLL to my working folder.

*******************************
*!* Example of using SendViaBLAT
*******************************
#DEFINE PRIORITYHIGH 1
#DEFINE PRIORITYLOW 0

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

aryAttach(1) = ""
aryAttach(2) = ""

LOCAL lcFrom, lcTo, lcSubject, lcBody, lcCC, lcBCC, lcMailServer, lcUserName, lcPassword, lnPort, lnPriority, llHTMLFormat, lcErrReturn

lcFrom = "mymailid@gmail.com"
lcTo = "yourmailid@hotmail.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 = "Hello"
lcCC = ""
lcBCC = ""
lcMailServer = "smtp.gmail.com" && my SMTP Server
lnPort = 25 &&25 && default SMTP Server port
lcUserName = "mysername" && my SMTP username
lcPassword = "mypassword" && my SMTP password
lnPriority = PRIORITYHIGH

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

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

*******************************************
PROCEDURE SendViaBLAT(tcReturn, tcFrom, tcTo, tcSubject, tcBody, taFiles, tcCC, tcBCC, tcMailServer, tnPort, tcUserName, tcPassword, tnPriority, tlHTMLFormat)
*******************************************
LOCAL lcBlatParam, lcBodyFile, lnCountAttachments, lnResult, loError as Exception

lcBodyFile = ""

TRY
*!* Include full path in Declare, such as "C:\Blat240\full\blat.dll"
*!* or make sure that blat.dll is included in the system's PATH variable
DECLARE INTEGER Send IN "blat.dll" STRING cParam

lcBodyFile = ADDBS(SYS(2023)) + SYS(2015) + ".txt"
STRTOFILE(tcBody, lcBodyFile, 0) && body is placed in a text file to be sent by BLAT

lcBlatParam = GetShortPath(lcBodyFile)

IF TYPE("tcTo") = "C"
lcBlatParam = lcBlatParam + " -to " + ALLTRIM(tcTo)
ENDIF
IF TYPE("tcFrom") = "C"
lcBlatParam = lcBlatParam + " -f " + ALLTRIM(tcFrom)
ENDIF
IF TYPE("tcCC") = "C"
lcBlatParam = lcBlatParam + " -cc " + ALLTRIM(tcCC)
ENDIF
IF TYPE("tcBCC") = "C"
lcBlatParam = lcBlatParam + " -bcc " + ALLTRIM(tcBCC)
ENDIF
IF TYPE("tcSubject") = "C"
lcBlatParam = lcBlatParam + [ -s "] + ALLTRIM(tcSubject) + ["]
ENDIF
IF TYPE("tcMailserver") = "C"
lcBlatParam = lcBlatParam + " -server " + ALLTRIM(tcMailserver)
ENDIF
IF TYPE("tnPort") = "N"
lcBlatParam = lcBlatParam + ":" + TRANSFORM(tnPort)
ENDIF
IF TYPE("tcUsername") = "C"
lcBlatParam = lcBlatParam + " -u " + ALLTRIM(tcUsername)
ENDIF
IF TYPE("tcPassword") = "C"
lcBlatParam = lcBlatParam + " -pw " + ALLTRIM(tcPassword)
ENDIF
IF TYPE("tnPriority") = "N" AND BETWEEN(tnPriority, 0, 1)
lcBlatParam = lcBlatParam + " -priority " + TRANSFORM(tnPriority)
ENDIF
IF TYPE("tlHTMLFormat") = "L" AND tlHTMLFormat
lcBlatParam = lcBlatParam + " -html"
ENDIF

IF .f. &&TYPE("taFiles", 1) = "A"
lcBlatParam = lcBlatParam + " -attach "
FOR lnCountAttachments = 1 TO ALEN(taFiles)
lcBlatParam = lcBlatParam + GetShortPath(ALLTRIM(taFiles(lnCountAttachments))) + ","
ENDFOR
lcBlatParam = LEFT(lcBlatParam, LEN(lcBlatParam) - 1) && Remove Extra Comma
ENDIF

lnResult = Send(ALLTRIM(lcBlatParam))

IF lnResult != 0
DO CASE
CASE lnResult = -2
THROW "The server actively denied our connection./The mail server doesn't like the sender name. "
CASE lnResult = -1
THROW "Unable to open SMTP socket" OR ;
"SMTP get line did not return 220" OR ;
"command unable to write to socket" OR ;
"Server does not like To: address" OR ;
"Mail server error accepting message data."
CASE lnResult = 1
THROW "File name (message text) not given" OR ;
"Bad argument given"
CASE lnResult = 2
THROW "File (message text) does not exist"
CASE lnResult = 3
THROW "Error reading the file (message text) or attached file"
CASE lnResult = 4
THROW "File (message text) not of type FILE_TYPE_DISK "
CASE lnResult = 5
THROW "Error Reading File (message text)"
CASE lnResult = 12
THROW "-server or -f options not specified and not found in registry"
CASE lnResult = 13
THROW "Error opening temporary file in temp directory"
OTHERWISE
THROW "Unknown Error"
ENDCASE
ENDIF

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
CLEAR DLLS "Send"
IF FILE(lcBodyFile)
ERASE (lcBodyFile)
ENDIF
ENDTRY
ENDPROC

****************************************
Function GetShortPath
****************************************
LPARAMETERS lcFileName
LOCAL lnReturn, lcBuffer

Declare Integer GetShortPathNameA In Win32API As GetShortPathName String, String, Integer

lcBuffer = SPACE(255)
lnReturn= GetShortPathName(lcFileName, @lcBuffer, 255)

Clear Dlls "GetShortPathName"

Return (Left(lcBuffer, lnReturn))
ENDFUNC
 
Again, nobody can't read your mind: What error do you get?
Do you expect us to copy the code, install blat and test this, to get the error.

I used blat already and it works in general.

I'm not here to browse through 150 lines to find the line of error.

There are many things, which can fail, even if blat itself works.

Bye, Olaf.
 
Have you tried adding the path to BLAT in the DECLARE line as the comments say you should?

Tamar
 
@Olaf: Thanks for you post and accept my fault. When I run above code I got error as under...

'Hey Have you tried VFP Email ?' failed to be sent. Reason.
Error : 11
Line NO : 108
Message : Function Argument value, type, or count is invalid
Procedure : sendviablat
Details :
StackLevel : 2
Line Contents : THROW "File name (message text) not given" OR "Bad argument given"

 
CASE lnResult = 1
THROW "File name (message text) not given" OR ;
"Bad argument given"

This means blat returned 1 from the Send() call, In regard to this documentation this does perhaps not mean, what the THROW tells:


Return code 1 also is returned on one of these conditions:
Unable to open SMTP socket
SMTP get line did not return 220
command unable to write to socket
Server does not like To: address
Mail server error accepting message data.

Perhaps start with a much simpler direct blat Send() call:

Save some short text to a file c:\mail.txt, then change the following code with your details (username, password) and run it:

Code:
DECLARE INTEGER Send IN blat.dll STRING blatstring

* change this with your username and password:
lcCmd = '"c:\mail.txt" '+;
'-f mymailid@gmail.com '+;
'-t yourmailid@hotmail.com '+;
'-s "Have you tried Mail in VFP?" '+;
'-server smtp.gmail.com '+;
'-u username '+;
'-pw password'

lnResult = Send(lcCmd)
? lnResult

By the way: Your gmail SMTP server user name is your mailadress.

Bye, Olaf.
 
my fault again. There should be one chr space after username. Now it's taking time to process but returns error # 2
 
again:
- connection denied
or
- The mail server doesn't like the sender name.
- File (message text) does not exist

Connetion deneid can be because of wrong password or because POP/SMTP isn't activated at all.

Google does not by default allow SMTP/POP3 access, you need to activate this first. Did you?

To check that you should perhaps simply try to configure a normal email client to use the google mail, and not just via redirection.

When logged in at google mail, click on the gearwheel symbol in the upper right corner (options), choose Email options. There should be a tab Redirection and POP/IMAP. Click on that tab and activate POP.

You also get configuration instructions for many mail client software to test the POP connection does work out. If it does it will also work with BLAT.

Bye, Olaf.
 
When logged in at google mail, click on the gearwheel symbol in the upper right corner (options), choose Email options. There should be a tab Redirection and POP/IMAP. Click on that tab and activate POP."

I checked

POP Download : 1. Status : POP is enabled
IMAP Access : Status: IMAP is enabled

Note: I can manage gmail from my outlook.

"Connetion deneid can be because of wrong password or because POP/SMTP isn't activated at all."

I checked carefully Password is right.

Even I have checked smtp.gmail.com via telnet it is ok
 
One more thing, BLAT by default port 25 and while googling I found gmail works with port 465. So tried as -Port 465 OR -server smtp.gmail.com:465 This time code-process takes longer time and returns error # 1.
 
Do you use port 465 in outlook? My outlook configuration is set to yet another port.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top