EasySmtpEmail

EasySmtpEmail

A .NET Standard library which help you easily work with smtp email

Note:

If you are using gmail for smtp server and can't send mail because google block it. Follow these steps:

#Goto Screen Shot 2021-12-15 at 21.54.04.png

#Then enable Screen Shot 2021-12-15 at 21.55.28.png

Installation

https://www.nuget.org/packages/EasySmtpEmail

Install-Package EasySmtpEmail

API

using EasyEmail;

Methods

bool ValidateEmailAddress(string emailAddress);

Validate an email address. It must be follow these rules: Has only one @ character. Has at least 3 chars after the @. Domain section contains at least one dot. Dot can't be immediately after the @ character.

Parameters Screen Shot 2022-01-09 at 15.14.35.png Return Screen Shot 2022-01-09 at 15.13.28.png Example

bool isValid = EmailHelper.ValidateEmailAddress("youremail@gmail.com");
Assert.AreEqual(true, isValid);

bool ValidateEmailAddress(IEnumerable<string> emailAddress);

Validate list email address

Parameters Screen Shot 2022-01-09 at 16.01.09.png Return Screen Shot 2022-01-09 at 16.02.06.png Example

List<string> listEmail = new List<string> 
{ 
     "abc@gmail.com", "12.34@123", "12@123.23", "12@123.23.21"
};

bool isValid = EmailHelper.ValidateEmailAddress(listEmail);
Assert.AreEqual(false, isValid);

ResponseData SendEmail(IEnumerable<string> toAddress, IEnumerable<string> ccAddress, string subject, string body);

Send an email without attach files

Parameters Screen Shot 2022-01-09 at 15.59.06.png Return Screen Shot 2022-01-09 at 15.47.32.png Example

EmailOptions emailOptions = new EmailOptions
{
     SmtpEmailAccount = "your_SmtpEmailAccount",
     SmtpPassword = "your_SmtpPassword",
     SmtpServer = "smtp.gmail.com",
     SmtpPort = 587,
     SmtpSsl = true,
     SmtpSentFrom = "your_SmtpSentFrom",
     SmtpDisplayName = "anything you want"
};
//if it not set => throw exception. (Init one time)
EmailHelper.InitSmtpEmail(emailOptions); 

List<string> listToEmail = new List<string> 
{ 
     "email1@gmail.com",
     "email2@gmail.com"
};

List<string> listCCEmail = new List<string>();

EmailHelper.SendEmail(listToEmail, listCCEmail, "subjectEmail", "bodyEmail");

ResponseData SendEmail(IEnumerable<string> toAddress, IEnumerable<string> ccAddress, string subject, string body, IEnumerable<Attachment> attachFiles);

Send an email with attach files

Parameters Screen Shot 2022-01-09 at 15.58.06.png Return Screen Shot 2022-01-09 at 15.47.32.png Example

EmailOptions emailOptions = new EmailOptions
{
     SmtpEmailAccount = "your_SmtpEmailAccount",
     SmtpPassword = "your_SmtpPassword",
     SmtpServer = "smtp.gmail.com",
     SmtpPort = 587,
     SmtpSsl = true,
     SmtpSentFrom = "your_SmtpSentFrom",
     SmtpDisplayName = "anything you want"
};
//if it not set => throw exception. (Init one time)
EmailHelper.InitSmtpEmail(emailOptions); 

List<string> listToEmail = new List<string> 
{ 
     "email1@gmail.com",
     "email2@gmail.com"
};

List<string> listCCEmail = new List<string>();

List<Attachment> attachFiles = new List<Attachment>();
attachFiles.Add(new Attachment("/Volumes/UserData/ReadMe.txt"));

EmailHelper.SendEmail(listToEmail, listCCEmail, "subjectEmail", "bodyEmail", attachFiles);