Skip to content

Commit 9e73d3a

Browse files
authored
Merge pull request #18 from PandaTechAM/development
removing ipv4 prioritization
2 parents 6186864 + 2ca980d commit 9e73d3a

File tree

2 files changed

+6
-64
lines changed

2 files changed

+6
-64
lines changed

src/Communicator/Communicator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<PackageReadmeFile>Readme.md</PackageReadmeFile>
99
<Authors>Pandatech</Authors>
1010
<Copyright>MIT</Copyright>
11-
<Version>2.2.1</Version>
11+
<Version>2.2.2</Version>
1212
<PackageId>Pandatech.Communicator</PackageId>
1313
<Title>SMS and Email Communication helper</Title>
1414
<PackageTags>pandatech;communicator;messaging;email;smtp;mailkit;mimekit;sms;twilio;dexatel;notifications;transactional;dotnet;net9;minimal-api;dependency-injection</PackageTags>

src/Communicator/Services/Implementations/EmailService.cs

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Net;
2-
using System.Net.Sockets;
3-
using Communicator.Helpers;
1+
using Communicator.Helpers;
42
using Communicator.Models;
53
using Communicator.Options;
64
using Communicator.Services.Interfaces;
@@ -79,69 +77,13 @@ private static SmtpClient CreateClient(EmailConfiguration config)
7977

8078
private static async Task ConnectAndAuthAsync(SmtpClient client, EmailConfiguration config, CancellationToken ct)
8179
{
82-
var options = ResolveSocketOptions(config.SmtpPort);
83-
client.Timeout = config.TimeoutMs;
80+
var socketOptions = ResolveSocketOptions(config.SmtpPort);
81+
await client.ConnectAsync(config.SmtpServer, config.SmtpPort, socketOptions, ct);
8482

85-
var addresses = await Dns.GetHostAddressesAsync(config.SmtpServer, ct);
86-
87-
var ordered = addresses
88-
.OrderByDescending(a => a.AddressFamily == AddressFamily.InterNetwork) // IPv4 first
89-
.ToArray();
90-
91-
Exception? last = null;
92-
93-
foreach (var ip in ordered)
83+
if (!string.IsNullOrWhiteSpace(config.SmtpUsername))
9484
{
95-
NetworkStream? stream = null;
96-
97-
try
98-
{
99-
using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct);
100-
cts.CancelAfter(config.TimeoutMs);
101-
102-
var socket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
103-
await socket.ConnectAsync(new IPEndPoint(ip, config.SmtpPort), cts.Token);
104-
105-
stream = new NetworkStream(socket, ownsSocket: true);
106-
107-
await client.ConnectAsync(stream, config.SmtpServer, config.SmtpPort, options, cts.Token);
108-
109-
stream = null; // MailKit now owns the connection
110-
111-
await client.AuthenticateAsync(config.SmtpUsername, config.SmtpPassword, cts.Token);
112-
return;
113-
}
114-
catch (Exception ex)
115-
{
116-
last = ex;
117-
118-
try
119-
{
120-
if (stream is not null)
121-
{
122-
await stream.DisposeAsync();
123-
}
124-
}
125-
catch
126-
{
127-
// ignored
128-
}
129-
130-
try
131-
{
132-
if (client.IsConnected)
133-
{
134-
await client.DisconnectAsync(true, CancellationToken.None);
135-
}
136-
}
137-
catch
138-
{
139-
// ignored
140-
}
141-
}
85+
await client.AuthenticateAsync(config.SmtpUsername, config.SmtpPassword, ct);
14286
}
143-
144-
throw last ?? new TimeoutException("SMTP connect failed.");
14587
}
14688

14789
private static SecureSocketOptions ResolveSocketOptions(int port)

0 commit comments

Comments
 (0)