Intention of this post is to send email using SMTP protocol, without using any external applications(like Gmail, Outlook, and other mail applications). Here I'm making use of Xamarin.Forms to implement this concept. This post will help the cross platform developers, especially Xamarin developers to integrate SMTP in their applications.
We have plenty of plugins in Xamarin.Forms to send mail, but these plugins rely on the external mail applications which are installed in the users mobile. But our requirement is to send mail without invoking any external applications.
In this post and in my examples, I will use Gmail's SMTP address to configure email transactions. To do that we have to get access from Gmail for utilizing its SMTP. We have a simple steps below to enable this access:
- Login to your gmail account.
- Open the Url : https://myaccount.google.com/lesssecureapps in new tab, which look like the below screen shot:
- Enable Allow less secure apps switch to provide access to use gmail's SMTP.
Initial setup to utilize SMTP protocol is done, now we will understand some basics for SMTP host address and port numbers. Generic SMTP host address will be in the form of smtp.domain.com and default port will be 25. Since we are using gmail account, the SMTP address will look like smtp.gmail.com and the post number is 587. Now lets see how to use these details in our Xamarin.Forms application to send email.
- Create a simple .Net Standard application in Xamarin.Forms.
- In MainPage.xaml add some required fields for sending email.
- To entry field
- Cc entry field
- Subject entry field
- Body editor field
- Send button
- In code behind page of MainPage i.e. MainPage.xaml.cs add below code in Send button click event.
Source code is available in Github : https://github.com/GovardhanNag/SendEmailViaSMTP. Download the project, update the NetworkCredentials with your google id and password. Run and enjoy testing. Please feel free to relay your comments, suggestions or corrections.
Reference Link:
https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient.sendasync?view=netframework-4.8
Reference Link:
https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient.sendasync?view=netframework-4.8

