Xamarin.Forms - Send Email Using AWS SES (Simple Email Service)

Introduction
 
Xamarin.Forms - Send Email using AWS SES(Simple Email Service)
 
Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that the process of reading and writing files is 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 AWS SES(Simple Email Service)
 
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, use 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.
 
AWS SES (Simple Email Service)
 
Xamarin.Forms - Send Email using AWS SES(Simple Email Service)
Amazon Simple Email Service (Amazon SES) is a cloud-based email sending service designed to help digital marketers and application developers send transactional emails. It is an Internet standard for electronic mail (email) transmission. It uses the default port 25, 465 or 587. You can use our SMTP interface or one of the AWS SDKs to integrate Amazon SES directly into your existing applications. SES is available in Transport Layer Security (TLS).
 
Prerequisites
  • Visual Studio 2017 (Windows or Mac)
  • AWS (Amazon Web Services) Account
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 AWS SES(Simple Email Service)
 
Name your app, select “Use Shared Library” for shared code, and target both Android and iOS.
 
Xamarin.Forms - Send Email using AWS SES(Simple Email Service)
 
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 AWS SES(Simple Email Service)
 
You now have a basic Xamarin.Forms app. Click the Play button to try it out.
 
Xamarin.Forms - Send Email using AWS SES(Simple Email Service)
 
Create an SES Service in AWS
 
In this step, create a service in the AWS. Go to the following link. Now, sign in using your preferred account.
 
Xamarin.Forms - Send Email using AWS SES(Simple Email Service)
 
After sign in, you can create an SES service in AWS console.
 
Xamarin.Forms - Send Email using AWS SES(Simple Email Service) 
 
Create SMTP Credentials
 
In this step, you can create an SMTP Credential under SMTP Settings.
 
Xamarin.Forms - Send Email using AWS SES(Simple Email Service) 
 
Now, you will get the IAM User Name; click Create.
 
Xamarin.Forms - Send Email using AWS SES(Simple Email Service) 
 
Now, the SMTP credential is created successfully. You can download and use it.
 
Xamarin.Forms - Send Email using AWS SES(Simple Email Service) 
 
Verify your Email Address 
 
In this step, Verify your Email Address, under Email Addresses menu.
 
Xamarin.Forms - Send Email using AWS SES(Simple Email Service) 
 
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="aws-ses-logo.png" ></Image>      
  7.          <Label Margin="0,0,0,10" Text="Email with AWS SES" FontAttributes="Bold" FontSize="Large" TextColor="#CA6F1E" HorizontalTextAlignment="Center" ></Label>     
  8.          <Entry x:Name="txtTo" Placeholder="[email protected]"> </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 AWS SES(Simple Email Service)
 
Troubleshooting
 
Allow less secure apps
 
Go to following link you must enable Less secure app access
 
https://myaccount.google.com/lesssecureapps
 
You can turn on Less secure app access.
 
Xamarin.Forms - Send Email using AWS SES(Simple Email Service)
 
 Server Nameemail-smtp.us-east-1.amazonaws.com 
 Host email-smtp.us-east-1.amazonaws.com
 Port 25, 465 or 587
 Use Transport Layer Security (TLS) Yes
 
Send Mail
 
In this step, write the following code for sending 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("email-smtp.us-east-1.amazonaws.com");  
  19.   
  20.                 mail.From = new MailAddress("[email protected]");  
  21.                 mail.To.Add(txtTo.Text.ToLower());  
  22.                 mail.Subject = txtSubject.Text;  
  23.                 mail.Body = txtBody.Text;  
  24.   
  25.                 SmtpServer.Port = 587;  
  26.                 SmtpServer.Host = "email-smtp.us-east-1.amazonaws.com";  
  27.                 SmtpServer.EnableSsl = true;  
  28.                 SmtpServer.UseDefaultCredentials = false;  
  29.                 SmtpServer.Credentials = new System.Net.NetworkCredential("AKI*****2LUH6IA""AnH9IL********eRL5xjGa2LsmDDbozvsTx");  
  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.
 
https://www.c-sharpcorner.com/article/how-to-implement-and-maintain-data-center-environment-with-high-availability/
 
Check your inbox
 
Xamarin.Forms - Send Email using AWS SES(Simple Email Service)
 
I hope you have understood how to send background email using AWS SES (Simple Email Service) in Xamarin.Forms.
 
Thanks for reading. Please share comments and feedback.


Similar Articles