Send email with an accessory with VFP

xiaoxiao2021-03-06  87

Email Doug Hennig with VFP is available for a class that can be used to send E-mail from the application. It combines two MAPI ActiveX controls to an easy-to-use class.

Today, many users want to send E-mail in their applications. For example, a field may have a field in a typical commercial application to save the customer's E-mail address. In this application There is no fast, no need to switch to Outlook Express and copy and paste their E-mail addresses (such as buttons or menu items on the form or toolbar or menu item) to send an e-mail to the customer is a shame. Double shame is because adding the function to the VFP application so easy. This article explores some classes available to send E-mail from your application.

Mapi ActiveX Controls

The easiest way to use the application can send E-mail is to use the VFP's own MAPI ActiveX control. There are two related controls in the file msmapi32.ocx: MapIMESSAGES and MAPISESSION. These controls work together -Mapisession control Manage the session period, the MAPIMESSAGES control is used to send and receive information. The two controls are non-visual. You can drag and drop them into the form or instance them with code CreateObject (these controls are Msmapi.Mapimsses and Msmapi.maPisession).

The properties, events, and methods (PEMs) of these controls are described in helping file mapi98.chm (located in the / windows / help directory in my system). Instead of how to use these controls, we will explore two combinations I created. Controls to a container class for easy-to-use objects. We will understand how ActiveX PEM is used in the code we are exploring. In addition, although these controls can send and receive mail, this article will only focus on sending information.

Sfmapi

In order to hide the complex processing of two controls, I created the SFMAPI class (in sfmapi.vcx). SFMAPI is based on the SfContainer container class, and our container base class is defined in sfCtrls.vcx. It includes an MAPIMESSAGES control (name For OMAPIMessage in sfmapi) and a mapising control (OMAPISession). I set up the Visible property of the container as .f. (Because it is a non-visible control at runtime) and adds a series of properties: Aattachments, one Protected arrays for saving and information-related file names; Arecipients, protected arrays for a recipient information; CMessage, information to send; csubject, information topic; lloggedin, a protected property, if success Log in to MAPI, its value is .t .; and lsignoffAfterSend, if .t. Means SFMAPI will stop the join MAPI session period after sending information.

To send a message with SFMAPI, start with the NewMessage method. This information simply initializes the inlet for the simple empty entry; it only needs to send the information before you used to send information. Second, for each Accept Call the AddRecipient method once, passing the E-mail address and optional recipient name and category (1 is the primary recipient, 2 is the copy recipient, 3 is a BLIND replication recipient). This method is confident that the Arecipients array is available After adding information to the Arecipients array:

LParameters tcaddress,;

Tcname,;

TNTYPE

Local lcname,;

LNTYPE,;

Lncount

Assert Vartype (Tcaddress) = 'c' and;

NOT EMPTY (TCADDRESS);

Message 'Invalid Address Specified'

LCNAME = IIF (Type ('tcname') <> 'C' or EMPTY (TCNAME), TCADDRESS, TCNAME

LNTYPE = IIF (Type ('TNTYPE') <> 'N' OR;

Not Between (TNTYPE, 1, 3), 1, TNTYPE

With THIS

Lncount = IIF (Empty (.arecipients [1, 1]), 1,;

Alen (.arecipients, 1) 1)

Dimension .arecipients [lncount, alen (.arecipients, 2)]

.arecipients [lncount, 1] = lcname

.arecipients [lncount, 2] = tcaddress

.arecipients [lncount, 3] = LNTYPE

ENDWITH

If the information has any attachments, call the AddAtTachment method, which is similar to addRecipient but adds the specified file name to aattachments array.

Finally, set the csubject attribute to the information theme, set the cMessage as the information body, and call the Send method. If you want to display the dialog, you must do this when you call this method from the VFP 5, you must do this to avoid one " Unavailable calls "fault (this error does not appear in VFP 6).

We will explore the code of the Send method. When it starts to make at least one recipient, then if you haven't logged in to MAPI, call the Signon method (we will see it immediately). If you fail, send returns .f.

Lparameters TldisplayDialog

Local LNI

With THIS

* Confident there is at least one recipient.

askERT NOT Empty (.arecipients [1, 1]);

Message 'sfmapi.send: no recipients'

* If there is no login yet, try to log in. If you cannot log in, return .f.

if not .lloggedin and not .signon ()

Return .f.

ENDIF

Next, it sets the SESSIONID attribute of the MAPIMESSAGES object to the property value of the mapisession object, which can communicate with the MAPI; this is similar to the file handle when opening a file with the fund () function. It calls Mapimestage's compose method to start a new Information and set the MsgnoteText and MsgSubject properties for its CMessage and CSUBject values.

.omapimssis.SessionId = .omaPission.SessionID

.omapimssis.compose ()

.omapimessage.msgnotext = .cmessage

.omapimassage.msgsubject = .csubject

The next step is to tell the Mapimestages object About the recipient. In an internal, recipient collection in the bizarre manner: specified by the Recipindex property, bind to the current internal recipient People. You set the recipher index for you want to handle (index is 0-based, so the first recipient index is 0), and the appropriate mapimsses property to read and write to selected Recipient. To add a new recipient, set RecipIndex as a value greater than the current recipient (in fact, equal to the number of recipients, because the index is based on 0), it is saved in the RecipCount property In this way, do this automatically increase RecipCount. We are interested, with the recipient-related attribute is RecipdisplayName (display to the user's recipient name), RecipAddress (E-mail address), and RecipType (Recipheal, I The range value discussed earlier). After setting these properties, call the Resolvename method to decompose the name from the address thin, the recipient information to the code of the MapiMessages object traversed the Arecipients array, setting the recipient Add another recipient and new recipient's appropriate properties.

For lnu = 1 to alen (.arecipients, 1)

.omapimesses.recipindex =;

.omapiMASsage.recipcount

.omapimedage.recipdisplayname = .arecipients [lni, 1]

.omapimAssage.recipaddress = .arecipients [lnu, 2]

.omapimAssage.reciptype = .arecipients [lni, 3]

.omapimessage.resolvename ()

Next LNI

The file processing in the attachment is very simple: AttachmentIndex property indicates which internal attachment to send, and attachmentPosition, attachmentpathname and attachmentName properties are binding to the current attachment. AttachmentPathname (full name and path of attachments) and attachmentname (Recipient will The name of the attachment that is seen is easy to understand, but attachmentPosition is bizarre: It indicates that the attachment will be in the information. I can't know why the attachment should contact the information body, but because there is no two attachments. In the same location, and there is no attachment can be placed away from the tail, I decided to place them to the beginning of the information (position is based on 0; "therefore is 1) in the following code).

For lnu = 1 to alen (.aattachments)

If not empty (.aattachments [lni])

.omapimensage.attachmentIndex =;

.omapimessesage.attachmentcount

.omapimensage.attachmentPosition = lni - 1

.omapimassage.attachmentPathname = .aattachments [lni]

.omapimensage.attementname =;

Justfname (.aattachments [lni])

Endif Not Empty (.aattachments [lni])

Next LNI

Finally, we need to call the SEND method of the Mapimestages object to send information. If we assume a dialog (as I am previously mentioned, you must do this in VFP 5), transfer value 1 to send. Then we call Signoff methods (Assume that the message is sent after the message is sent. You may think that the MSGSent property value that returns MAPIMESSAGE is meaningful (it indicates whether the message is successfully sent to the mail server; but it cannot indicate whether the mail will succeed from the mail server) but For some reasons, for me, this property is always returned .F..IF TLDISPLAYDIALOGOG

.omapimessage.send (1)

Else

.omapimessage.send ()

Endif TldisPlayDialog

if .lsignoffafoft

. Signoff ()

Endif .lsignoffaftersend

ENDWITH

Protected Signon method, if we have not logged in to MAPI to log in to MAPI from the Send method. It first ensures that Mapi32.dll can be found in the Windows System Directory (the GetSystemDirectory API function is used to locate the directory), then Set the DownloadMail property of the mapisession object to .f. (At this point we just want to send mail) and logonui properties are .f. (If we want to display a login dialog to allow users to enter their names and passwords, set this property to .t .). It calls the Mapisession Signon method to perform login to MAPI, then set the sfmapi's LLoggedIn property as .t. (If the mapiseness is greater than 0). If the login is successful, the method returns .t ..

Local lcdirectory,;

lnlen

With THIS

* If the DLL is not found, it is not necessary to try to log in to MAPI.

Declare Integer GetsystemDirectory in Win32API;

String @, INTEGER

LcDirectory = Replicate (CHR (0), 80)

Lnlen = GetSystemDirectory (@lcdirectory, 80)

LcDirectory = addbs (Left (LCDIRECTORY, LNLEN))

* You can find it, so set some properties and try to log in.

IF file (LcDirectory Mapi32.dll ')

.omapisession.downloadmail = .f.

.omapisession.logonui = .f.

.omapission.signon ()

.lloggedin = .omapisession.SessionID> 0

Else

MessageBox ('Cannot Find Mapi32.dll')

.lloggedin = .f.

Endif Not File (LcDirectory Mapi32.dll ')

ENDWITH

Return this.lloggedin

Protected signoff method, call from the Send, Destroy method, and Error method when the lsignoffafoft attribute is. That is called to hang up the MAPI and set the LLoggedIn property as .f.

With this.omapisession

IF.SessionID <> 0

. Signoff ()

Endif.SessionId <> 0

ENDWITH

This.lloggedin = .f.

Example form

Testmapi.scx is a form that demonstrates sfmapi. It looks like a typical customer data input form, with a text box such as name, address. It also has a text box that enters the client's E-mail address. This text box The front label is black, showing it to the user) and has the following code in its DBLClick method: with thisform.comApi

.Addrecipient (Alltrim (THISFORM.TXTEMAIL.VALUE))

.Send (.t.)

ENDWITH

When you double-click the tag, these code adds the value in the E-mail text box as the recipient and calls the SFMAPI control to send parameters .t. So a dialog is displayed. Note that the csubject and cMessage properties are not set; After all, we don't know how users want to set these values, so they can enter the required information in the dialog box shown.

You can provide users with other methods to send an E-mail: a command button next to the e-mail text box, a button on a form or the toolbar, a menu item, and more. The well-known VFP head Markus Egger created A smart mechanism is part of Visual Landpro (Visual Landpro is a helpful application, has been selected as a two-year VFP Excellent Awards final program). It creates a display E-mail address For a hot-connected text box (that is, it shows a blue underline text in the white background). User click it, just like they are a hot connection, to send a message to the address. About him The great thing in the class is that you don't even have to explain how it works to large users.

in conclusion

Since adding E-mail capability to an application simple to drag an SFMAPI object to the form, set its properties, and call some methods when the user pointed out how they want to send an E-mail, there is no reason to add this ability to It is afraid that it is the simplest application.

Next month, we will explore some reusable tools that send mailing lists to a group of people: they offer most mail list foundations (no, I am not lunch, there are many legitimate reasons to send the same mail to a set People). When I went, I hope you like those classes.

转载请注明原文地址:https://www.9cbs.com/read-122887.html

New Post(0)