https://github.com/promact/emailservice
Generic Email service implementation for sending emails via different Email providers (SES, SendGrid etc.)
https://github.com/promact/emailservice
Last synced: 28 days ago
JSON representation
Generic Email service implementation for sending emails via different Email providers (SES, SendGrid etc.)
- Host: GitHub
- URL: https://github.com/promact/emailservice
- Owner: Promact
- Created: 2020-10-03T15:59:39.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-02-16T09:33:52.000Z (almost 2 years ago)
- Last Synced: 2025-09-26T12:54:34.926Z (5 months ago)
- Language: C#
- Size: 584 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# EmailService
Generic Email service implementation for sending emails via different Email providers (SES, SendGrid,SMTP,Azure etc.)
# Usage
```
private IEmailService _emailService;
public async Task MyMethod()
{
// The email which we are using should be verified in Service Provider whichever you are using (SES, SendGrid,SMTP etc.)
//In Case Of Azure Your EmailDomain Should be Verified And Connected To Your Email Communications Services
var email = new Email(
to: new List
{
new EmailAddress("xyz@domain.com", "Receiver Name"),
new EmailAddress("lmn@domain.com", "Receiver Name")
},
from: new EmailAddress("abc@pqr.com", "Sender Name"),
cc: new List
{
new EmailAddress("xyz1@domain.com", "Receiver Name"),
new EmailAddress("lmn1@domain.com", "Receiver Name")
},
bcc: new List
{
new EmailAddress("xyz2@domain.com", "Receiver Name"),
new EmailAddress("lmn2@domain.com", "Receiver Name")
},
subject: "Test subject",
body: "Test body",
isBodyHtml: true
);
// Add attachments. You can Attach Multiple Attachment
email.Attachments.Add(new AttachmentData(
content: System.IO.File.ReadAllBytes("path/to/file.txt"),
fileName: "FileName",
contentType: "FileType"
));
await _emailService.SendEmailAsync(email);
}
public async Task SendTemplatedEmail()
{
var templatedEmailRequest = new TemplatedEmailRequest(
to: new List
{
new EmailAddress("xyz@domain.com", "Receiver Name"),
new EmailAddress("lmn@domain.com", "Receiver Name")
},
from: new EmailAddress("abc@pqr.com", "Sender Name"),
cc: new List
{
new EmailAddress("xyz1@domain.com", "Receiver Name"),
new EmailAddress("lmn1@domain.com", "Receiver Name")
},
bcc: new List
{
new EmailAddress("xyz2@domain.com", "Receiver Name"),
new EmailAddress("lmn2@domain.com", "Receiver Name")
},
templateNameOrId: "TemplateID", // Replace with your actual template name or ID
templateData: new { name = "Value1" }, // Replace with your actual template data
subject: "Subject"
);
// Add attachments
templatedEmailRequest.Attachments.Add(new AttachmentData(
content: System.IO.File.ReadAllBytes("path/to/file.txt"),
fileName: "FileName",
contentType: "FileType"
));
await _emailService.SendTemplatedEmailAsync(templatedEmailRequest);
}
```
# SES
Add nuget package
```
Promact.EmailService.SES
```
## ASP.NET Core projects
Add below in `Startup.cs` inside `ConfigureServices` method
```
services.AddSESEmailService(options =>
{
options.AccessKeyId = Configuration.GetSection("AWS:AccessKeyId").Value;
options.SecretAccessKey = Configuration.GetSection("AWS:SecretAccessKey").Value;
options.Region = Configuration.GetSection("AWS:Region").Value;
});
```
Add relevant appsettings.json values
```
"AWS": {
"AccessKeyID": "",
"SecretAccessKey": "",
"Region": ""
}
```
Inject `IEmailService` in class constructor from where you want to send emails
```
public MyClass(IEmailService emailService)
{
...
}
```
## Console or other type of applications
Create new object of SESEmailService passing relevant configuration values
```
var emailService = new SESEmailService(new SESOptions(){ });
```
# SendGrid
Add nuget package
```
Promact.EmailService.SendGrid
```
## ASP.NET Core projects
Add below in `Startup.cs` inside `ConfigureServices` method
```
services.AddSendGridEmailService(options =>
{
options.APIKey = Configuration.GetSection("SendGrid:APIKey").Value;
});
```
Add relevant appsettings.json values
```
"SendGrid": {
"APIKey": ""
}
```
Inject `IEmailService` in class constructor from where you want to send emails
```
public MyClass(IEmailService emailService)
{
...
}
```
## Console or other type of applications
Create new object of SendGridEmailService passing relevant configuration values
```
var emailService = new SendGridEmailService(new SendGridOptions(){ });
```
# SMTP
Add nuget package
```
Promact.EmailService.SMTP
```
## ASP.NET Core projects
Add below in `Startup.cs` inside `ConfigureServices` method
```
services.AddSMTPEmailService(options =>
{
options.Host = configuration.GetSection("SMTP:Host").Value;
options.Port = int.Parse(configuration.GetSection("SMTP:Port").Value);
options.UserName = configuration.GetSection("SMTP:UserName").Value;
options.Password = configuration.GetSection("SMTP:Password").Value;
});
```
Add relevant appsettings.json values
```
"SMTP": {
"Host": "",
"Port":,
"UserName": "",
"Password": ""
}
```
Inject `IEmailService` in class constructor from where you want to send emails
```
public MyClass(IEmailService emailService)
{
...
}
```
## Console or other type of applications
Create new object of SMTPEmailService passing relevant configuration values
```
var emailService = new SMTPEmailService(new SMTPOptions(){ });
```
# Azure
Add nuget package
```
Promact.EmailService.Azure
```
## ASP.NET Core projects
Add below in `Startup.cs` inside `ConfigureServices` method
```
services.AddAzureEmailService(options =>
{
options.ConnectionString= Configuration.GetSection("Azure:ConnectionString").Value;
});
```
Add relevant appsettings.json values
```
// You can get your connection string From Azure Portal in Your Communication Service > Setting > Keys.
"Azure": {
"ConnectionString": ""
},
```
Inject `IEmailService` in class constructor from where you want to send emails
```
public MyClass(IEmailService emailService)
{
...
}
```
## Console or other type of applications
Create new object of AzureEmailService passing relevant configuration values
```
var emailService = new AzureEmailService(new AzureOptions(){ });
```