Skip to content

Commit b18fd59

Browse files
authored
Merge pull request #7 from PandaTechAM/development
Add Outlook support with different response
2 parents 58848a3 + 3eb3339 commit b18fd59

File tree

2 files changed

+70
-28
lines changed

2 files changed

+70
-28
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: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,98 @@ 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 response in responses)
4365
{
44-
// Check if the response string matches the expected format
45-
if (!match.Success || match.Groups.Count != 6)
66+
var match = Regex.Match(response, GmailPattern);
67+
68+
if (!match.Success)
4669
{
47-
throw new ArgumentException("Invalid response string format.");
70+
match = Regex.Match(response, OutlookPattern);
71+
72+
if (!match.Success)
73+
{
74+
throw new ArgumentException("Invalid response string format.");
75+
}
4876
}
4977

50-
// Create a new instance of EmailSendResponse and populate its properties
51-
responseList.Add(new GeneralEmailResponse
78+
if (match.Groups.Count == 5)
5279
{
53-
Status = match.Groups[1].Value,
54-
Code = match.Groups[2].Value,
55-
TrackingId = match.Groups[3].Value,
56-
Id = match.Groups[4].Value,
57-
Service = match.Groups[5].Value
58-
});
80+
responseList.Add(new GeneralEmailResponse
81+
{
82+
Status = match.Groups[1].Value,
83+
Code = match.Groups[2].Value,
84+
TrackingId = null,
85+
Id = match.Groups[3].Value,
86+
Service = match.Groups[4].Value
87+
});
88+
}
89+
90+
if (match.Groups.Count == 6)
91+
{
92+
responseList.Add(new GeneralEmailResponse
93+
{
94+
Status = match.Groups[1].Value,
95+
Code = match.Groups[2].Value,
96+
TrackingId = match.Groups[3].Value,
97+
Id = match.Groups[4].Value,
98+
Service = match.Groups[5].Value
99+
});
100+
}
59101
}
60102

61103
return responseList;

0 commit comments

Comments
 (0)