Due to the masking of powerful database functions, the email function of PB is very knowledgeable. The CDO control can be easily used in VB (using NMSMTP in Delphi), and the mail function in the PB is more flexible and convenient than they. PB has won the favor of the majority of programmers with its unique and efficient database access technology. From the surface, it seems to be only suitable for developing database applications, in fact, PB has developed many internal functions and data structures, which has developed many internal functions and data structures for support for email, so it is also a quite good Mail application development tool. The basic transaction to be processed by an email application mainly includes: logging in to the mail server to start the session, sending a message, and ending the mail session. In PB, all of these transactions are expanded around the Mailsession object. Therefore, when programming, you need to define an instance variable of this type in the main window: mailsession mymail then initialize it in the Open event: MyMail = Create Mailsession
1. Log in to the mail server
After initialization, Mymail will be connected to a mail server with the maillogon command: MyMail.maillogon ('JQ75', 'Alexander', MailneWSessionWithDownload!) The first two parameters are user names and passwords, if they are ignored, running A registration dialog will appear. The last parameter represents a new message session and downloads the server on the server to his inbox.
figure 1
2. Read mail
User information After verification on the server, a valid mail session is established, and the job to do is to receive an email. PB describes an email with a MailMessage object, which encapsulates information such as the subject, address, message body, and attachment. Figure 1 is a reading interface of the mail application, which lists all messages in the inbox, you can choose one of them. The code for the main window Open event is:
... // Connect to Mail Server MyMail.mailgetMail INUM = UpperBound (MyMail.MessageId []) // Get the number of messages in the inbox for i = 1 To Inum // reads each email in the inbox and adds its topics to the list box MyMail.mailReadMessage (MyMail.MAssageID!, true) Tab-1.Tabpage-1. PLB-1.Additem (msg.subject, 1) Next // MSG Adds the following code to the mailMessage type instance variable in the SelectionChange Event of the PLB-1 control: MyMail.MailReadMessage (MyMail.MailEndireMessage! , TRUE) SLE-1.Text = msg.subject // Displays the current message SLE-2.Text = msg. Recipient [1] .address // Display sender address SLE-3.Text = msg.dateReceD / / Display Recipient Time MLE-1.Text = MSG.NOTEXT // Display Message IattachmentFileNum = UpperBound (msg.attachmentFile "// Gets the number of attachments for the current message for i = 1 to attachmentFileNum StratTfile = StratTfile MSG. AttachmentFile [i] .pathname "~ r ~ n" Next // strattfile is added to the Clicked event of the String type in the Clicked event of the Attachment button:
MessageBox ("Mail Accessories", StratTfile // Displays the attachment of the current message and its location stored on the hard disk
3. send email
Figure 2 is a send interface of the Mail application that receives the recipient address, mail topic, message body, and attachments, used to populate a mailMessage type object MSG, and then send it. The Add Accessory button is used to select disk files, and its Clicked event code is:
figure 2
String Docname, Named Integer Value Value = GetFileOpenName ("Select Accessories", DOCNAME, NAMED, "DOC", "All Files (*. *), *. *") If value = 1 THEN PLB-2.Additem (DOCNAME, 1) // DOCNAME must contain a complete path information "Delete Accessories" button to remove the added file, its Clicked event code is: Ind = PLB-2.SelectedIndex () if Ind <> - 1 THEN PLB- 2. DeleteItem (IND) "Send" button ClickED event code is: msg.subject = SLE_4.TEXT // Mail Topic msg.notetext = msg.Recipient [1] .recipientType = mailto ! // Specify the recipient type to mailto! Msg.recipient [1] .address = 'smtp:' SLE-5.Text // Recipient address, you must add SMTP protocol for i = 1 to PLB-2 . TOTALITEMS () // Put the selected files in the attachment msg.attachmentfile [i] .filetype = mailattach! // Specify the type of attachment I msg.attachmentFile [i] .pathname = PLB-2.Text (i) / / Annex I's full file name NEXT MyMail.mailsend (MSG) // Send Mail 4. End email session
Add the following code to the CLICKED event of the "Exit" button:
MyMail.maillogoff () // End Mail Session Destroy MyMail // Destroy Session Object
At this point, a simple mail application is comparable, although it is not enough to compare with Foxmail, Outlook, but has its most basic feature. The programs in the example are touched in PB6.5, and most of the code can be utilized.