Preface
With the development of computer networks, the time for information transmission between people has been greatly shortened. Many files are transmitted in the form of e-mail; usually people who have used computers will use E-Mail to transmit information more or less. This article will introduce how to use Microsoft MAPI (Messaging application PRogram Interface) to design a Mail program. First, use the MAPIsessioncontrol and MAPIMessagecontrol provided by Visual Basic to design the Mail sending and receiving program, and then use the E-Mail program provided by Microsoft Internet Explorer 3.0 to test the correctness of the program design.
Introduction to MAPI
The MAPI interface is a series of programming interfaces provided by Microsoft for users to develop Mail, Scheduling, bulletinboard, and communication programs. When using MAPI to design a program, you must first establish one or several Sessions between the program and MAPI; after the Session is established, the client program can use the functions provided by MAPI.
The functions of MAPI are mainly divided into three parts: AddressBooks, Transport and MessageStore. AddressBooks is mainly responsible for setting parameters such as E-mailtype and protocol; Transport is responsible for functions such as sending and receiving files; MessageStore is responsible for processing information such as sending and receiving.
Having said all that, the MAPI interface seems scary. But it doesn't matter. The MAPISessioncontrol and MAPIMessagecontrol provided by Visual Basic have packaged many complex parts into simple properties and functions. We only need to make some simple settings for the properties and functions to write a program for sending and receiving mail.
Below, the author will use Visual Basic's MAPIcontrol to design the program for mail transmission and reception.
E-mail sending part
Step1: Set Exchange parameters
Enter the Exchange system and select New Settings File; the required information services will be displayed on the screen, select InternetMail. Set the name of the settings file to test. The screen will display two options, you can choose to connect via modem or network. The author is using an Internet dedicated line, so I choose network; if you are using PPP dial-up to connect to the Internet, you must choose modem. After selecting network, Exchange will ask us to enter the ipaddress of the mailserver. The Mailserver address I am currently using is 202.96.128.111, and it is set to this address in this program. Then set the mode of Transformmessage to automatic, so that when we connect to Mailserver, new information will be automatically downloaded to local
end.
Next, set the Emailaddress, fullname, passWord, and downloadpath you are using step by step. This completes the Exchange
Parameter settings.
Step2: Programming
Select NewProject in the File option to create a new project. At this time, you will see many options on the screen, such as ActiveXDocument.EXE, ActiveXDocument.DLL, ActiveX.EXE, ActiveXControl, etc... At this time, you must select Standard.EXE.
Under Project, select the Property attribute option; under the general option, change ProjectName to email-send, and change the Form name and Caption to email-send
Add a MAPImessagecontrol to the Form on the screen and name it MAPIm1; add a MAPIsessioncontrol and name it MAPIs1; add three textboxcontrols and name them subject, content, and addr. And add a Label in front of each of the three textboxes, and change the Captions to subject, content, and addr respectively.
In order to facilitate readers to understand the relationship between each other, the following table explains it:
(TextBox)Name
subject (enter the title of the letter)
content (enter the content of the letter)
addr (enter the address to which the letter is to be sent)
-------------------------------------------------- ----------------------------------
(Label)Caption
subject(Enter letter title prompt)
content (prompt for entering the content of the letter)
addr (enter the address prompt for the letter to be sent)
Set the various properties of MAPIm1 as follows:
downloadmail=TRUE
logoUI=TRUE
newsession=FALSE
username="test"
Here downloadmail is set to TRUE. When the program and mailserver connect for the first time, the new mail will be downloaded to the local end. Set logoUI to TRUE, then when you enter an incorrect logo name in your program, the system will display a MessageBox to let you enter the correct name. Since this program only uses one session, we set its value to FALSE. Of course! You can also set it to TRUE if you have many sessions to establish. What is filled in username is the name of the settings file we added in Exchange; if there is no content, the system will display some message boxes asking you to enter the file. Add three Buttoncontrols to the Form on the screen, logon, logoff, and send, and change the Caption to logon, logoff, and send respectively.
(Button)
NameCaption
logon(execute login command)logon
logoff (execute logout command) logoff
send (execute outgoing mail command) send
Add the following program code to logon_Click(). This action allows us to log in to the mailserver.
MAPIS1.SignOn
MAPIM1.SessionID=MS1.SessionID
MsgBox"youridis" Str(MS1.SessionID)
MS1.SignOn is a login action. When logging in, because the downloadmail attribute of MAPIm1control has been set to TRUE, you can see the messagebox on the screen showing that the system is downloading mail. After successful login, the system will return a session ID and fill in the ID into the Session ID of MAPIM1, so that this session can be used to send E-mail.
Add the following program code to logoff_Click(). This action causes us to leave the mailserver.
MAPIS1.SignOff
Add the following program code to send_Click(). This action allows us to send an email.
MAPIM1.Compose
MAPIM1.RecipDisplayName=addr.text
MAPIM1.AddressResolveUI=True
MAPIM1.MsgSubject=subject.text
MAPIM1.MsgNoteText=content.text
MAPIM1.Send
The main purpose of the MsgBox "yousuccessful" Compose command is to enable you to change the content of RecipDisplayName, fill in the email address, subject, and article content you want to send into RecipDisplayName, MsgSubject, and MsgNoteText respectively, and then use the send command to send it.
Step3: Test the program with IE3.0Mail program
Execute the email-send program designed above. Fill in "Sending Test" in the subjecttextbox, fill in [email protected] in the addresstextbox, enter the content to be sent in the contenttextbox: "Hello everyone! This is a sending test", and then press Send. The letter is sent!
Run IE3.0 to choose to receive mail, and select readmail in the mail option. We will see a letter titled "Sending Test" in the inbox, and then we will take a look at its content, yes! That's the test letter we just sent.
E-mail receiving part
Step1: Programming
Select NewProject in the File option to create a new project. You will see many options on the screen, such as ActiveXDocument.EXE, ActiveXDocument.DLL, ActiveX.EXE, ActiveXControl, etc. At this time, you should select Standard.EXE.
Then under Project, select the Property attribute option; under the general option, change ProjectName to email-get, and change the FORM name and Caption to email-get.
Add a MAPI message control to the Form on the screen and name it MAPIm
1; Add a MAPIsessioncontrol, named MAPIs1; add three Textboxcontrols, named subject, content, indexno respectively, change their Caption to mail-subject, mail-content, indexno; and add a Labelcontrol in front of the TextBox. Change the Captions to subject, content, and indexno respectively.
Set the various properties of MAPIm1 as follows:
downloadmail=TRUE
logoUI=TRUE
newsession=FALSE
username="test"
The principle of these settings is the same as designing the sending mail program part. Next, add a Buttoncontrolget-mail to the Form on the screen and change the Caption to getmail. Just like the sending mail program part, a session is established with the mailserver.
Add the following program code to get-mail_Click(). This action makes me
We receive emails.
MAPIM1.Fetch
Form1.Caption=MAPIM1.MsgCount
MAPIM1.MsgIndex=CINT(indexno.text)
subjec.Text=MM1.MsgNoteText
content.Text=MAPI1.MsgSubject
The Fetch command captures letters into the inbuffer of the system memory. After we capture the letters, we can know the number of letters through MsgCountproperty. Then you can use MsgIndex to set the content and title of which letter you want to see.
Stept2: Test the mail receiving program
First, use any mail program to write any number of letters to the mailserver. In the Caption of Form1, we will see the number of letters received. Execute the email-get program designed above and fill in 1 in the indexnotextbox. Then press the getmailButton, so that the content and title of the letter will be displayed in the two TextBoxes of content and subject. Congratulations on your success! ->