ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Communication / Managing emails
  • Overview of "Simple MAPI"
  • Principle
  • Details of different steps
  • Step 1: Creating a user profile
  • Step 2: Starting an email session
  • Step 3: Sending emails
  • Step 3 bis: Reading the emails
  • Step 4: Closing the email session
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
Overview of "Simple MAPI"
Simple MAPI is used to simplify the management of the emails received at the hosting company. When an email is read, it is automatically loaded in the local message box and deleted from the server (at the hosting company).
All the characteristics required to manage the emails (POP3 protocol, SMTP protocol, remote access, etc.) are grouped in the "User Profile".
With the email functions of WLanguage, a WINDEV application or a WEBDEV site can directly handle the emails managed in an application that is using "Simple MAPI".
Principle
To send or read messages via Simple MAPI, you must:
  1. Describe a user profile. This user profile must be created in the Microsoft application for email management (MS Exchange client for example).
  2. From the WINDEV application (or from the WEBDEV site), connect to the application for email management (MS Exchange client for example) with EmailStartSession.
  3. Send and read the messages.
  4. Close the session to the application for email management (MS Exchange client for example) with EmailCloseSession.
Details of different steps

Step 1: Creating a user profile

The user profile is used to configure the application for email management (MS Exchange client for example).
The following elements are defined in the user profile:
  • the SMTP protocol used,
  • the POP3 protocol used,
  • the different communication services used. To use the "email" functions of WLanguage, the user profile must use the Microsoft Exchange Server.
Remark: A profile must be created for each different user or email account. The profile name will be used to start the email session with EmailStartSession.
To create a profile:
  1. Open the control panel.
  2. Double-click the "Email" option.
  3. Click the "Display the profiles" button.
  4. In the window named "Choosing a profile", click the "Add" button.
  5. Give a name to the profile. This name will be used in the WINDEV programs.
  6. Select "Add a new email account".
  7. Select the "Microsoft Exchange Server" service.
  8. Enter the name of Microsoft Exchange server.

Step 2: Starting an email session

To start an email session, use EmailStartSession. This function must be the first "email" function used in your WINDEV application (or in your WEBDEV site).
EmailStartSession returns the session identifier. This identifier will be used by all the WLanguage functions for email management.
Example: The following procedure is used to start an email session from a profile. If an error occurs, the Email.Error variable is used to identify the error.
FUNCTION StartSession(Profile)
// Start the session
SessionNum is int
SessionNum = EmailStartSession(Profile)
IF SessionNum = 0 THEN
Error("The session cannot be started. Error: " + Email.Error)
END
RESULT SessionNum

Step 3: Sending emails

The following functions are use to send emails via Simple MAPI:
  • EmailSendMessage: this function is used to place the message in the out-box of the application for email management (out-box of the MS Exchange client for example).
  • EmailUpdate: this function is used to synchronize the email server and the application for email management: the new incoming emails are automatically transferred into the in-box, the emails found in the out-box are sent.
For more details, see: Preparing to send an email.
Example: The following code sends all the emails in a Table control populated programmatically ("TABLE_TOSEND") via the MS Exchange Client. Each table row corresponds to an email.
For each email, the information in the Table control is transferred to the email structure, and the email is sent. Then, the email server is updated.
I is int
FOR I = 1 _TO_ TableCount(TABLE_TOSEND)
// The email is sent to a single person
Email.NbRecipient = 1
Email.Recipient[1] = ExtractString(TABLE_TOSEND[I], 1)
// Subject and message
Email.Subject = ExtractString(TABLE_TOSEND[I], 2)
Email.Message = ExtractString(TABLE_TOSEND[I], 3)
// No attached file
Email.NbAttach = 0
// Send the message to MS Exchange
EmailSendMessage(SessionNum, False)
END
// Send the messages from MS Exchange to the email server
IF NOT EmailUpdate(SessionNum) THEN
Error("Problem with MS Exchange. Error:" + Email.Error)
END

Step 3 bis: Reading the emails

Reading the emails via Simple MAPI is performed by:
  • the EmailUpdate function: this function is used to synchronize the email server and the software for email management: the new incoming emails are automatically transferred into the in-box, the emails found in the out-box are sent.
  • the functions for reading emails (EmailReadFirst, EmailReadNext, etc.): these functions are used to initialize the email structure of WLanguage with the characteristics of the email currently read (sender, subject, etc.).
For more details, see: Reading an email.
Example: The following code is used to read the emails. Incoming emails are stored in a Table control populated programmatically ("TABLE_Messages"). The SessionNum variable corresponds to the identifier of the session. In this example, the incoming messages are deleted from the inbox and from the email server by EmailDeleteMessage.
// Receives the pending messages found on the email server
IF NOT EmailUpdate(SessionNum) THEN
Error("Problem with MS Exchange. Error: " + Email.Error)
END
 
// Read the first unread message
IF NOT EmailReadFirst(SessionNum, "NOT READ") THEN
Error("Error while reading the first message")
END
 
// Loop through unread messages and display them in a Table control populated programmatically
TableDeleteAll(TABLE_Messages)
WHILE NOT Email.Out
// The reception date, the address of the sender and the message
// are assigned to the table
TableAdd("TABLE_Messages", Email.ReceiveDate + TAB +...
Email.SenderAddress + TAB + Email.Message)
 
// Delete the message
IF NOT EmailDeleteMessage(SessionNum) THEN
Error("Error during the deletion. The message was not deleted")
END
// Read the next unread message
IF NOT EmailReadNext(SessionNum, "NOT READ") THEN
Error("Error while reading the next message")
END
END

Step 4: Closing the email session

Once the management of incoming and outgoing emails is completed, the session is closed with EmailCloseSession. This function must be the last "email" function used.
Example: The following code is a procedure used to close an email session. In this code, the SessionNum variable corresponds to the session identifier returned by EmailStartSession.
PRODEDURE CloseSession(SessionNum)
// Close the session
EmailCloseSession(SessionNum)
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 05/26/2022

Send a report | Local help