Skip to content

Commit 2317479

Browse files
author
Ruben Bisharyan
committed
As a general response now will be used returned string instead of GeneralEmailResponse to handle all email providers
1 parent 3eb3339 commit 2317479

File tree

6 files changed

+96
-87
lines changed

6 files changed

+96
-87
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: 77 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,66 +8,53 @@
88
var builder = WebApplication.CreateBuilder(args);
99

1010
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-
// });
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+
"TransactionalSender", new EmailConfiguration
46+
{
47+
SmtpServer = "smtp.gmail.com",
48+
SmtpPort = 465, // 587
49+
SmtpUsername = "[email protected]",
50+
SmtpPassword = "uqihfffvqfngeczq",
51+
SenderEmail = "[email protected]",
52+
UseSsl = true, // false
53+
TimeoutMs = 10000
54+
}
55+
}
56+
};
57+
});
7158

7259
builder.Services.AddEndpointsApiExplorer();
7360
builder.Services.AddSwaggerGen();
@@ -121,7 +108,7 @@
121108
return Results.Ok("Email and SMS sent successfully.");
122109
});
123110

124-
app.MapGet("/send/email/html-body", async (IEmailService emailService) =>
111+
app.MapGet("/send/email/html-body/outlook", async (IEmailService emailService) =>
125112
{
126113
var email = new EmailMessage
127114
{
@@ -135,7 +122,21 @@
135122
return Results.Ok("Email sent successfully.");
136123
});
137124

138-
app.MapGet("/send/email/html-body/with-response", async (IEmailService emailService) =>
125+
app.MapGet("/send/email/html-body/gmail", async (IEmailService emailService) =>
126+
{
127+
var email = new EmailMessage
128+
{
129+
Recipients = ["[email protected]"],
130+
Subject = "Some subject",
131+
Body = EmailTemplates.AddEmailAddressTemplate("Test", "Test", "https://www.google.com/"),
132+
IsBodyHtml = true,
133+
Channel = EmailChannels.TransactionalSender
134+
};
135+
await emailService.SendAsync(email);
136+
return Results.Ok("Email sent successfully.");
137+
});
138+
139+
app.MapGet("/send/email/html-body/with-response/outlook", async (IEmailService emailService) =>
139140
{
140141
var email = new EmailMessage
141142
{
@@ -149,6 +150,20 @@
149150
return Results.Ok(response);
150151
});
151152

153+
app.MapGet("/send/email/html-body/with-response/gmail", async (IEmailService emailService) =>
154+
{
155+
var email = new EmailMessage
156+
{
157+
Recipients = ["[email protected]"],
158+
Subject = "Some subject",
159+
Body = EmailTemplates.AddEmailAddressTemplate("Test", "Test", "https://www.google.com/"),
160+
IsBodyHtml = true,
161+
Channel = EmailChannels.TransactionalSender
162+
};
163+
var response = await emailService.SendAsync(email);
164+
return Results.Ok(response);
165+
});
166+
152167
app.MapGet("/send/email/multiple-recipients/html-body", async (IEmailService emailService) =>
153168
{
154169
var email = new EmailMessage

0 commit comments

Comments
 (0)