Code
·
49 lines
·
1501 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49using MailSharp.Common;
using MailSharp.SMTP.Extensions;
using System.Net;
namespace MailSharp.SMTP.Session;
public partial class SmtpSession
{
// In HandleMailAsync, add SPF check
private async Task HandleMailAsync(string[] parts, string line, CancellationToken ct)
{
if (state != SmtpState.HeloReceived && state != SmtpState.TlsStarted)
{
await writer.WriteLineAsync(configuration["SmtpResponses:BadSequence"], ct);
return;
}
if (security == SecurityEnum.StartTls && state != SmtpState.TlsStarted)
{
await writer.WriteLineAsync(configuration["SmtpResponses:TlsRequired"], ct);
return;
}
if (!line.Contains("FROM:", StringComparison.OrdinalIgnoreCase))
{
await writer.WriteLineAsync(configuration["SmtpResponses:SyntaxError"], ct);
return;
}
string mailFromAddress = line.Substring(line.IndexOf("FROM:", StringComparison.OrdinalIgnoreCase) + 5).Trim();
string mailFromDomain = mailFromAddress.Substring(mailFromAddress.IndexOf('@') + 1);
string clientIp = (client.Client.RemoteEndPoint as IPEndPoint)?.Address.ToString() ?? "Unknown";
if (configuration.GetValue<bool>("SmtpSettings:RequireDkim"))
{
bool spfValid = await spfChecker.CheckSpfAsync(clientIp, mailFromDomain, mailFromDomain);
if (!spfValid)
{
await writer.WriteLineAsync("550 SPF check failed", ct);
return;
}
}
mailFrom = mailFromAddress;
state = SmtpState.MailFromReceived;
await writer.WriteLineAsync(configuration["SmtpResponses:Ok"], ct);
}
}