Skip to main content

MailkitSender Implementation

# MailkitSender Implementation

Overview

MailkitSender is an email sender that mirrors SmtpSender functionality but adds proxy support via MailKit's ProxyClient. It implements the IEmailSender interface and uses MailKit.Net.Smtp.SmtpClient instead of System.Net.Mail.SmtpClient.

When MailkitSender is Used

The sender selection logic (in EmailTemplateService.cs and GiftboxService.cs) checks whether an EmailProfileAddress has an active proxy configured:

  • If the sender type is "smtp", "outlook", "gmailapi", or "hmail" AND an active EmailProfileAddressProxy record exists linking it to an active ProxySettingsMailkitSender is used.
  • Otherwise → SmtpSender is used (unchanged behavior).

New Entities

ProxySettings

PropertyTypeDescription
IdintPrimary key
NamestringDisplay label (max 255)
HoststringProxy server hostname/IP (required, max 255)
PortintProxy server port (required)
UsernamestringOptional proxy auth username (max 255)
PasswordstringOptional proxy auth password (max 255)
ProxyTypestring"Socks5" or "HttpConnect" (max 50)
ActiveboolWhether this proxy is enabled

EmailProfileAddressProxy

PropertyTypeDescription
IdintPrimary key
EmailProfileAddressIdintFK → EmailProfileAddresses
ProxySettingsIdintFK → ProxySettings
ActiveboolWhether this proxy link is active

Architecture

EmailProfileAddresses (existing)

│ 1:N via EmailProfileAddressProxy

EmailProfileAddressProxy (junction table)

│ N:1

ProxySettings (reusable proxy definitions)

A single ProxySettings record can be shared across multiple email profile addresses.

Proxy Types Supported

ProxyTypeMailKit ClassUse Case
Socks5Socks5ClientSOCKS5 proxy (common for tunnels)
HttpConnectHttpProxyClientHTTP CONNECT proxy

SSL/TLS Handling

MailKit uses SecureSocketOptions instead of EnableSsl:

PortSecureSocketOptions
465SslOnConnect
587StartTls
OtherAuto

Internal servers (mail.soproserver.co.uk, 185.22.252.171) use SecureSocketOptions.None.

Key Differences from SmtpSender

AspectSmtpSenderMailkitSender
SMTP ClientSystem.Net.Mail.SmtpClientMailKit.Net.Smtp.SmtpClient
Proxy SupportNoneSOCKS5 / HTTP CONNECT via ProxyClient
Message FormatMailMessage → send directlyMailMessageMimeMessage → send
SSL ConfigEnableSsl = trueSecureSocketOptions enum
Send MethodSynchronous client.Send()Async client.SendAsync()
ConstructorParameterless or with log svcRequires ProxySettings

Files Changed/Created

FileAction
SoProEntities/SoProEntities/ProxySettings.csCreated
SoProEntities/SoProEntities/EmailProfileAddressProxy.csCreated
Services/Services/Email/MailkitSender.csCreated
SoproData/SoproData/SoProDataContext.csAdded DbSets
Services/Services/EmailTemplateService.csModified sender selection
Services/Services/GiftBox/GiftboxService.csModified sender selection
Services/Services/Services.csprojUpdated package refs + added file
Services/Services/packages.configUpdated package versions
SoProEntities/SoProEntities/SoProEntities.csprojAdded new entity files

Database Migration Required

After deploying, run EF migrations or manually create tables:

CREATE TABLE ProxySettings (
Id INT IDENTITY(1,1) PRIMARY KEY,
Name NVARCHAR(255) NOT NULL,
Host NVARCHAR(255) NOT NULL,
Port INT NOT NULL,
Username NVARCHAR(255) NULL,
Password NVARCHAR(255) NULL,
ProxyType NVARCHAR(50) NOT NULL,
Active BIT NOT NULL DEFAULT 1
);

CREATE TABLE EmailProfileAddressProxy (
Id INT IDENTITY(1,1) PRIMARY KEY,
EmailProfileAddressId INT NOT NULL,
ProxySettingsId INT NOT NULL,
Active BIT NOT NULL DEFAULT 1,
CONSTRAINT FK_EmailProfileAddressProxy_EmailProfileAddresses
FOREIGN KEY (EmailProfileAddressId) REFERENCES EmailProfileAddresses(Id),
CONSTRAINT FK_EmailProfileAddressProxy_ProxySettings
FOREIGN KEY (ProxySettingsId) REFERENCES ProxySettings(Id)
);

Usage Example

To route an email profile address through a proxy:

  1. Create a ProxySettings record with proxy host/port/credentials.
  2. Create an EmailProfileAddressProxy linking the EmailProfileAddresses.Id to the ProxySettings.Id with Active = true.
  3. The sender selection logic will automatically pick MailkitSender for that address.