Email micro service.
It's a simple service just to send emails.
.Net Core 3.1
- Create and fill
.envfile
SENDER_EMAIL=sender account email
SENDER_PASSWORD=sender account password
SMTP_HOST=sender smtp port
SMTP_PORT=sender smtp port- Pull latest docker image
docker-compose pull- Run service
docker-compose up
Now you can send requests to localhost:5000, authorization header value placed in src/EmailService/appsettings.Development.json. See How to use for api format.
In src/EmailService/appsettings.json file add this configuration
{
// configuration omitted
"EmailSenderOptions": {
"Email": "email address from which your service will send emails. example@gmail.com",
"Password": "password for email address from which your service will send emails. superhardpassword",
"SmtpHost": "smtp server or some another host. smtp.mail.ru",
"SmtpPort": 25 //Use your smtp port
},
"HeaderAuthorizationOptions": {
"Key": "some random key for Header Authoriztion"
},
"LOGS_ACCESS_TOKEN": "some token for access to logs via websocket"
// configuration omitted
}cd ./src/EmailService
dotnet runAPI will be available on localhost:5000
There is one available POST request on url /api/email/send.
Request body (all fields are required!):
{
"Email": "email address to which send email. test@test.com",
"Subject": "email's subject. My first Test subject",
"Body": "email's message in html. Just a test mail.<br>Second line."
}Email address which would receive email: example@mail.ru
Subject of email: Confirm your email
Body of email: Please visit this <a>link</a> to confirm your email.
curl --header "Content-Type: application/json" \
--header "Authorization: some random key for Header Authoriztion"
--request POST \
--data '{"email":"example@mail.ru","subject":"Confirm your email", "body":"Please visit this <a>link</a> to confirm your email."}' \
http://localhost:5000/api/email/send- Install RTUITLab.EmailService.Client package to your ASP.Net Core project
- Extend your appsettings.json file with following code:
{
// configuration omitted
"EmailSenderOptions": {
"BaseAddress": "url to your email service that you've created upper. http://localhost:5000",
"Key": "some random key for Header Authoriztion. Check instructions above"
}
// configuration omitted
}- Edit Startup.cs file:
// Add email service
services.AddEmailSender(Configuration
.GetSection(nameof(EmailSenderOptions))
.Get<EmailSenderOptions>());- By following code you'll send request to your email service. Email service will deliver emails:
RTUITLab.EmailService.Client.IEmailSender sender;
await sender.SendEmailAsync("test@test.com", "Email letter subject", "<h1>Html body</h1>")// Example with some random controller
public class EmailSendController : ControllerBase
{
private readonly RTUITLab.EmailService.Client.IEmailSender sender;
public EmailSender(RTUITLab.EmailService.Client.IEmailSender sender)
{
this.sender = sender;
DoStuff();
}
private async Task DoStuff()
{
// await sender.SendEmailAsync("email to send", "subject of email", "html body - message to send via email");
await sender.SendEmailAsync("test@test.com", "Email letter subject", "<h1>Html body</h1>");
}
}