How to send emails using VB.net, why you would want to, and how the server can send email alerts for a specific event.
Automated emailing systems are becoming more popular with the rise of the IoT. In this article, we will explore how to send emails using VB.net, why we would want to do this, and a simple example of a RIOTOUS server sending email alerts upon a specific event.
Emailing – Free Texting Anywhere!
When email first became popular, some were worried that the postal service would become obsolete. Luckily, people fell in love with online shopping! While there are many phone packages for sending multiple SMS messages, each message costs the sender some amount of money. The same goes for the postal service; sending mail costs the sender money. But email costs neither the sender or the recipient, which is one of the reasons email has become one of the dominant forms of communication. But emails are not just free, they can be sent to anyone anywhere around the globe, and are effectively borderless.
Since their introduction, emails have mostly been used for sending and receiving messages, but thanks to the rise of the Internet of Things, a new application for emails has arisen: email alerts. Imagine the scenario where a security camera has detected an intruder at your home while you are at work. Typically, the camera would catch the criminal in the act of theft, but you would not know about it until you got home. However, thanks to the IoT, such security systems can now use emails to alert their users of events as they occur. For example, a security camera could not only alert the user, but it could also send snapshots of the criminals as attachments.
With the re-release of RIOTOUS, we can expect to see many RIOTOUS-based projects ranging from security systems to data monitoring. Since all of these projects will be based around the VB.net RIOTOUS server, adding email functionality could be potentially massive. So, how does emailing work in VB.net, and how can we use a simple VB.net application to send emails?
Step 1 – Make a new email address
While you likely already have email addresses, using your personal email accounts with IoT projects is not advised, because we will need to store email usernames and passwords, which opens up a number of security issues. Therefore, it is best to create an IoT-specific email account whose only purpose is to send email alerts, so cyber criminals cannot access personal information. The email provider you choose doesn’t matter, but it is best to use an email provider that allows for access without verification methods. For example, Gmail works on many different devices, but if a new device is trying to access a Gmail account, it may potentially be denied access unless verification codes are needed. However, Gmail so far has not complained when being used from a Windows form, so hopefully, you too can use Gmail!
Step 2 – Create an empty VB.net form application and create the basic code
The next step is to create a simple form application with multiple controls that will allow you to enter email addresses to send to, set the message header, enter body text, and various other options.
With our form made, we need to double-click the button to automatically create a button-click event. At the same time, we also need to add an import to our project (similar to a C include), which will allow us to use email.
Step 3 – The Email Code
The code below is used to send an email when the button is clicked.
The first few lines are needed to create an email object that will allow us to enter the main text, whom the email is being sent to, and who is sending it. However, these can simply be copied and pasted without too much thought. You only really need to worry about the details themselves (such as your email account’s username and password). The first task is to not use the default credentials in the Smtp_Server object, but enter your credentials into the Smtp_Server object. The next task is to select the port that your email provider uses and if SSL should be used. You will need to check with your email provider for these details, but the example above shows how to use Gmail. The last step is to enter the host address, which is provided by your email provider.
Now that an SMTP object has been configured, it’s time to create the email itself. Thanks to VB.net being incredibly user-friendly, this is a trivial task and does not even need explaining. Adding attachments is also trivial, with the commented-out code showing how this is done. The last step is to send the email, which is done with Smtp_Server.send(e_mail).
This article demonstrates how trivial VB.net is and why it was chosen for the RIOTOUS server. With just a few lines of code, your RIOTOUS server can send email alerts to you no matter where you are. The best part is that since RIOTOUS client/server communication is simple, your next microcontroller project can easily send emails without needing complex libraries or error-prone procedures.