Skip to content

Commit

Permalink
(#126) adding email service to infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed May 27, 2024
1 parent 471c0ec commit 9acba8b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,12 @@
}
}
}
},
"smtp": {
"host": "smtp.namecheap.com",
"port": 587,
"fromEmail": "info@itsharppro.com",
"password": "tX8R0JL8oNphvZxlO8nKbpTeDKiYeO",
"enableSSL": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
using MiniSpace.Services.Email.Application.Services.Clients;
using MiniSpace.Services.Email.Infrastructure.Services.Clients;
using Elasticsearch.Net;
using Microsoft.Extensions.Configuration;


namespace MiniSpace.Services.Email.Infrastructure
Expand All @@ -51,6 +52,18 @@ public static class Extensions
{
public static IConveyBuilder AddInfrastructure(this IConveyBuilder builder)
{
var smtpConfig = builder.Services.BuildServiceProvider().GetService<IConfiguration>().GetSection("smtp").Get<SmtpSettings>();
builder.Services.AddSingleton(smtpConfig);

builder.Services.AddSingleton<IEmailService, SmtpEmailService>(provider =>
new SmtpEmailService(
smtpConfig.Host,
smtpConfig.Port,
smtpConfig.Username,
smtpConfig.Password,
smtpConfig.EnableSSL
));

builder.Services.AddSingleton<IEventMapper, EventMapper>();
builder.Services.AddTransient<IStudentEmailsRepository, StudentEmailsMongoRepository>();
builder.Services.AddSingleton<IEventMapper, EventMapper>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net;
using MiniSpace.Services.Email.Application.Services;

namespace MiniSpace.Services.Email.Infrastructure.Services
{
public class SmtpEmailService : IEmailService
{
private readonly string _smtpHost;
private readonly int _smtpPort;
private readonly string _fromEmail;
private readonly string _password;
private readonly bool _enableSSL;

public SmtpEmailService(string smtpHost, int smtpPort, string fromEmail, string password, bool enableSSL)
{
_smtpHost = smtpHost;
_smtpPort = smtpPort;
_fromEmail = fromEmail;
_password = password;
_enableSSL = enableSSL;
}

public async Task SendEmailAsync(string to, string subject, string body)
{
var mailMessage = new MailMessage(_fromEmail, to, subject, body);
using (var client = new SmtpClient(_smtpHost, _smtpPort))
{
client.EnableSsl = _enableSSL;
client.Credentials = new NetworkCredential(_fromEmail, _password);
await client.SendMailAsync(mailMessage);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MiniSpace.Services.Email.Infrastructure.Services
{
public class SmtpSettings
{
public string Host { get; set; }
public int Port { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public bool EnableSSL { get; set; }
}
}

0 comments on commit 9acba8b

Please sign in to comment.