Created
February 2, 2021 04:58
-
-
Save danielplawgo/c890d7789ad3cdd09405d9727a01f51f to your computer and use it in GitHub Desktop.
SendGrid - wysyłka wiadomości email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"SendGrid": { | |
"ApiKey": "wygenerowany klucz api", | |
"SenderEmail": "blogtest@plawgo.pl", | |
"SenderName": "Blog PROGRAMUJE.NET" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"SendGrid": { | |
"ApiKey": "wygenerowany klucz api", | |
"SenderEmail": "blogtest@plawgo.pl", | |
"SenderName": "Blog PROGRAMUJE.NET", | |
"TemplateId": "id szablonu" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SendGridConfig | |
{ | |
public string ApiKey { get; set; } | |
public string SenderEmail { get; set; } | |
public string SenderName { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SendGridConfig | |
{ | |
public string ApiKey { get; set; } | |
public string SenderEmail { get; set; } | |
public string SenderName { get; set; } | |
public string TemplateId { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Route("api/[controller]")] | |
[ApiController] | |
public class SendGridController : ControllerBase | |
{ | |
private readonly SendGridConfig _config; | |
public SendGridController(IOptions<SendGridConfig> config) | |
{ | |
_config = config.Value; | |
} | |
[HttpPost] | |
public async Task<IActionResult> Send(SendDto request) | |
{ | |
var client = new SendGridClient(_config.ApiKey); | |
var msg = MailHelper.CreateSingleEmail(new EmailAddress(_config.SenderEmail, _config.SenderName), | |
new EmailAddress(request.Email, request.Name), | |
"Email Subject", | |
"Email body", | |
"<strong>Email body</strong>"); | |
var response = await client.SendEmailAsync(msg); | |
return await ProcessResponse(response); | |
} | |
private async Task<IActionResult> ProcessResponse(Response response) | |
{ | |
var responseContent = await response.Body.ReadAsStringAsync(); | |
return string.IsNullOrEmpty(responseContent) ? Ok() : BadRequest(responseContent); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPut] | |
public async Task<IActionResult> SendWithTemplate(SendDto request) | |
{ | |
var client = new SendGridClient(_config.ApiKey); | |
var msg = MailHelper.CreateSingleTemplateEmail(new EmailAddress(_config.SenderEmail, _config.SenderName), | |
new EmailAddress(request.Email, request.Name), | |
_config.TemplateId, | |
new { Name = request.Name}); | |
var response = await client.SendEmailAsync(msg); | |
return await ProcessResponse(response); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.Configure<SendGridConfig>(Configuration.GetSection("SendGrid")); | |
services.AddControllers(); | |
services.AddSwaggerGen(c => | |
{ | |
c.SwaggerDoc("v1", new OpenApiInfo { Title = "SendGridTests", Version = "v1" }); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment