Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions TickAPI/TickAPI/Common/Mail/Abstractions/IMailService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using TickAPI.Common.Mail.Models;
using TickAPI.Common.Results;

namespace TickAPI.Common.Mail.Abstractions;

public interface IMailService
{
public Task<Result> SendTicketAsync(string toEmail, string toLogin, string eventName, byte[] pdfData);

public Task<Result> SendMailAsync(string toEmail, string toLogin, string subject, string content,
List<MailAttachment>? attachments);
}
8 changes: 8 additions & 0 deletions TickAPI/TickAPI/Common/Mail/Models/MailAttachment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace TickAPI.Common.Mail.Models;

public class MailAttachment
{
public string fileName { get; set; }
public string base64Content { get; set; }
public string fileType { get; set; }
}
64 changes: 64 additions & 0 deletions TickAPI/TickAPI/Common/Mail/Services/MailService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Net;
using SendGrid;
using SendGrid.Helpers.Mail;
using TickAPI.Common.Mail.Abstractions;
using TickAPI.Common.Mail.Models;
using TickAPI.Common.Results;

namespace TickAPI.Common.Mail.Services;

public class MailService : IMailService
{
private SendGridClient _client;
private EmailAddress _fromEmailAddress;

public MailService(IConfiguration configuration)
{
var apiKey = configuration["SendGrid:ApiKey"];
_client = new SendGridClient(apiKey);
var fromEmail = configuration["SendGrid:FromEmail"];
var fromName = configuration["SendGrid:FromName"];
_fromEmailAddress = new EmailAddress(fromEmail, fromName);
}

public async Task<Result> SendTicketAsync(string toEmail, string toLogin, string eventName, byte[] pdfData)
{
var subject = $"Ticket for {eventName}";
var htmlContent = "<strong>Download your ticket from attachments</strong>";
var base64Content = Convert.ToBase64String(pdfData);
List<MailAttachment> attachments =
[
new MailAttachment
{
base64Content = base64Content,
fileName = "ticket.pdf",
fileType = "application/pdf"
}
];
var res = await SendMailAsync(toEmail, toLogin, subject, htmlContent, attachments);
return res;
}

public async Task<Result> SendMailAsync(string toEmail, string toLogin, string subject, string content,
List<MailAttachment>? attachments = null)
{
var toEmailAddress = new EmailAddress(toEmail, toLogin);
var msg = MailHelper.CreateSingleEmail(_fromEmailAddress, toEmailAddress, subject,
null, content);
if (attachments != null)
{
foreach (var a in attachments)
{
msg.AddAttachment(a.fileName, a.base64Content, a.fileType);
}
}

var response = await _client.SendEmailAsync(msg).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
return Result.Success();
}
return Result.Failure(500, "Error sending email");
}

}
5 changes: 4 additions & 1 deletion TickAPI/TickAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
using TickAPI.Common.Claims.Services;
using TickAPI.Common.Redis.Abstractions;
using TickAPI.Common.Redis.Services;
using TickAPI.Common.Mail.Abstractions;
using TickAPI.Common.Mail.Services;

// Builder constants
const string allowClientPolicyName = "AllowClient";
Expand All @@ -55,8 +57,8 @@
})
.AddGoogle(options =>
{
options.ClientId = builder.Configuration["Authentication:Google:ClientId"];

Check warning on line 60 in TickAPI/TickAPI/Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
options.ClientSecret = builder.Configuration["Authentication:Google:ClientSecret"];

Check warning on line 61 in TickAPI/TickAPI/Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
})
.AddJwtBearer(options =>
{
Expand All @@ -68,7 +70,7 @@
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["Authentication:Jwt:Issuer"],
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(builder.Configuration["Authentication:Jwt:SecurityKey"]))

Check warning on line 73 in TickAPI/TickAPI/Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 's' in 'byte[] Encoding.GetBytes(string s)'.
};
});

Expand Down Expand Up @@ -120,6 +122,7 @@
builder.Services.AddScoped<IDateTimeService, DateTimeService>();
builder.Services.AddScoped<IClaimsService, ClaimsService>();
builder.Services.AddScoped<IRedisService, RedisService>();
builder.Services.AddScoped<IMailService, MailService>();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
Expand Down Expand Up @@ -202,4 +205,4 @@

app.MapControllers();

app.Run();
app.Run();
2 changes: 2 additions & 0 deletions TickAPI/TickAPI/TickAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.2" />
<PackageReference Include="SendGrid" Version="9.29.3" />
<PackageReference Include="SendGrid.Extensions.DependencyInjection" Version="1.0.1" />
<PackageReference Include="StackExchange.Redis" Version="2.8.31" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.3.1" />
</ItemGroup>
Expand Down
8 changes: 7 additions & 1 deletion TickAPI/TickAPI/appsettings.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@
"SecurityKey": "IH4xhBUKl3z51Gig5MFfg4kl0yLOulGk",
"ExpirySeconds" : "3600"
}
},

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary empty line

"SendGrid": {
"ApiKey": "ApiKey",
"FromEmail": "your_mail",
"FromName": "Resellio"
}
}
}
Loading