Skip to content

Commit

Permalink
privateemail smtp instead of sendgrid
Browse files Browse the repository at this point in the history
  • Loading branch information
jetcar committed Apr 15, 2024
1 parent 863608f commit 5b75286
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 3,449 deletions.
2 changes: 1 addition & 1 deletion IdentityServer/vue-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@vue/tsconfig": "^0.4.0",
"npm-run-all2": "^6.1.1",
"typescript": "~5.2.0",
"vite": "^4.4.11",
"vite": "^4.5.3",
"vue-tsc": "^1.8.19"
}
}
3 changes: 2 additions & 1 deletion PassiWebApiTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public void OneTimeSetUp()
passiApiStartup.ConfigureServices(services);

services.Remove(new ServiceDescriptor(typeof(IMyRestClient), typeof(MyRestClient)));//remove real requests services
services.Remove(new ServiceDescriptor(typeof(IEmailSender), typeof(EmailSender)));//remove real requests services
services.Remove(new ServiceDescriptor(typeof(IEmailSender), typeof(SendgridEmailSender)));//remove real requests services
services.Remove(new ServiceDescriptor(typeof(IEmailSender), typeof(SmtpEmailSender)));//remove real requests services

services.AddSingleton<IMyRestClient, TestRestClient>();
services.AddScoped<IEmailSender, TestEmailSender>();
Expand Down
4 changes: 2 additions & 2 deletions Services/EmailSender.cs → Services/SendgridEmailSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
namespace Services
{
[Profile]
public class EmailSender : IEmailSender
public class SendgridEmailSender : IEmailSender
{
private SendGridClient client;
private AppSetting _appSetting;

public EmailSender(AppSetting appSetting)
public SendgridEmailSender(AppSetting appSetting)
{
_appSetting = appSetting;
var apiKey = _appSetting["SendgridApiKey"];
Expand Down
80 changes: 80 additions & 0 deletions Services/SmtpEmailSender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using ConfigurationManager;
using System;
using System.Net;
using GoogleTracer;
using System.Net.Mail;
using Serilog;

namespace Services
{
[Profile]
public class SmtpEmailSender : IEmailSender
{
private SmtpClient client;
private AppSetting _appSetting;
private readonly ILogger _logger;

public SmtpEmailSender(AppSetting appSetting, ILogger logger)
{
_appSetting = appSetting;
_logger = logger;
var host = _appSetting["smtpHost"];
if (!Convert.ToBoolean(_appSetting["DoNotSendMail"]) || !string.IsNullOrEmpty(host))
this.client = new SmtpClient() { Host = host, Port = 465, EnableSsl = true, Credentials = new NetworkCredential(_appSetting["smtpUsername"], _appSetting["smtpPassword"]) };
}

public string SendInvitationEmail(string email, string code)
{
if (Convert.ToBoolean(_appSetting["DoNotSendMail"]))
email = _appSetting["testMail"];
if (email == null)
return "ok";
var message = new MailMessage(_appSetting["smtpUsername"], email)
{
IsBodyHtml = true,
Subject = $"Passi code {code}",
Body = "<html><body><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" >" +
"<tr><td>Enter this code in application.</td></tr>" +
$"<tr><td><b>{code}</b></td></tr>" +
"</table></body></html>"
};
try
{
client.Send(message);
}
catch (Exception e)
{
_logger.Error(e, e.Message);
return "failed";
}
return "ok";
}

public string SendDeletingEmail(string email, string code)
{
if (Convert.ToBoolean(_appSetting["DoNotSendMail"]))
email = _appSetting["testMail"];
if (email == null)
return "ok";
var message = new MailMessage(_appSetting["smtpUsername"], email)
{
IsBodyHtml = true,
Subject = $"Passi code {code}",
Body = "<html><body><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" >" +
"<tr><td>Enter this code in application.</td></tr>" +
$"<tr><td><b>{code}</b></td></tr>" +
"</table></body></html>"
};
try
{
client.Send(message);
}
catch (Exception e)
{
_logger.Error(e, e.Message);
return "failed";
}
return "ok";
}
}
}
Loading

0 comments on commit 5b75286

Please sign in to comment.