Xamarin.Forms - Send Email Using SMTP

In this article, you will learn how to send background Email with SMTP in Xamarin Forms.

Introduction
 
Xamarin.Forms - Send Email using SMTP
 
Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files are most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
 
SMTP (Simple Mail Transfer Protocol)
Xamarin.Forms - Send Email using SMTP
Use SMTP to send and receive mail messages. SMTP provides a set of protocols that simplifies the communication of email messages between email servers. It is an Internet standard for electronic mail (email) transmission. SMTP is a part of the application layer of the TCP/IP protocol. The default TCP port used by SMTP is 25 and the SMTP connections secured by SSL, known as SMTPS, uses the default to port 465. Most SMTP server names are written in the form "smtp.domain.com" or "mail.domain.com": for example, a Gmail account will refer to smtp.gmail.com the Gmail port number is 587.
 
Prerequisites
  • Visual Studio 2017(Windows or Mac)
Setting up a Xamarin.Forms Project
 
Start by creating a new Xamarin.Forms project. You’ll learn more by going through the steps yourself.
 
Choose the Xamarin.Forms App Project type under Cross-platform/App in the New Project dialog.
 
Xamarin.Forms - Send Email using SMTP 
 
Name your app, select “Use Shared Library” for shared code, and target both Android and iOS.
 
Xamarin.Forms - Send Email using SMTP 
 
You probably want your project and solution to use the same name as your app. Put it in your preferred folder for projects and click Create.
 
Xamarin.Forms - Send Email using SMTP 
 
You now have a basic Xamarin.Forms app. Click the play button to try it out.
 
Xamarin.Forms - Send Email using SMTP 
 
Setting up the User Interface
 
Go to MainPage.Xaml and write the following code.
 
MainPage.xaml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamarinFormsEmail" x:Class="XamarinFormsEmail.MainPage">  
  3.     <StackLayout>      
  4.         <StackLayout HorizontalOptions="Center" VerticalOptions="Start">      
  5.          <Image Margin="0,50,0,0" x:Name="imgBanner" Source="banner.png" ></Image>      
  6.          <Image Margin="0,0,0,10" x:Name="imgEmail" HeightRequest="150" Source="maillogo.png" ></Image>      
  7.          <Label Margin="0,0,0,10" Text="Email with SMTP" FontAttributes="Bold" FontSize="Large" TextColor="#CA6F1E" HorizontalTextAlignment="Center" ></Label>     
  8.          <Entry x:Name="txtTo" Placeholder="someone@example.com"> </Entry>    
  9.          <Entry x:Name="txtSubject" Placeholder="Subject"> </Entry>    
  10.          <Editor x:Name="txtBody" HeightRequest="50" > </Editor>   
  11.          <Button x:Name="btnSend" Text="Send" Clicked="btnSend_Clicked" />      
  12.         </StackLayout>      
  13.     </StackLayout>    
  14. </ContentPage>  
Click the play button to try it out.
 
Xamarin.Forms - Send Email using SMTP 
 
Troubleshooting
 
Allow less secure apps
 
Go to the following link. You must enable less secure app access.
 
https://myaccount.google.com/lesssecureapps
 
Now, you can turn on less secure app access.
 
Xamarin.Forms - Send Email using SMTP 
 
 Host smtp.gmail.com
 Port 587
 
Send Mail
 
In this step, write the following code to send an email.
 
MainPage.xaml.cs
  1. using System.Net.Mail;  
  2. using Xamarin.Forms;  
  3.   
  4. namespace XamarinFormsEmail  
  5. {  
  6.     public partial class MainPage : ContentPage  
  7.     {  
  8.         public MainPage()  
  9.         {  
  10.             InitializeComponent();  
  11.         }  
  12.   
  13.         void btnSend_Clicked(object sender, System.EventArgs e)  
  14.         {  
  15.             try{  
  16.   
  17.                 MailMessage mail = new MailMessage();  
  18.                 SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");  
  19.   
  20.                 mail.From = new MailAddress("susairajs18@gmail.com");  
  21.                 mail.To.Add(txtTo.Text);  
  22.                 mail.Subject = txtSubject.Text;  
  23.                 mail.Body = txtBody.Text;  
  24.   
  25.                 SmtpServer.Port = 587;  
  26.                 SmtpServer.Host = "smtp.gmail.com";  
  27.                 SmtpServer.EnableSsl = true;  
  28.                 SmtpServer.UseDefaultCredentials = false;  
  29.                 SmtpServer.Credentials = new System.Net.NetworkCredential("susairajs18@gmail.com""***********");  
  30.   
  31.                 SmtpServer.Send(mail);  
  32.             }  
  33.             catch(Exception ex)  
  34.             {  
  35.                 DisplayAlert("Faild", ex.Message, "OK");  
  36.             }  
  37.         }  
  38.     }  
  39. }  
Click the play button to try it out.
 
Xamarin.Forms - Send Email using SMTP 
 
Check your inbox
 
Xamarin.Forms - Send Email using SMTP 
I hope you have understood how to send background Email with SMTP in Xamarin.Forms.
 
Thanks for reading. Please share comments and feedback.