Skip to content

Commit 445864c

Browse files
author
Ruben Bisharyan
committed
Fix email sending issue with outlook response
1 parent 823e7ae commit 445864c

File tree

3 files changed

+75
-78
lines changed

3 files changed

+75
-78
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>1.0.3</Version>
11+
<Version>1.0.4</Version>
1212
<PackageId>Pandatech.Communicator</PackageId>
1313
<Title>SMS and Email Communication helper</Title>
1414
<PackageTags>Pandatech, library, Sms, Email, Messages</PackageTags>

src/Communicator/Models/GeneralResponses/GeneralEmailResponse.cs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,62 @@ public class GeneralEmailResponse
66
{
77
public string Status { get; set; } = null!;
88
public string Code { get; set; } = null!;
9-
public string TrackingId { get; set; } = null!;
9+
public string? TrackingId { get; set; }
1010
public string Id { get; set; } = null!;
1111
public string Service { get; set; } = null!;
1212

1313
// Define a regular expression pattern to extract relevant parts of the response
14-
private const string Pattern = @"^(\d+\.\d+\.\d+)\s+(\w+)\s+(\d+)\s+(\S+)\s+\-\s+(\w+)$";
14+
private const string GmailPattern = @"^(\d+\.\d+\.\d+)\s+(\w+)\s+(\d+)\s+(\S+)\s+\-\s+(\w+)$";
15+
16+
private const string OutlookPattern =
17+
@"^(?<Version>\d+\.\d+\.\d+)\s(?<Status>\w+)\s<(?<EmailId>[^>]+)>\s\[Hostname=(?<Hostname>[^\]]+)\]$";
1518

1619
public static GeneralEmailResponse Parse(string response)
1720
{
18-
// Match the response string against the pattern
19-
var match = Regex.Match(response, Pattern);
21+
var match = Regex.Match(response, GmailPattern);
22+
23+
if (!match.Success)
24+
{
25+
match = Regex.Match(response, OutlookPattern);
2026

21-
// Check if the response string matches the expected format
22-
if (!match.Success || match.Groups.Count != 6)
27+
if (!match.Success)
28+
{
29+
throw new ArgumentException("Invalid response string format.");
30+
}
31+
}
32+
33+
if (match.Groups.Count == 5)
2334
{
24-
throw new ArgumentException("Invalid response string format.");
35+
return new GeneralEmailResponse
36+
{
37+
Status = match.Groups[1].Value,
38+
Code = match.Groups[2].Value,
39+
TrackingId = null,
40+
Id = match.Groups[3].Value,
41+
Service = match.Groups[4].Value
42+
};
2543
}
2644

27-
// Create a new instance of EmailSendResponse and populate its properties
28-
return new GeneralEmailResponse
45+
if (match.Groups.Count == 6)
2946
{
30-
Status = match.Groups[1].Value,
31-
Code = match.Groups[2].Value,
32-
TrackingId = match.Groups[3].Value,
33-
Id = match.Groups[4].Value,
34-
Service = match.Groups[5].Value
35-
};
47+
return new GeneralEmailResponse
48+
{
49+
Status = match.Groups[1].Value,
50+
Code = match.Groups[2].Value,
51+
TrackingId = match.Groups[3].Value,
52+
Id = match.Groups[4].Value,
53+
Service = match.Groups[5].Value
54+
};
55+
}
56+
57+
throw new ArgumentException("Invalid response string format.");
3658
}
3759

3860
public static List<GeneralEmailResponse> Parse(IEnumerable<string> responses)
3961
{
4062
var responseList = new List<GeneralEmailResponse>();
4163

42-
foreach (var match in responses.Select(response => Regex.Match(response, Pattern)))
64+
foreach (var match in responses.Select(response => Regex.Match(response, GmailPattern)))
4365
{
4466
// Check if the response string matches the expected format
4567
if (!match.Success || match.Groups.Count != 6)

test/Communicator.Demo/Program.cs

Lines changed: 36 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,42 @@
77

88
var builder = WebApplication.CreateBuilder(args);
99

10-
builder.AddCommunicator();
11-
// builder.AddCommunicator(options =>
12-
// {
13-
// options.SmsFake = false;
14-
// options.SmsConfigurations = new Dictionary<string, SmsConfiguration>
15-
// {
16-
// {
17-
// "GeneralSender", new SmsConfiguration
18-
// {
19-
// Provider = "Dexatel",
20-
// From = "sender_name",
21-
// Properties = new Dictionary<string, string>
22-
// {
23-
// { "X-Dexatel-Key", "key" }
24-
// },
25-
// TimeoutMs = 10000
26-
// }
27-
// },
28-
// {
29-
// "TransactionalSender", new SmsConfiguration
30-
// {
31-
// Provider = "Twilio",
32-
// From = "sender_number",
33-
// Properties = new Dictionary<string, string>
34-
// {
35-
// { "SID", "key" },
36-
// { "AUTH_TOKEN", "token" }
37-
// },
38-
// TimeoutMs = 10000
39-
// }
40-
// }
41-
// };
42-
// options.EmailFake = false;
43-
// options.EmailConfigurations = new Dictionary<string, EmailConfiguration>
44-
// {
45-
// {
46-
// "GeneralSender2", new EmailConfiguration
47-
// {
48-
// SmtpServer = "smtp.test.com",
49-
// SmtpPort = 465, // 587
50-
// SmtpUsername = "test",
51-
// SmtpPassword = "test123",
52-
// SenderEmail = "[email protected]",
53-
// UseSsl = true, // false
54-
// TimeoutMs = 10000
55-
// }
56-
// },
57-
// {
58-
// "TransactionalSender", new EmailConfiguration
59-
// {
60-
// SmtpServer = "smtp.gmail.com",
61-
// SmtpPort = 465, // 587
62-
// SmtpUsername = "vazgen",
63-
// SmtpPassword = "vazgen123",
64-
// SenderEmail = "[email protected]",
65-
// UseSsl = true, // false
66-
// TimeoutMs = 10000
67-
// }
68-
// }
69-
// };
70-
// });
10+
// builder.AddCommunicator();
11+
builder.AddCommunicator(options =>
12+
{
13+
options.SmsFake = false;
14+
options.SmsConfigurations = new Dictionary<string, SmsConfiguration>
15+
{
16+
{
17+
"GeneralSender", new SmsConfiguration
18+
{
19+
Provider = "Dexatel",
20+
From = "sender_name",
21+
Properties = new Dictionary<string, string>
22+
{
23+
{ "X-Dexatel-Key", "key" }
24+
},
25+
TimeoutMs = 10000
26+
}
27+
}
28+
};
29+
options.EmailFake = false;
30+
options.EmailConfigurations = new Dictionary<string, EmailConfiguration>
31+
{
32+
{
33+
"GeneralSender", new EmailConfiguration
34+
{
35+
SmtpServer = "smtp-mail.outlook.com",
36+
SmtpPort = 587, // 587
37+
SmtpUsername = "[email protected]",
38+
SmtpPassword = "rbmpbzsytkvqfzdv",
39+
SenderEmail = "[email protected]",
40+
UseSsl = false, // false
41+
TimeoutMs = 10000
42+
}
43+
}
44+
};
45+
});
7146

7247
builder.Services.AddEndpointsApiExplorer();
7348
builder.Services.AddSwaggerGen();

0 commit comments

Comments
 (0)