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

How to send email... 1

Status
Not open for further replies.

Mandy_crw

Programmer
Jul 23, 2020
585
PH
Hi everyone... Can you send email using vfp9? May you please show me how.... Thanks...
 
I use a few techniques.

If you are not too concerned with cost - look at ChilKat Software, it is very comprehensive.
Below is a messy, embarrassing really, wrapper to send an email, with optional body, couple of attachments and an HTML body as well.
ChilKat have a free 30 day trial, I've used them since at least 2019, perhaps earlier and have no affiliation with them...

Code:
FUNCTION SECUREEMAIL
	PARAMETERS m.SMTPSERVER,m.SMTPNAME,m.SMTPUSERNAME,m.SMTPPASSWORD,m.RECIPIENT,m.SUBJECT,m.BODY,m.SMTPPORT,m.ATTFILE,m.ATTFILE2,M.HTMLBODY
	PRIVATE m.SMTPSERVER,m.SMTPNAME,m.SMTPUSERNAME,m.SMTPPASSWORD,m.RECIPIENT,m.SUBJECT,m.BODY,M.SMTPPORT, m.ATTFILE,m.ATTFILE2,I
	LOCAL LOMAILMAN, LOEMAIL,LNSUCCESS

	IF PCOUNT() < 11
		m.HTMLBODY = .F.
	ENDIF
	IF PCOUNT() < 10
		m.ATTFILE2 = ""
	ENDIF
	IF PCOUNT() < 9
		m.ATTFILE = ""
	ENDIF
	m.SMTPNAME = STRTRAN(m.SMTPNAME,";",",")
	m.RECIPIENT = STRTRAN(m.RECIPIENT,";",",")

	*  The mailman object is used for sending and receiving email.
	LOMAILMAN = CREATEOBJECT('Chilkat_9_5_0.MailMan')

	*  Any string argument automatically begins the 30-day trial.
	LNSUCCESS = LOMAILMAN.UNLOCKCOMPONENT("secretcodehere")
	IF (LNSUCCESS <> 1) THEN
		MESSAGEBOX(LOMAILMAN.LASTERRORTEXT,48,"Problem")
	ELSE

		LOMAILMAN.SMTPHOST	= m.SMTPSERVER
		LOMAILMAN.SMTPUSERNAME = m.SMTPUSERNAME
		LOMAILMAN.SMTPPASSWORD = m.SMTPPASSWORD
		LOMAILMAN.SMTPSSL = 0
		LOMAILMAN.STARTTLS = 1
		LOMAILMAN.SMTPPORT = m.SMTPPORT

		LOEMAIL = CREATEOBJECT("Chilkat_9_5_0.Email")

		loemail.Charset = "utf-8"
		LOEMAIL.FROM = m.SMTPNAME
		IF !","$m.RECIPIENT .and. !m.CRLF$m.RECIPIENT .and. !";"$m.RECIPIENT
			LOEMAIL.ADDTO(m.RECIPIENT,m.RECIPIENT)
		ELSE
			M.RECIPIENT = STRTRAN(STRTRAN(M.RECIPIENT, ',', M.CRLF),";",m.CRLF)
			FOR I=1 to MEMLINES(M.RECIPIENT)
				IF "@"$MLINE(M.RECIPIENT,I)
					LOEMAIL.ADDTO(MLINE(m.RECIPIENT,I),MLINE(m.RECIPIENT,I))
				ENDIF
			ENDFOR
		ENDIF
		IF !M.HTMLBODY
			loEmail.AddPlainTextAlternativeBody(m.Body)
		ELSE
			loEmail.AddHtmlAlternativeBody(m.Body)
		ENDIF
		
		LOEMAIL.SUBJECT = m.SUBJECT
		IF !EMPTY(m.AttFile)
			loEmail.AddFileAttachment(m.AttFile)		
		ENDIF
		IF !EMPTY(m.AttFile2)
			loEmail.AddFileAttachment(m.AttFile2)		
		ENDIF
		WAIT "Sending... Please Wait" WINDOW NOWAIT TIMEOUT 10
		LOMAILMAN.OPENSMTPCONNECTION()
		LLRESULT = LOMAILMAN.SENDEMAIL(LOEMAIL)
		WAIT CLEAR
		IF LLRESULT <> 1
			DO CASE
				CASE LEFT(ALLTRIM(LOMAILMAN.LASTERRORTEXT),3)= "-3 "
					MESSAGEBOX("Problem Occurred during Test:"+m.CRLF+m.CRLF+LOMAILMAN.LASTERRORTEXT+m.CRLF+m.CRLF+;
						"THIS PROBLEM GENERALLY MEANS THE NAME OF THE SMTP SERVER IS NOT CORRECT, IT IS NOT CURRENTLY RUNNING OR NOT VISIBLE FROM THIS WORKSTATION."+m.CRLF+m.CRLF+;
						"THIS CAN SOMETIMES BE CAUSED BY ENTHUSIASTIC ANTI-VIRUS SOFTWARE - TRYING TO STOP SPAM BEING SENT FROM YOUR PC.",48,"Problem")
				CASE LEFT(ALLTRIM(LOMAILMAN.LASTERRORTEXT),3)= "500"
					MESSAGEBOX("Problem Occurred during Test:"+m.CRLF+m.CRLF+LOMAILMAN.LASTERRORTEXT+m.CRLF+m.CRLF+;
						"THIS PROBLEM GENERALLY MEANS THE SMTP SERVER IS CONFIGURED NOT TO ACCEPT E-MAILS FOR THE SPECIFIED RECIPIENT (OR THEIR DOMAIN [THE XXX.COM BIT]."+m.CRLF+m.CRLF+;
						"YOU WILL PROBABLY NEED TO GET THE SMTP SERVER'S ADMINISTRATOR TO HELP YOU.",48,"Problem")
				CASE LEFT(ALLTRIM(LOMAILMAN.LASTERRORTEXT),3)= "530"
					MESSAGEBOX("Problem Occurred during Test:"+m.CRLF+m.CRLF+LOMAILMAN.LASTERRORTEXT+m.CRLF+m.CRLF+;
						"THIS PROBLEM GENERALLY MEANS THE SMTP SERVER IS CONFIGURED TO EXPECT AUTHENTICATION."+m.CRLF+m.CRLF+;
						"YOU WILL PROBABLY NEED TO GET A USERNAME AND A PASSWORD FROM THE SMTP SERVER'S ADMINISTRATOR.",48,"Problem")
				CASE LEFT(ALLTRIM(LOMAILMAN.LASTERRORTEXT),3)= "535"
					MESSAGEBOX("Problem Occurred during Test:"+m.CRLF+m.CRLF+LOMAILMAN.LASTERRORTEXT+m.CRLF+m.CRLF+;
						"YOU HAVE PROBABLY ENTERED YOUR SMTP SERVER USERNAME OR PASSWORD INCORRECTLY."+m.CRLF+m.CRLF+;
						"YOU SHOULD CHECK THEM - THEY MAY BE CASE SENSITIVE.",48,"Problem",200)
				CASE LEFT(ALLTRIM(LOMAILMAN.LASTERRORTEXT),3)= "550"
					MESSAGEBOX("Problem Occurred during Test:"+m.CRLF+m.CRLF+LOMAILMAN.LASTERRORTEXT+m.CRLF+m.CRLF+;
						"THIS PROBLEM GENERALLY MEANS THE SMTP SERVER IS NOT SET-UP TO ACCEPT E-MAILS FROM THIS WORKSTATION."+m.CRLF+m.CRLF+;
						"YOU WILL PROBABLY NEED TO GET THE SMTP SERVER'S ADMINISTRATOR TO HELP YOU.",48,"Problem")
				OTHERWISE
					_CLIPTEXT=LOMAILMAN.LASTERRORTEXT
					MESSAGEBOX("Problem Occurred during Test:"+m.CRLF+m.CRLF+LOMAILMAN.LASTERRORTEXT,48,"Unexpected Problem")
			ENDCASE
		ENDIF
		LNSUCCESS = LOMAILMAN.CLOSESMTPCONNECTION()
		IF (LNSUCCESS <> 1) THEN
			MESSAGEBOX( "Connection to SMTP server not closed cleanly.",48,"Problem")
		ENDIF

		RELEASE LOEMAIL
	ENDIF
	RELEASE LOMAILMAN
	RETURN(.T.)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Hi Mandy,

There are several ways of sending email, depending on what software is already installed on the client machine and how much money (if any) you are prepared to spend.

You could start by perusing the FAQs in this forum. Click the FAQs link at the top of this page, then use your browser's "search in page" feature to search for "email" or "e-mail". The technique I use is described in Mike Gagnon's faq184-4969. It is fairly simple to implement and does not require any additional software

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
That's an overwhelming information Griff!!!! [bigsmile] Anyway thank you Griff and Mike... My Project is already up and running... just wanted to add sending email feature... but as i see it, its really OMG! That's hard! I think i'm backing out of the feature i want to add.... [bigsmile] You're awesome guys... Thank you so much...
 
Hi Mandy

I have picked up some very beneficial advice from this forum and I've used the below for sometime, adapting it accordingly.

Code:
* The below is intended for use with the the OUTLOOK application

USE MYTABLE SHARED
GO myrecno

oOutlook = Createobject('Outlook.Application') 

oNameSpace = oOutlook.getnamespace('MAPI')
oOutbox = oNameSpace.GetDefaultFolder(4)
oItems = oOutbox.Items

oMailItem = oItems.Add(0)

oMailItem.To = ALLTRIM(EMAIL)
oMailItem.Subject = TRIM(SUBJECT)

oMailItem.Body = "Whatever your message is here"+ ;
  CHR(13)+CHR(13)+"Next line if required"

oMailItem.attachments.add(\myfiles\filename.pdf)
		
oMailItem.ReadReceiptRequested = .T. && Request a READ receipt

* oMailItem.Display  && Display the email before you send it
* oMailItem.Send  && Send the email without viewing it

This is just a suggestion which has worked very well for me.

Best wishes and good luck

Thank you

Steve Williams
VFP9, SP2, Windows 10
 
One of the biggest problems is that Antivirus software looks at this and says - that looks like a spam originator... better block that!

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Mandy, perhaps we should have asked you a bit more about your requirements. In particular, are you mainly interested in sending reports or other documents as attachments? Will the text in the body of your emails be generated automatically by your program, or will it be supplied by the user at the point at which the message is sent? Do you need to integrate the emails with an existing email client such as Outlook? Will you obtain the email addresses of the recipients from an existing address book outside your program, or are they already held in your database?

As you have seen, it is a large subject, and there are different approaches to meet the different requirements.

For what it's worth, here are the methods I have used:

- Mike Gagnon's FAQ, mentioned above. I use this to send invoices from the application. Each email contains some personalised text, generated by the program, with the invoice as an attachment.

- Outlook Automation. I use this to send bulk emails to a large mailing list. I have a form in which the user types the body of the message, specifies the selection criteria for the recipients (e.g. selecting by region or country), and adds any files that are to be included as attachments. The user can then choose to send the message straight away, or to preview it in Outlook. The messages can use any templates within Outlook. I don't have to worry about storing copies of outgoing messages as that is done in Outlook. The disadvantage of this approach is that there is quite a lot of programming involved.

- Finally, if I just want to give the user a button to click on, to send a message to a specific recipient, I use the technique described in my article, An easy way to send email from a Visual FoxPro application. When the user clicks the button, an email composing window opens in the user's default desktop email client (such as Outlook Express or Thunderbird). The To: address is already filled, and the user then adds the Subject and the body of the message, and clicks the Send button. This is trivially easy to program, but puts more work on the user.

I hope the above might help you to decide on the overall direction to take.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mandy,

I know it looks a bit counterintuitive that a third party Mail tool (Chilkat_9_5_0.MailMan) needs so many lines to send a mail. it's the vast options you have with mails in general that's possible, but also optional in many cases.

And the Outlook solution only needs a few lines, but needs an Outlook installation (not Outlook Express or Windows Mail, the Office Suite product).

It's still quite simple to find something that works for you. As Mike said we need to know a bit more. The top level question to ask yourself is whether you want to do everythibg yourself, ie connect to a mailserver and talk to it and send the mail or make use of an installed mail client which handles that ommunication with a mailserver, besides already being configured for it. The former needs a lot more as Griffs code shows and the latter can start with a ShellExecute of a mailto: URL, which can also contain the mail text of short mails. And I think you already learned to use ShellExecute to start another EXE.

Chriss
 
Hi everyone... Mike i just want to send to a piece of info (from the textbox) to the emailadd inputted in the textbox....
 
You still don't answer the question which is the first order major decision, even though it seems secondary.

But assume you don't care how the mail is sent and what you want can be covered with the ShellExecute of a mail URL.


Code:
DECLARE integer ShellExecute IN shell32.dll ;
integer hndWin, string cAction, string cFileName, ;
string cParams, string cDir, integer nShowWin

LOCAL lcEmail

*!* Replace the email addy below with a valid one
lcEmail = "mailto:somewhere@somehost.com?Subject=Testing+ShellExecute+For+Email&Body=Hello+World!"

ShellExecute(0,"open",lcEMail,"","",1)

Code pulled out from faq184-3889.

Instead of the given srtring you can of course add in a mail address from a textbox and a mail text from another one. There's just a limit of how long a URL is allowed to be.

And how mailto links are created is explained in more detail here:
Chriss
 
Thank you so much Chris... i'll try to study this, so that i may add it to my project... God Bless...
 
Hi Mandy,

There are many out there as others said. I know and have used Blat ( ) and Mail Alert Simple Mailer ( )

1. Mail Alert you can even use with Gmail. However, as gmail restrict access from 3rd parties, you will have to enable 3rd party access from the account's control panel. If your requirement is only to send out emails, you can open a Gmail account only for that purpose and use it thereby eliminating any risk with your existing gmail account. By the way, I think there are restrictions in Gmail how many mails you can push out within particular span of time. You will have to check that.

2. Blat, I used to use many years back, but still it is out there active and must be useful to you.

For both of these, their documentation is very well enough to get you through them.

Our friends here who are experts in dealing with mailing, especially SMTP protocol (Blat is on that I think) must be able to help you more on this I hope.

Rajesh
 
Hello Mandy,

you may have a look on :

There is a series how to send email via BLAT / MAPI / OUTLOOK / CDO ,....
Sometimes its partly offline, but its referenced and copied in other sites or on archive.org
Choose wisely :)

To my opinion the easiest way for internal/customers software software maybe outlook, if not available Chilkat.
I prefered BLAT years ago, but some one reported problems with SSL.

And as mentioned there is chilkat Shortened without error handling,... Griff's post is better :)
loMailman = CreateObject('Chilkat_9_5_0.MailMan')
loMailman.SmtpHost = "smtp.chilkatsoft.com"
loMailman.SmtpUsername = "myUsername"
loMailman.SmtpPassword = "myPassword"
loEmail = CreateObject('Chilkat_9_5_0.Email')
loEmail.Subject = "This is a test"
loEmail.Body = "This is a test"
loEmail.From = "Chilkat Support <support@chilkatsoft.com>"
lnSuccess = loEmail.AddTo("Chilkat Admin","admin@chilkatsoft.com")
lnSuccess = loMailman.SendEmail(loEmail)
lnSuccess = loMailman.CloseSmtpConnection()


Best regards
tom

We also did a special solution for Tobit's DAVID, ask if you are using this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top