|
1 | 1 | namespace AspNetCoreTemplate.Services.Messaging
|
2 | 2 | {
|
3 | 3 | using System;
|
4 |
| - using System.Net.Http; |
5 |
| - using System.Net.Http.Headers; |
6 |
| - using System.Text; |
| 4 | + using System.Collections.Generic; |
| 5 | + using System.Linq; |
7 | 6 | using System.Threading.Tasks;
|
8 | 7 |
|
9 |
| - using AspNetCoreTemplate.Services.Messaging.SendGrid; |
| 8 | + using SendGrid; |
| 9 | + using SendGrid.Helpers.Mail; |
10 | 10 |
|
11 |
| - using Microsoft.AspNetCore.Identity.UI.Services; |
12 |
| - using Microsoft.Extensions.Logging; |
13 |
| - |
14 |
| - using Newtonsoft.Json; |
15 |
| - |
16 |
| - // Documentation: https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html |
17 | 11 | public class SendGridEmailSender : IEmailSender
|
18 | 12 | {
|
19 |
| - private const string AuthenticationScheme = "Bearer"; |
20 |
| - private const string BaseUrl = "https://api.sendgrid.com/v3/"; |
21 |
| - private const string SendEmailUrlPath = "mail/send"; |
| 13 | + private readonly SendGridClient client; |
22 | 14 |
|
23 |
| - private readonly string fromAddress; |
24 |
| - private readonly string fromName; |
25 |
| - private readonly HttpClient httpClient; |
26 |
| - private readonly ILogger logger; |
| 15 | + private readonly EmailAddress from; |
27 | 16 |
|
28 |
| - public SendGridEmailSender(ILoggerFactory loggerFactory, string apiKey, string fromAddress, string fromName) |
| 17 | + public SendGridEmailSender(string apiKey, string fromAddress, string fromName) |
29 | 18 | {
|
30 |
| - if (loggerFactory == null) |
31 |
| - { |
32 |
| - throw new ArgumentNullException(nameof(loggerFactory)); |
33 |
| - } |
34 |
| - |
35 |
| - if (string.IsNullOrWhiteSpace(apiKey)) |
36 |
| - { |
37 |
| - throw new ArgumentOutOfRangeException(nameof(apiKey)); |
38 |
| - } |
39 |
| - |
40 |
| - if (string.IsNullOrWhiteSpace(fromAddress)) |
41 |
| - { |
42 |
| - throw new ArgumentOutOfRangeException(nameof(fromAddress)); |
43 |
| - } |
44 |
| - |
45 |
| - if (string.IsNullOrWhiteSpace(fromName)) |
46 |
| - { |
47 |
| - throw new ArgumentOutOfRangeException(nameof(fromName)); |
48 |
| - } |
49 |
| - |
50 |
| - this.logger = loggerFactory.CreateLogger<SendGridEmailSender>(); |
51 |
| - this.httpClient = new HttpClient(); |
52 |
| - this.httpClient.DefaultRequestHeaders.Authorization = |
53 |
| - new AuthenticationHeaderValue(AuthenticationScheme, apiKey); |
54 |
| - this.httpClient.BaseAddress = new Uri(BaseUrl); |
55 |
| - this.fromAddress = fromAddress; |
56 |
| - this.fromName = fromName; |
| 19 | + this.client = new SendGridClient(apiKey); |
| 20 | + this.from = new EmailAddress(fromAddress, fromName); |
57 | 21 | }
|
58 | 22 |
|
59 |
| - public async Task SendEmailAsync(string email, string subject, string message) |
| 23 | + public async Task SendEmailAsync(string to, string subject, string htmlContent, IEnumerable<EmailAttachment> attachments = null) |
60 | 24 | {
|
61 |
| - if (string.IsNullOrWhiteSpace(this.fromAddress)) |
62 |
| - { |
63 |
| - throw new ArgumentOutOfRangeException(nameof(this.fromAddress)); |
64 |
| - } |
65 |
| - |
66 |
| - if (string.IsNullOrWhiteSpace(email)) |
| 25 | + if (string.IsNullOrWhiteSpace(subject) && string.IsNullOrWhiteSpace(htmlContent)) |
67 | 26 | {
|
68 |
| - throw new ArgumentOutOfRangeException(nameof(email)); |
| 27 | + throw new ArgumentException("Subject and message should be provided."); |
69 | 28 | }
|
70 | 29 |
|
71 |
| - if (string.IsNullOrWhiteSpace(subject) && string.IsNullOrWhiteSpace(message)) |
| 30 | + var toAddress = new EmailAddress(to); |
| 31 | + var message = MailHelper.CreateSingleEmail(this.from, toAddress, subject, null, htmlContent); |
| 32 | + if (attachments?.Any() == true) |
72 | 33 | {
|
73 |
| - throw new ArgumentException("Subject and/or message must be provided."); |
| 34 | + foreach (var attachment in attachments) |
| 35 | + { |
| 36 | + message.AddAttachment(attachment.FileName, Convert.ToBase64String(attachment.Content), attachment.MimeType); |
| 37 | + } |
74 | 38 | }
|
75 | 39 |
|
76 |
| - var msg = new SendGridMessage( |
77 |
| - new SendGridEmail(email), |
78 |
| - subject, |
79 |
| - new SendGridEmail(this.fromAddress, this.fromName), |
80 |
| - message); |
81 | 40 | try
|
82 | 41 | {
|
83 |
| - var json = JsonConvert.SerializeObject(msg); |
84 |
| - var response = await this.httpClient.PostAsync( |
85 |
| - SendEmailUrlPath, |
86 |
| - new StringContent(json, Encoding.UTF8, "application/json")); |
87 |
| - |
88 |
| - if (!response.IsSuccessStatusCode) |
89 |
| - { |
90 |
| - // See if we can read the response for more information, then log the error |
91 |
| - var errorJson = await response.Content.ReadAsStringAsync(); |
92 |
| - throw new Exception( |
93 |
| - $"SendGrid indicated failure! Code: {response.StatusCode}, reason: {errorJson}"); |
94 |
| - } |
| 42 | + var response = await this.client.SendEmailAsync(message); |
| 43 | + Console.WriteLine(response.StatusCode); |
| 44 | + Console.WriteLine(await response.Body.ReadAsStringAsync()); |
95 | 45 | }
|
96 |
| - catch (Exception ex) |
| 46 | + catch (Exception e) |
97 | 47 | {
|
98 |
| - this.logger.LogError($"Exception during sending email: {ex}"); |
| 48 | + Console.WriteLine(e); |
| 49 | + throw; |
99 | 50 | }
|
100 | 51 | }
|
101 | 52 | }
|
|
0 commit comments