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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Emailling from within VFP 3

Status
Not open for further replies.

jcharnes

Programmer
Jan 30, 2003
3
US
I've been reading a lot about enabling email from within a VFP program, but I haven't jumped in with both feet yet. I've seen references to using your own smtp engine and interfacing to Outlook and Outlook express (MegaFox).

I was wondering if anyone who has tried it might suggest the best approach they have found to this process?

TIA for your thoughts

JC
 
jcharnes

was wondering if anyone who has tried it might suggest the best approach they have found to this process?

It depends on your requirements. You can look at faq184-3894.


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
If you do a Keyword Search within forum184 (the original VFP forum) for "Email" you will find a LARGE number of postings and replies.

The way that has worked well for me was to use a combination of Outlook and Redemption (to get around Outlook's Security Message). That enabled me to send both emails and FAX's (using MS Shared FAX Transport Services) with basically one single approach.
However others have found a number of other good approaches.

Good Luck,


JRB-Bldr
VisionQuest Consulting
Business Analyst & CIO Consulting Services
CIOServices@yahoo.com
 
jcharnes,

Here are a few ways to go about emailing from VFP included is (MAPI, OUTLOOK, CDONTS, CDOSYS, and JMAIL). Credit goes some to me and some to all of the other members of VFP Tek-Tips that wrote code that I used to create this. I wrote this really fast just now so it could be improved but is a good example of the ways to send email.

Code:
*!* Sample code using SendEmail procedure
DIMENSION aryAttach(1)
LOCAL lcFrom, lcTo, lcSubject, lcBody, lnCount
aryAttach(1) = "C:\autoexec.bat"
lcFrom = "me@mydomain.com"
lcTo = "someone@somedomain.com"
lcSubject = "Hey Have You Tried VFP Email?"
lcBody = "Just wanted to let you know that VFP is pretty versatile and has a lot of ways to send email."

FOR lnCount = 1 TO 5 &&let's do them all, some will work some may not
	=SendEmail(lnCount, lcFrom, lcTo, lcSubject, lcBody, @aryAttach)
ENDFOR


PROCEDURE SendEmail(tcType, tcFrom, tcTo, tcSubject, tcBody, tcFiles)
	LOCAL llEmailStatus, lcErrorHandlerWas, lcType
	lcErrorHandlerWas = ON("ERROR")
	lcType = ""
	WAIT WINDOW "     One Moment... Email is being generated and sent     " NOWAIT
	DO CASE
	CASE tcType = 1
		llEmailStatus = SendViaMAPI(tcFrom, tcTo, tcSubject, tcBody)
		lcType = "MAPI"
	CASE tcType = 2
		llEmailStatus = SendViaOutlook(tcFrom, tcTo, tcSubject, tcBody, @tcFiles)
		lcType = "OUTLOOK"
	CASE tcType = 3
		llEmailStatus = SendViaCDOnts(tcFrom, tcTo, tcSubject, tcBody, @tcFiles)
		lcType = "CDONTS"
	CASE tcType = 4
		llEmailStatus = SendViaCDOsys(tcFrom, tcTo, tcSubject, tcBody, @tcFiles)
		lcType = "CDOSYS"
	CASE tcType = 5
		llEmailStatus = SendViaJMail(tcFrom, tcTo, tcSubject, tcBody, @tcFiles)
		lcType = "JMAIL"
	ENDCASE
	IF !EMPTY(lcErrorHandlerWas)
		ON ERROR &lcErrorHandlerWas
	ELSE
		ON ERROR
	ENDIF
	WAIT CLEAR
	IF llEmailStatus
		MESSAGEBOX("Your message to " + tcTo + " has been sent.",64,"EMAIL SENT SUCCESSFULLY VIA " + lcType)
	ELSE
		MESSAGEBOX("Your message to " + tcTo + " was not sent.",64,"EMAIL PROBLEM WITH " + lcType)
	ENDIF
ENDPROC

FUNCTION SendViaMAPI(tcFrom, tcTo, tcSubject, tcBody)
	ON ERROR RETURN(.F.)
	LOCAL loSession, loMessages
	loSession = CREATEOBJECT( "MSMAPI.MAPISession" )
	loSession.Signon()
	IF (loSession.SessionID > 0)
		loMessages = CREATEOBJECT( "MSMAPI.MAPIMessages" )
		loMessages.SessionID = loSession.SessionID
	ENDIF
	WITH loMessages
		.Compose()
		.RecipDisplayName = tcTo
		.RecipType = 1
		.ResolveName()
		.MsgSubject = tcSubject
		.MsgNoteText = tcBody
		.SEND(.F.)
	ENDWITH
	loSession.Signoff()
	STORE .NULL. to loSession, loMessages
	RELEASE loSession, loMessages
	RETURN .T.
ENDFUNC


FUNCTION SendViaOutlook(tcFrom, tcTo, tcSubject, tcBody, tcFiles)
	ON ERROR RETURN(.F.)
	LOCAL oOutlook, oItem, lnCountAttachments
	oOutlook = CREATEOBJECT("outlook.application")
	oItem = oOutlook.CreateItem(0)
	WITH oItem
		.Subject = tcSubject
		.TO = tcTo
		.Body = tcBody
		IF PCOUNT() > 4
			FOR lnCountAttachments = 1 TO ALEN(tcFiles)
				.Attachments.ADD(tcFiles(lnCountAttachments))
			ENDFOR
		ENDIF
		.SEND
	ENDWITH
	STORE .NULL. to oOutlook, oItem
	RELEASE oOutlook, oItem
	RETURN .T.
ENDFUNC


FUNCTION SendViaCDOnts(tcFrom, tcTo, tcSubject, tcBody, tcFiles)
	ON ERROR RETURN(.F.)
	LOCAL oNewMail, lnCountAttachments
	oNewMail = CREATEOBJECT("CDONTS.NewMail")
	WITH oNewMail
		.FROM = tcFrom
		.TO = tcTo
		.Subject = tcSubject
		.Body = tcBody
		IF PCOUNT() > 4
			FOR lnCountAttachments = 1 TO ALEN(tcFiles)
				.AttachFile = tcFiles(lnCountAttachments)
			ENDFOR
		ENDIF
		.SEND
	ENDWITH
	oNewMail = .NULL.
	RELEASE oNewMail
	RETURN .T.
ENDFUNC

FUNCTION SendViaCDOsys(tcFrom, tcTo, tcSubject, tcBody, tcFiles)
	ON ERROR RETURN(.F.)
	Local lcSchema, loConfig, loMsg, loAtt, lnCountAttachments
	lcSchema = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/"[/URL]

	loConfig = CREATEOBJECT("CDO.Configuration")

	WITH loConfig.FIELDS
	    .ITEM(lcSchema + "smtpserverport") = 25 && SMTP Port
	    .ITEM(lcSchema + "sendusing") = 2 && Send it using port
	    .ITEM(lcSchema + "smtpserver") = "mail.myhost.com" && host of smtp server
	    .ITEM(lcSchema + "smtpauthenticate") = 1 && Authenticate
	    .ITEM(lcSchema + "sendusername") = "VaLiDUserNaMe" && Username
	    .ITEM(lcSchema + "sendpassword") = "PaSsWoRd1234" && Password
	    .UPDATE
	ENDWITH

	loMsg = CREATEOBJECT ("CDO.Message")
	loMsg.Configuration = loConfig
	WITH loMsg
		.TO = tcTo
		.From = tcFrom
		.Subject = tcSubject
		.TextBody = tcBody
		IF PCOUNT() > 4
			FOR lnCountAttachments = 1 TO ALEN(tcFiles)
				loAtt=.AddAttachMent(tcFiles(lnCountAttachments))
			ENDFOR
		ENDIF
		.SEND()
	ENDWITH

	STORE .NULL. to loConfig, loMsg
	RELEASE loConfig, loMsg
 	RETURN .T.
ENDFUNC

FUNCTION SendViaJMail(tcFrom, tcTo, tcSubject, tcBody, tcFiles)
	ON ERROR RETURN(.F.)
	***Requires FREEWARE  w3 JMail  Free, v 4.3
	***Download at [URL unfurl="true"]http://tech.dimac.net/[/URL]
	LOCAL oSMTPMail, lnCountAttachments
	oSMTPMail = CREATEOBJECT("jmail.SMTPMail")
	*!* oSMTPMail .ServerAddress = "mail.domain.com"
	WITH oSMTPMail
		.AddRecipient(tcTo)
		.Sender = tcFrom
		.SenderName = tcFrom
		.Subject = tcSubject
		.Body = tcBody
		IF PCOUNT() > 4
			FOR lnCountAttachments = 1 TO ALEN(tcFiles)
				.AddAttachMent(tcFiles(lnCountAttachments))
			ENDFOR
		ENDIF
		.Execute()
	ENDWITH
	oSMTPMail = .NULL.
	RELEASE oSMTPMail
	RETURN .T.
ENDFUNC

boyd.gif

craig1442@mchsi.com
"Whom computers would destroy, they must first drive mad." - Anon​
 
Thanks to all for the great help. Special thanks to Craig - I always find clearly written code to be the best learning tool. It'll take a bit for me to digest this, but I have no doubt that this is what I need to know.

Again, thanks. I'll try to contribute in my areas of expertise.

JC
 
As was said, it all depends on your circumstances:
Do you want to require your users to have Outlook?
Do you want to require your users to have some MAPI compliant email program?
Do you want to require your users to have Win2k+?
Do you want to distribute an ActiveX control to do it?
Can you put up with the warning message email programs show the user when you automate them? Can you work around it?
Do you need to send a Whole Lot of emails? Fast?
Does the server(s) you're using (most don't) require strong authentication?
 
I use the osstrosoft.com dll and have a very simple prg that uses it. Freeware


code (put in prg)

PARAMETERS lcFrom, lcTo, lcSubject, lcBody, lcAttachment


** Download ossmtp.dll from ** copy to %systemroot%\winnt\system32
** go to start/run and type cmd then enter
** in dos window type cd \winnt\system32
** type regsvr32 ossmtp.dll
** if successfull you should be able to use this program
** Rob Sutton 06/10/04

lcServer = "192.168.1.22"
lnPort = 25
lcAuthType = 0

oSmtp = CREATEOBJECT("OSSMTP.SMTPSession")

oSmtp.Server = lcServer
oSmtp.Port = lnPort


oSmtp.MailFrom=lcFrom
oSmtp.SendTo = lcTo
*oSmtp.BCC = lcBCC
*oSmtp.CC = lcCC

oSmtp.MessageSubject = lcSubject

oSmtp.MessageText = lcBody
*oSmtp.MessageHTML = (FILETOSTR("htmlsample.txt"))
oSmtp.Attachments.Add(lcAttachment)

oSmtp.AuthenticationType=0

oSmtp.SendEmail


RETURN oSmtp.Status
RELEASE oSmtp


*!* Methods:
*!* oSmtp.SendEmail()
*!* connects to server, authenticates (if requested), sends message, closes the connection

*!* Properties:
*!* oSmtp.AuthenticationType

*!* Returns/Sets authentication type (default - 0)
*!* supported types are: POP3(1), AUTH LOGIN (2), AUTH PLAIN(3)

*!* oSmtp.BCC
*!* Returns/Sets BCC e-mail address

*!* oSmtp.CC
*!* Returns/Sets CC e-mail address

*!* oSmtp.Charset
*!* Returns/Sets message charset (default - "us-ascii")

*!* oSmtp.MailFrom
*!* Returns/Sets sender e-mail address

*!* oSmtp.MessageHTML
*!* Returns/Sets HTML part of message (optional)

*!* oSmtp.MessageSubject
*!* Returns/Sets subject of message

*!* oSmtp.MessageText
*!* Returns/Sets text of message

*!* oSmtp.Password
*!* Returns/Sets password for server authentication (optional)

*!* oSmtp.POPServer
*!* Returns/Sets server for POP3 authentication (optional)

*!* oSmtp.Port
*!* Returns/Sets object server port (default - 25)

*!* oSmtp.SendTo
*!* Returns/Sets recipient e-mail address

*!* oSmtp.Server
*!* Returns/Sets object server name or IP address

*!* oSmtp.Status
*!* Returns status of component

*!* oSmtp.TimeStamp
*!* Returns/Sets message timestamp (default - system date/time)

*!* oSmtp.Username
*!* Returns/Sets username for server authentication (optional)




The more I learn the more I find I know nothing.

Rob
 
Rob

Thanks for the tip on the ostrosoft dll. This project has been on the back burner for a while but is becoming active again. Your timing is perfect. I'll try your technique.

JC
 
JC,
Thanks for the star. I enjoy simplicity when I can by using third party class libraries. Why reinvent the wheel? Less debugging means more time developing and creating reports. With something as important as email functionality in my programs, I would rather use someones third party dll which has been tested thoroughly and is a specialized product. I also like the price, FREE and its simplicity. When you have a minute check out ostrosoft's other dll's. They have a DNS, WHOIS, and web socket interface. I think one of the most fun part of using Fox would be the flexibility of being able to use these third party dll's and explore their functionality.

The more I learn the more I find I know nothing.

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top