Skip to content

Commit

Permalink
Switch to MailKit for SMTP (#288)
Browse files Browse the repository at this point in the history
Closes #287
  • Loading branch information
jvyden authored Nov 25, 2023
2 parents b2e98e0 + 804c423 commit e5584c9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
1 change: 1 addition & 0 deletions Refresh.GameServer/Refresh.GameServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Discord.Net.Webhook" Version="3.13.0" />
<PackageReference Include="MailKit" Version="4.3.0" />
<PackageReference Include="NPTicket" Version="3.1.0" />
<PackageReference Include="Pfim" Version="0.11.2" />
<PackageReference Include="Realm" Version="11.6.1" />
Expand Down
47 changes: 26 additions & 21 deletions Refresh.GameServer/Services/SmtpService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Net;
using System.Net.Mail;
using Bunkum.Core;
using Bunkum.Core.Services;
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
using NotEnoughLogs;
using Refresh.GameServer.Configuration;
using Refresh.GameServer.Types.UserData;
Expand All @@ -10,7 +11,6 @@ namespace Refresh.GameServer.Services;

public class SmtpService : EndpointService
{
private readonly SmtpClient? _smtpClient;
private readonly IntegrationConfig _integrationConfig;
private readonly GameServerConfig _gameConfig;

Expand All @@ -20,33 +20,36 @@ internal SmtpService(IntegrationConfig integrationConfig,
{
this._integrationConfig = integrationConfig;
this._gameConfig = gameConfig;

if (!this._integrationConfig.SmtpEnabled) return;

this._smtpClient = new SmtpClient(this._integrationConfig.SmtpHost)
{
Port = this._integrationConfig.SmtpPort,
EnableSsl = this._integrationConfig.SmtpTlsEnabled,
Credentials = new NetworkCredential(this._integrationConfig.SmtpUsername, this._integrationConfig.SmtpPassword),
DeliveryMethod = SmtpDeliveryMethod.Network,
};
}

private bool SendEmail(string recipient, string subject, string body)
{
if (!this._integrationConfig.SmtpEnabled || this._smtpClient == null) return false;
if (!this._integrationConfig.SmtpEnabled) return false;

using SmtpClient client = new();

SecureSocketOptions tlsOptions = SecureSocketOptions.None;

if (this._integrationConfig is { SmtpTlsEnabled: true, SmtpPort: 587 })
tlsOptions = SecureSocketOptions.StartTls;
else if (this._integrationConfig.SmtpTlsEnabled)
tlsOptions = SecureSocketOptions.SslOnConnect;

MailMessage message = new()
{
From = new MailAddress(this._integrationConfig.SmtpUsername),
To = { new MailAddress(recipient) },
Subject = subject,
Body = body,
client.Connect(this._integrationConfig.SmtpHost, this._integrationConfig.SmtpPort, tlsOptions);
client.Authenticate(this._integrationConfig.SmtpUsername, this._integrationConfig.SmtpPassword);

MimeMessage message = new();
message.From.Add(new MailboxAddress(this._gameConfig.InstanceName, this._integrationConfig.SmtpUsername));
message.To.Add(new MailboxAddress(recipient, recipient));

message.Subject = subject;
message.Body = new TextPart("plain") {
Text = body,
};

try
{
this._smtpClient.Send(message);
client.Send(message);
}
catch (Exception e)
{
Expand All @@ -55,6 +58,8 @@ private bool SendEmail(string recipient, string subject, string body)
}

this.Logger.LogDebug(BunkumCategory.Service, $"Successfully sent '{subject}' to '{recipient}'");

client.Disconnect(true);
return true;
}

Expand Down

0 comments on commit e5584c9

Please sign in to comment.