# 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1641719882492/QRHDRTVuq.png)
#Then enable
![Screen Shot 2021-12-15 at 21.55.28.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641719893844/x771-L2T3.png)
### Installation 
[https://www.nuget.org/packages/EasySmtpEmail](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](https://cdn.hashnode.com/res/hashnode/image/upload/v1641716080917/gU3a8n8pU.png)
**Return**
![Screen Shot 2022-01-09 at 15.13.28.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641716014851/M_GRjcwtx.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1641718873681/h21mLLsBH.png)
**Return**
![Screen Shot 2022-01-09 at 16.02.06.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641718931355/3cZ0-XLzG.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1641718751748/lMP7DUVV7.png)
**Return**
![Screen Shot 2022-01-09 at 15.47.32.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641718058076/chGu43QF8.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1641718690754/dF6tNc1hu.png)
**Return**
![Screen Shot 2022-01-09 at 15.47.32.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641718058076/chGu43QF8.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);
```
