Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ApiMailSenderOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DevBetterWeb.Infrastructure.Services;

public class ApiMailSenderOptions
{
public string? ApiBaseUrl { get; set; }
public string? ApiKey { get; set; }
public string? Sender { get; set; }
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Head over to [devBetter.com](https://devbetter.com) to see the live site. Scroll
- Register
- Login
- View Public Questions/Topics
- Validate Accounts via Email (SendGrid)
- Validate Accounts via Email (SMTP2Go)

### Members Only

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<PackageReference Include="Markdig" />
<PackageReference Include="MediatR" />
<PackageReference Include="NimblePros.Vimeo" />
<PackageReference Include="Sendgrid" />
<PackageReference Include="Ardalis.EFCore.Extensions" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static IServiceCollection AddInfrastructureServices(this IServiceCollecti
}
else
{
services.AddTransient<IEmailService, SendGridEmailService>();
services.AddTransient<IEmailService, Smtp2GoEmailService>();
}

// Common Dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DevBetterWeb.Infrastructure.Services;

public class ApiMailSenderOptions
{
public string? ApiBaseUrl { get; set; }
public string? ApiKey { get; set; }
public string? Sender { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public class AuthMessageSenderOptions
{
public string? SendGridUser { get; set; }
public string? SendGridKey { get; set; }
public string? SmtpServer { get; set; }
public int SmtpPort { get; set; } = 587;
public string? ApiKey { get; set; }
}
51 changes: 0 additions & 51 deletions src/DevBetterWeb.Infrastructure/Services/SendGridEmailService.cs

This file was deleted.

50 changes: 50 additions & 0 deletions src/DevBetterWeb.Infrastructure/Services/Smtp2GoEmailService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Ardalis.GuardClauses;
using DevBetterWeb.Core.Interfaces;
using Microsoft.Extensions.Options;

namespace DevBetterWeb.Infrastructure.Services;

public class Smtp2GoEmailService : IEmailService
{
private readonly HttpClient _httpClient;
public Smtp2GoEmailService(IOptions<AuthMessageSenderOptions> optionsAccessor)
{
Guard.Against.Null(optionsAccessor, nameof(optionsAccessor));
Guard.Against.Null(optionsAccessor.Value, nameof(optionsAccessor.Value));
Options = optionsAccessor.Value;
_httpClient = new HttpClient();
}

public AuthMessageSenderOptions Options { get; }

public async Task SendEmailAsync(string email, string subject, string message)
{
if (string.IsNullOrEmpty(Options.ApiKey)) throw new Exception("SMTP API Key not set.");

var request = new HttpRequestMessage(HttpMethod.Post, "https://api.smtp2go.com/v3/email/send");
request.Headers.Add("Authorization", $"Bearer {Options.ApiKey}");

var payload = new
{
sender = "donotreply@devbetter.com",
to = new[] { email },
subject = subject,
text_body = message,
html_body = message
};
string json = JsonSerializer.Serialize(payload);
request.Content = new StringContent(json, Encoding.UTF8, "application/json");

var response = await _httpClient.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
var error = await response.Content.ReadAsStringAsync();
throw new Exception($"SMTP2GO API error: {response.StatusCode} - {error}");
}
}
}
2 changes: 1 addition & 1 deletion src/DevBetterWeb.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

builder.Services.AddLogging();

builder.Services.Configure<AuthMessageSenderOptions>(builder.Configuration.GetSection("AuthMessageSenderOptions"));
builder.Services.Configure<ApiMailSenderOptions>(builder.Configuration.GetSection("ApiMailSenderOptions"));
builder.Services.Configure<DiscordWebhookUrls>(builder.Configuration.GetSection("DiscordWebhookUrls"));
builder.Services.Configure<StripeOptions>(builder.Configuration.GetSection("StripeOptions"));
builder.Services.Configure<SubscriptionPlanOptions>(builder.Configuration.GetSection("SubscriptionPlanOptions"));
Expand Down
5 changes: 5 additions & 0 deletions src/DevBetterWeb.Web/appsettings.Template.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"ApiSettings": {
"ApiKey": "[api key string goes here]"
},
"ApiMailSenderOptions": {
"ApiBaseUrl": "https://api.smtp2go.com/v3/",
"ApiKey": "[smtp2go api key goes here]",
"Sender": "donotreply@devbetter.com"
},
"Logging": {
"ApplicationInsights": {
"LogLevel": {
Expand Down
6 changes: 6 additions & 0 deletions src/DevBetterWeb.Web/appsettings.Testing.Template.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
"ApiSettings": {
"ApiKey": "[api key string goes here]"
},
"AuthMessageSenderOptions": {
"SmtpServer": "mail.smtp2go.com",
"SmtpPort": 587,
"SmtpUsername": "[SMTP2Go username goes here]",
"SmtpPassword": "[SMTP2Go password goes here]"
},
"Logging": {
"ApplicationInsights": {
"LogLevel": {
Expand Down
5 changes: 5 additions & 0 deletions src/DevBetterWeb.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"ApiSettings": {
"ApiKey": "[api key string goes here]"
},
"ApiMailSenderOptions": {
"ApiBaseUrl": "https://api.smtp2go.com/v3/",
"ApiKey": "[smtp2go api key goes here]",
"Sender": "donotreply@devbetter.com"
},
"Logging": {
"ApplicationInsights": {
"LogLevel": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Threading.Tasks;
using DevBetterWeb.Infrastructure.Services;
using Microsoft.Extensions.Options;
using Xunit;

namespace DevBetterWeb.Tests.Services.EmailServiceTests;

public class Smtp2GoEmailServiceTests
{
[Fact]
public void Constructor_WithNullOptions_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => new Smtp2GoEmailService(null!));
}

[Fact]
public async Task SendEmailAsync_WithNullApiKey_ThrowsException()
{
var options = new AuthMessageSenderOptions
{
SmtpServer = "mail.smtp2go.com"
};
var service = new Smtp2GoEmailService(Options.Create(options));

var exception = await Assert.ThrowsAsync<Exception>(() =>
service.SendEmailAsync("test@example.com", "Test Subject", "Test Message"));

Assert.Equal("SMTP API Key not set.", exception.Message);
}
}
Loading