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 activeEmailProfileAddressProxyrecord exists linking it to an activeProxySettings→ MailkitSender is used. - Otherwise → SmtpSender is used (unchanged behavior).
New Entities
ProxySettings
| Property | Type | Description |
|---|---|---|
| Id | int | Primary key |
| Name | string | Display label (max 255) |
| Host | string | Proxy server hostname/IP (required, max 255) |
| Port | int | Proxy server port (required) |
| Username | string | Optional proxy auth username (max 255) |
| Password | string | Optional proxy auth password (max 255) |
| ProxyType | string | "Socks5" or "HttpConnect" (max 50) |
| Active | bool | Whether this proxy is enabled |
EmailProfileAddressProxy
| Property | Type | Description |
|---|---|---|
| Id | int | Primary key |
| EmailProfileAddressId | int | FK → EmailProfileAddresses |
| ProxySettingsId | int | FK → ProxySettings |
| Active | bool | Whether 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
| ProxyType | MailKit Class | Use Case |
|---|---|---|
Socks5 | Socks5Client | SOCKS5 proxy (common for tunnels) |
HttpConnect | HttpProxyClient | HTTP CONNECT proxy |
SSL/TLS Handling
MailKit uses SecureSocketOptions instead of EnableSsl:
| Port | SecureSocketOptions |
|---|---|
| 465 | SslOnConnect |
| 587 | StartTls |
| Other | Auto |
Internal servers (mail.soproserver.co.uk, 185.22.252.171) use SecureSocketOptions.None.
Key Differences from SmtpSender
| Aspect | SmtpSender | MailkitSender |
|---|---|---|
| SMTP Client | System.Net.Mail.SmtpClient | MailKit.Net.Smtp.SmtpClient |
| Proxy Support | None | SOCKS5 / HTTP CONNECT via ProxyClient |
| Message Format | MailMessage → send directly | MailMessage → MimeMessage → send |
| SSL Config | EnableSsl = true | SecureSocketOptions enum |
| Send Method | Synchronous client.Send() | Async client.SendAsync() |
| Constructor | Parameterless or with log svc | Requires ProxySettings |
Files Changed/Created
| File | Action |
|---|---|
SoProEntities/SoProEntities/ProxySettings.cs | Created |
SoProEntities/SoProEntities/EmailProfileAddressProxy.cs | Created |
Services/Services/Email/MailkitSender.cs | Created |
SoproData/SoproData/SoProDataContext.cs | Added DbSets |
Services/Services/EmailTemplateService.cs | Modified sender selection |
Services/Services/GiftBox/GiftboxService.cs | Modified sender selection |
Services/Services/Services.csproj | Updated package refs + added file |
Services/Services/packages.config | Updated package versions |
SoProEntities/SoProEntities/SoProEntities.csproj | Added 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:
- Create a
ProxySettingsrecord with proxy host/port/credentials. - Create an
EmailProfileAddressProxylinking theEmailProfileAddresses.Idto theProxySettings.IdwithActive = true. - The sender selection logic will automatically pick
MailkitSenderfor that address.