Using SMTP

To use SMTP, you create an SMTPClient object, and then use the SMTPClient functions to set the email content, the email sender, etc., and then send the email. For complete information about the SMTPClient object and the MimeMessage object, see SMTPClient in Objects and Controls and MimeMessage in Objects and Controls.

Examples

Example 1: This example provides the most basic email-sending function.

Integer li_rc
SMTPClient  lnv_SmtpClient

lnv_SmtpClient = CREATE SMTPClient

//Sets the email-sender information       
lnv_SmtpClient.Host = "smtp.testmail.com"
lnv_SmtpClient.Port = 25
lnv_SmtpClient.Username = "tester001@testmail.com"
lnv_SmtpClient.password = "Mypassword001"
lnv_SmtpClient.EnableTLS = False

//Sets the email message
lnv_SmtpClient.Message.SetSender("tester001@testmail.com","Tester001")
lnv_SmtpClient.Message.AddRecipient("tester002@testmail.com")
lnv_SmtpClient.Message.Subject = "SMTPClient Test Message"
lnv_SmtpClient.Message.TextBody = "SMTPClient example message body"

//Sends the email
li_rc = lnv_SmtpClient.Send()

IF li_rc = 1 THEN
                Messagebox('SMTPClient','Mail sent successfully')
ELSE
                Messagebox('SMTPClient' ,'Email sending failed. Return ' + String(li_rc) + '.', StopSign!)
END IF

DESTROY lnv_SmtpClient

Example 2: This example sends an HTML-formatted email with the Cc, attachment, priority, encoding and proxy settings. In addition, the EnableTLS option is set to true, and the logging feature is enabled.

Integer li_rc
String ls_linkedResources, ls_contentID,ls_HTML
SMTPClient lnv_SmtpClient

lnv_SmtpClient = CREATE SMTPClient

//Sets the email-sender information     
lnv_SmtpClient.Host = "smtp.gmail.com"
lnv_SmtpClient.Port = 587
lnv_SmtpClient.Username = "tester001@gmail.com"
lnv_SmtpClient.password = "Mypassword001"
lnv_SmtpClient.EnableTLS = True
lnv_SmtpClient.SetProxy("192.168.168.10",8004)
lnv_SmtpClient.LogFile("c:\mail.log")

//Sets the email message
lnv_SmtpClient.Message.SetSender("tester001@gmail.com","Tester001")
lnv_SmtpClient.Message.AddRecipient("tester002@gmail.com")
lnv_SmtpClient.Message.AddCc("CcTester1@gmail.com" )
lnv_SmtpClient.Message.AddAttachment("c:\image\test01.png")
lnv_SmtpClient.Message.Subject = "SMTPClient Test Message"
lnv_SmtpClient.Message.Priority = 2 //High
lnv_SmtpClient.Message.Encoding = "UTF-8"

//Sets the HTML content to be sent
ls_linkedResources = "c:\\image\\example.jpg"
ls_HTML =  &
"<html><body>" + "~r~n" + &
"<p>This is the inline " + "~r~n" + &
"<b>HTML</b> message of the e-mail.</p><br />" + "~r~n" + &
"</body></html>"
lnv_SmtpClient.Message.HTMLBody = ls_HTML

//Sends the email
li_rc = lnv_SmtpClient.Send()

IF li_rc = 1 THEN
                Messagebox('SMTPClient','Mail sent successfully')
ELSE
                Messagebox('SMTPClient' ,'Email sending failed. Return ' + String(li_rc) + '.', StopSign!)
END IF

DESTROY lnv_SmtpClient

Example 3: The example sends an email with OAuth2 authentication. For how to properly acquire the OAuth2 access token, refer to TokenRequest in Objects and Controls.

Integer li_rc
SMTPClient  lnv_SmtpClient

lnv_SmtpClient = CREATE SMTPClient

//Sets the email message
lnv_SmtpClient.Message.SetSender("tester001.appeon@gmail.com","Tester001")
lnv_SmtpClient.Message.AddRecipient("tester002.appeon@gmail.com")
lnv_SmtpClient.Message.Subject = "SMTPClient Test Message"
lnv_SmtpClient.Message.TextBody = "SMTPClient example message body"

//Sets the email-sender information        
lnv_SmtpClient.Host = "ssmtp.gmail.com"
lnv_SmtpClient.Port = 587
lnv_SmtpClient.EnableTLS = True
lnv_SmtpClient.Username = "tester001.appeon@gmail.com"

//Model Authentication
lnv_SmtpClient.XOAuth2AccessToken = "ya29.a0AVvZVsrZY8ra_fyIw3EyVSYUQIXYbWcz4_mYqlqtxFE-4t_hzLyV_BaZbaxhM3nC10Nr5Qm6hmHPBQvd5BfaH7kXN5KxS-XR0dnV-1EHltUJmcgBSZIHbPEkR6qLf_CEaEozOESSyebgAWv5SGynKfyCzsvR6QaCgYKAeMSARASFQGbdwaIy4ayfJPAa23BKltg4-AJjg0165"

//Sends the email
li_rc = lnv_SmtpClient.Send()

IF li_rc = 1 THEN
                Messagebox('SMTPClient','Mail sent successfully')
ELSE
                Messagebox('SMTPClient' ,'Email sending failed. Return ' + String(li_rc) + '.', StopSign!)
END IF

DESTROY lnv_SmtpClient

Example 5: The example sends the email asynchronously. Note: You can obtain the result of the async call in the OnSendFinished event.

Long ll_handle
n_smtpclient inv_SmtpClient

inv_SmtpClient = CREATE n_smtpclient

//Sets the email-sender information        
inv_SmtpClient.Host = "smtp.gmail.com"
inv_SmtpClient.Port = 587
inv_SmtpClient.Username = "tester001.appeon@gmail.com"
inv_SmtpClient.password = "Mypassword001"
inv_SmtpClient.EnableTLS = True

//Sets the email message
inv_SmtpClient.Message.SetSender("tester001.appeon@gmail.com","Tester001")
inv_SmtpClient.Message.AddRecipient("tester002.appeon@gmail.com")
inv_SmtpClient.Message.Subject = "SMTPClient Test Message"
inv_SmtpClient.Message.TextBody = "SMTPClient example message body"

//Sends the email asynchronously
ll_handle = inv_SmtpClient.SendAsync()

...
DESTROY inv_SmtpClient

//Check the OnSendFinished EVENT for the email-sending result