The full source for this article is available here.
It is very easy to send email with ASNA Visual RPG. The core enablers are baked into the .NET Framework in these two namespaces:
- System.Net.Mail.SmtpClient
- System.Net.Mail.MailMessage
The documentation for the
System.Net.Mail.SmtpClient
class says that because this class “doesn’t support many modern protocols” it is deprecated and that you should use the Nuget MailKit package to send emails. However, theSystem.Net.Mail.SmtpClient
has worked fine for me for a long time with the SMTP services I use. It appears to be obsolete only when used with Xamarin. Read more about this issue here.
The code is at this GitHub repo and provides everything you need to be sending emails quickly. You can also read the fully annotated code here.
SMTP configuration
The project needs an App.config
file that provides your SMTP server configuration details. An App.config.sample
file is included to use as a base for your tailored App.config
file.
It needs:
- The name or IP address or your SMTP server
- The SMTP port (which is almost always 587)
- The SMTP user
- The SMTP user password
- The email “from” address
The App.config.sample
file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="smtp-server" value="SMTP IP address or name/>
<add key="smtp-port" value="587"/>
<!--
Port 465 is deprecated and most SMTP servers require using
port 587 to send eamil with TLS.
-->
<add key="smtp-user" value="smtp user"/>
<add key="smtp-password" value="smtp password"/>
<add key="from-address" value="email from address"/>
</appSettings>
</configuration>
Copy the App.config.sample
file to App.config
and fill in your SMTP provider details.
This project was compiled with AVR 15x. This project be opened and used with higher versions of AVR. You can’t open this project with an earlier version of AVR, but if you copy and pasted the code as-in to older versions I am pretty sure it works at least down to AVR 12.x.
Send an email
This repo provides a reusable Emailer
class that you can use to send emails through an SMTP server with ASNA Visual RPG. The project code on GitHub includes a more detailed example, but the short version of sending email with the Emailer
class looks like this:
DclFld em Type(Emailer) New()
em.ToAdd(rp@asna.com)
em.Subject "This is the email subject"
em.Body = "This is the email body"
em.Send()
The Emailer
class has been used successfully with several email providers including Office 365, GMail, and Zoho. If you use two-factor authentication with your SMTP provider, check its documentation. Generally, with two-factor authentication you’ll need to generate a special-case password.