ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Communication / Managing emails
  • Principle
  • Reading an email (Email variable): Steps to follow
  • Example
  • Reading an email (Email structure): Steps to follow
  • Example
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
Principle
This help page explains how to read an email from a WINDEV application and how to display its content in a WINDEV application.
The email is read by several WLanguage functions (EmailReadFirst, EmailReadNext, ...).
The content of the email can be retrieved:
  • in a variable of type Email. The Email variables allow you to handle several emails simultaneously.
  • via the Email structure. The variables of this structure contain the informations regarding the email read.
The variables of Email structure (as well as the properties of Email variables) correspond to the email characteristics.
Reading an email (Email variable): Steps to follow
The steps for reading an email in WLanguage are as follows:
  1. Use a function for reading emails to browse all the emails of a messaging session, several syntaxes are available:
    • Perform a loop such as:
      MySession is emailPOP3Session
      MyMessage is Email
      EmailReadFirst(MySession, MyMessage)
      WHILE NOT MyMessage.Out
      // Insert the message process
      EmailReadNext(MySession, MyMessage)
      END
    • Retrieve all the messages in an array with EmailGetAll:
      MySession is emailPOP3Session
      MyMessages is array of Email = EmailGetAll(MySession)

      Remarks:
      • When using the POP3 protocol, the incoming emails that can be read are the emails that have been received when the session was started. All the emails received once the session is started are not "visible". To access the new incoming emails, the session must be closed and re-started.
      • To follow the reading progress, use EmailProgressBar.
  2. Read the content of the Email variable.
    Remark: If the email contains specific headers, they can be read via the Email variable.
  3. If the HTML property is not empty: the message is in HTML format. It must be displayed in a browser (see the example below).
    For each attached file:
    • Save the file on disk (EmailSaveAttachment).
    • Browse the HTML message and find the "cID:"+Identifier value of the attached file. Replace this value by the full path of attached file copied on disk.
      Display the message in a browser.
  4. If the HTML property is empty: the message is in text format.
  5. Save the attached files on disk if necessary and display the message text.

Example

MyMessage is Email
...
Temp_Dir is string = "C:\temp\"
CID is string
I is int
// For each attached file
Attachment is emailAttach
FOR EACH Attachment OF MyMessage.Attach
// Copy the file into a temporary directory
EmailSaveAttachment(Attachment, Temp_Dir + Attachment.Name)
// Retrieve the identifier of the attached file
CID ="cid:" + Attachment.Identifier
// Replace the references to the attached file by the real name of the file
MyMessage.HTML = Replace(MyMessage.HTML, CID, ...
"file:" + Temp_Dir + Attachment.Name)
END
// Display the HTML content in an HTML control
HTM_Display = MyMessage.HTML
Reading an email (Email structure): Steps to follow
The steps for reading an email in WLanguage are as follows:
  1. Use a function for reading emails (EmailReadFirst, EmailReadNext, ...). To follow the reading progress, use EmailProgressBar.
  2. Read the content of the email structure (see the example below).
  3. If the Email.HTML variable is not empty: the message is in HTML format. It must be displayed in a browser.
    For each attached file (the Email.NbAttach variable is not empty):
    • Save the file on disk (EmailSaveAttachment).
    • Browse the HTML message and find the "cID:"+Email.AttachIdentifier value of the attached file. Replace this value by the full path of attached file copied on disk.
    • Display the message in a browser.
  4. If the Email.HTML variable is empty: the message is in text format.
  5. Save the attached files on disk if necessary and display the message text.
Remark: When using the POP3 protocol, the incoming emails that can be read are the emails that have been received when the session was started. All the emails received once the session is started are not "visible". To access the new incoming emails, the session must be closed and re-started.

Example

Temp_Dir is string = "C:\temp\"
CID is string
I is int
 
// For each attached file
FOR I = 1 TO Email.NbAttach
// Copy the file into a temporary directory
EmailSaveAttachment(Email.Attach[I], ...
Temp_Dir + Email.Attach[I])
// Retrieve the identifier of the attached file
CID = "cid:" + Email.AttachIdentifier[I]
// Replace the references to the attached file
// by the real name of the file
Email.HTML = Replace(Email.HTML, CID, "file:" + ...
Temp_Dir + Email.Attach[I])
END
 
// Display the HTML content in a Web browser
// Create a temporary file containing the HTML
FileName is string = Temp_Dir + "temp.htm"
hFile is int = fOpen(FileName, foCreate + foWrite)
fWrite(hFile, Email.HTML, Size(Email.HTML))
fClose(hFile)
 
// Supply the temporary file to the browser
// Web_Browser is an ActiveX control "Microsoft Web Browser"
Web_browser>>Navigate(FileName)
Related Examples:
WD Mail Complete examples (WINDEV): WD Mail
[ + ] This application is an email client developed in WINDEV. It is based on the Email objects.
This email client is used to retrieve and send emails by using the POP, IMAP and SMTP protocols.
You have the ability to apply filters to the incoming emails.

The application can also be used to manage several email accounts. The writing of an email is based on the HTML edit control.
The POP3 Email functions Unit examples (WINDEV): The POP3 Email functions
[ + ] Using the Email functions to manage the POP3 protocol.
This protocol is used to retrieve emails from a server.
WD JavaMail Training (WINDEV): WD JavaMail
[ + ] This example is a Java example used to read and send emails.
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 06/22/2023

Send a report | Local help