Skip to content

Commit fc0650b

Browse files
authored
Merge pull request #8 from PandaTechAM/development
Use string instead of GeneralEmailResponse
2 parents b18fd59 + dc8f405 commit fc0650b

File tree

6 files changed

+49
-27
lines changed

6 files changed

+49
-27
lines changed

Readme.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -360,18 +360,11 @@ var email = new EmailMessage
360360
};
361361
await emailService.SendAsync(email);
362362
```
363-
Both methods return response (SendAsync - `GeneralEmailResponse`; SendBulkAsync - `List<GeneralEmailResponse>`) when you use them while sending email.
364-
If you set a variable to the call, you will be able to use returned response.
363+
Both methods return response (SendAsync - `string`; SendBulkAsync - `List<string>`) when you use them while sending email.
364+
If you set a variable to the call, you will be able to use returned response. Response structure varies on different email providers, so you can create your ouw return type and map returned string into it.
365365

366-
```csharp
367-
public class GeneralEmailResponse
368-
{
369-
public string Status { get; set; } = null!;
370-
public string Code { get; set; } = null!;
371-
public string TrackingId { get; set; } = null!;
372-
public string Id { get; set; } = null!;
373-
public string Service { get; set; } = null!;
374-
}
366+
```text
367+
2.0.0 OK 8ONXSST18NU4.DSCFVS8PQ0Q13@test AM0PR08MB5346.eurprd08.prod.outlook.com
375368
```
376369

377370
## 1.6. Limitations

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

src/Communicator/Services/Implementations/EmailService.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ internal class EmailService(CommunicatorOptions options)
1313
{
1414
private EmailConfiguration _emailConfiguration = null!;
1515

16-
public async Task<GeneralEmailResponse> SendAsync(EmailMessage emailMessage, CancellationToken cancellationToken = default)
16+
public async Task<string> SendAsync(EmailMessage emailMessage, CancellationToken cancellationToken = default)
1717
{
1818
EmailMessageValidator.Validate(emailMessage);
1919

2020
var message = CreateMimeMessage(emailMessage);
2121
return await SendEmailAsync(message, cancellationToken);
2222
}
2323

24-
public async Task<List<GeneralEmailResponse>> SendBulkAsync(List<EmailMessage> emailMessages, CancellationToken cancellationToken = default)
24+
public async Task<List<string>> SendBulkAsync(List<EmailMessage> emailMessages, CancellationToken cancellationToken = default)
2525
{
26-
var responses = new List<GeneralEmailResponse>();
26+
var responses = new List<string>();
2727

2828
foreach (var emailMessage in emailMessages)
2929
{
@@ -80,7 +80,7 @@ private MimeMessage CreateMimeMessage(EmailMessage emailMessage)
8080
return message;
8181
}
8282

83-
private async Task<GeneralEmailResponse> SendEmailAsync(MimeMessage message, CancellationToken cancellationToken)
83+
private async Task<string> SendEmailAsync(MimeMessage message, CancellationToken cancellationToken)
8484
{
8585
using var smtpClient = new SmtpClient();
8686
smtpClient.Timeout = _emailConfiguration.TimeoutMs;
@@ -91,7 +91,7 @@ await smtpClient.ConnectAsync(_emailConfiguration.SmtpServer, _emailConfiguratio
9191
var response = await smtpClient.SendAsync(message, cancellationToken);
9292
await smtpClient.DisconnectAsync(true, cancellationToken);
9393

94-
return GeneralEmailResponse.Parse(response);
94+
return response;
9595
}
9696

9797
private EmailConfiguration GetEmailConfigurationByChannel(string channel)

src/Communicator/Services/Implementations/FakeEmailService.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,27 @@ namespace Communicator.Services.Implementations;
88

99
internal class FakeEmailService(ILogger<FakeEmailService> logger) : IEmailService
1010
{
11-
public Task<GeneralEmailResponse> SendAsync(EmailMessage emailMessage, CancellationToken cancellationToken = default)
11+
public Task<string> SendAsync(EmailMessage emailMessage, CancellationToken cancellationToken = default)
1212
{
1313
EmailMessageValidator.Validate(emailMessage);
1414

1515
logger.LogCritical("Email sent to {Recipient}\n Email subject is {Subject} \n Email body is {Body}",
1616
emailMessage.Recipients, emailMessage.Subject, emailMessage.Body);
17-
18-
return Task.FromResult(new GeneralEmailResponse());
17+
18+
return Task.FromResult("2.0.0 OK");
1919
}
2020

21-
public Task<List<GeneralEmailResponse>> SendBulkAsync(List<EmailMessage> emailMessages, CancellationToken cancellationToken = default)
21+
public Task<List<string>> SendBulkAsync(List<EmailMessage> emailMessages,
22+
CancellationToken cancellationToken = default)
2223
{
2324
foreach (var emailMessage in emailMessages)
2425
{
2526
EmailMessageValidator.Validate(emailMessage);
26-
27+
2728
logger.LogCritical("Email sent to {Recipient} \n Email subject is {Subject} \n Email body is {Body}",
2829
emailMessage.Recipients, emailMessage.Subject, emailMessage.Body);
2930
}
3031

31-
return Task.FromResult(new List<GeneralEmailResponse>());
32+
return Task.FromResult(new List<string>() { "2.0.0 OK" });
3233
}
3334
}

src/Communicator/Services/Interfaces/IEmailService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace Communicator.Services.Interfaces;
55

66
public interface IEmailService
77
{
8-
Task<GeneralEmailResponse> SendAsync(EmailMessage emailMessage, CancellationToken cancellationToken = default);
9-
Task<List<GeneralEmailResponse>> SendBulkAsync(List<EmailMessage> emailMessages, CancellationToken cancellationToken = default);
8+
Task<string> SendAsync(EmailMessage emailMessage, CancellationToken cancellationToken = default);
9+
Task<List<string>> SendBulkAsync(List<EmailMessage> emailMessages, CancellationToken cancellationToken = default);
1010
}

test/Communicator.Demo/Program.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
return Results.Ok("Email and SMS sent successfully.");
122122
});
123123

124-
app.MapGet("/send/email/html-body", async (IEmailService emailService) =>
124+
app.MapGet("/send/email/html-body/outlook", async (IEmailService emailService) =>
125125
{
126126
var email = new EmailMessage
127127
{
@@ -135,7 +135,21 @@
135135
return Results.Ok("Email sent successfully.");
136136
});
137137

138-
app.MapGet("/send/email/html-body/with-response", async (IEmailService emailService) =>
138+
app.MapGet("/send/email/html-body/gmail", async (IEmailService emailService) =>
139+
{
140+
var email = new EmailMessage
141+
{
142+
Recipients = ["[email protected]"],
143+
Subject = "Some subject",
144+
Body = EmailTemplates.AddEmailAddressTemplate("Test", "Test", "https://www.google.com/"),
145+
IsBodyHtml = true,
146+
Channel = EmailChannels.TransactionalSender
147+
};
148+
await emailService.SendAsync(email);
149+
return Results.Ok("Email sent successfully.");
150+
});
151+
152+
app.MapGet("/send/email/html-body/with-response/outlook", async (IEmailService emailService) =>
139153
{
140154
var email = new EmailMessage
141155
{
@@ -149,6 +163,20 @@
149163
return Results.Ok(response);
150164
});
151165

166+
app.MapGet("/send/email/html-body/with-response/gmail", async (IEmailService emailService) =>
167+
{
168+
var email = new EmailMessage
169+
{
170+
Recipients = ["[email protected]"],
171+
Subject = "Some subject",
172+
Body = EmailTemplates.AddEmailAddressTemplate("Test", "Test", "https://www.google.com/"),
173+
IsBodyHtml = true,
174+
Channel = EmailChannels.TransactionalSender
175+
};
176+
var response = await emailService.SendAsync(email);
177+
return Results.Ok(response);
178+
});
179+
152180
app.MapGet("/send/email/multiple-recipients/html-body", async (IEmailService emailService) =>
153181
{
154182
var email = new EmailMessage

0 commit comments

Comments
 (0)