3
3
using Cofoundry . Core . Mail . Internal ;
4
4
using SendGrid ;
5
5
using SendGrid . Helpers . Mail ;
6
- using System ;
7
- using System . Collections . Generic ;
8
- using System . Threading . Tasks ;
9
6
10
- namespace Cofoundry . Plugins . Mail . SendGrid . Internal
7
+ namespace Cofoundry . Plugins . Mail . SendGrid . Internal ;
8
+
9
+ public class SendGridMailDispatchSession : IMailDispatchSession
11
10
{
12
- public class SendGridMailDispatchSession : IMailDispatchSession
11
+ private readonly Queue < SendGridMessage > _mailQueue = new Queue < SendGridMessage > ( ) ;
12
+ private readonly Core . Mail . MailSettings _mailSettings ;
13
+ private readonly SendGridSettings _sendGridSettings ;
14
+ private readonly SendGridClient _sendGridClient ;
15
+ private readonly DebugMailDispatchSession _debugMailDispatchSession ;
16
+
17
+ public SendGridMailDispatchSession (
18
+ Core . Mail . MailSettings mailSettings ,
19
+ SendGridSettings sendGridSettings ,
20
+ IPathResolver pathResolver
21
+ )
13
22
{
14
- private readonly Queue < SendGridMessage > _mailQueue = new Queue < SendGridMessage > ( ) ;
15
- private readonly Core . Mail . MailSettings _mailSettings ;
16
- private readonly SendGridSettings _sendGridSettings ;
17
- private readonly SendGridClient _sendGridClient ;
18
- private readonly DebugMailDispatchSession _debugMailDispatchSession ;
19
-
20
- public SendGridMailDispatchSession (
21
- Core . Mail . MailSettings mailSettings ,
22
- SendGridSettings sendGridSettings ,
23
- IPathResolver pathResolver
24
- )
25
- {
26
- _mailSettings = mailSettings ;
27
- _sendGridSettings = sendGridSettings ;
23
+ _mailSettings = mailSettings ;
24
+ _sendGridSettings = sendGridSettings ;
28
25
29
- if ( _mailSettings . SendMode == MailSendMode . LocalDrop )
30
- {
31
- _debugMailDispatchSession = new DebugMailDispatchSession ( mailSettings , pathResolver ) ;
32
- }
33
- else
34
- {
35
- _sendGridClient = new SendGridClient ( _sendGridSettings . ApiKey ) ;
36
- }
26
+ if ( _mailSettings . SendMode == MailSendMode . LocalDrop )
27
+ {
28
+ _debugMailDispatchSession = new DebugMailDispatchSession ( mailSettings , pathResolver ) ;
37
29
}
38
-
39
- public void Add ( MailMessage mailMessage )
30
+ else
40
31
{
41
- var messageToSend = FormatMessage ( mailMessage ) ;
32
+ _sendGridClient = new SendGridClient ( _sendGridSettings . ApiKey ) ;
33
+ }
34
+ }
42
35
43
- if ( _mailSettings . SendMode == MailSendMode . LocalDrop )
44
- {
45
- _debugMailDispatchSession . Add ( mailMessage ) ;
46
- return ;
47
- }
36
+ public void Add ( MailMessage mailMessage )
37
+ {
38
+ var messageToSend = FormatMessage ( mailMessage ) ;
48
39
49
- _mailQueue . Enqueue ( messageToSend ) ;
40
+ if ( _mailSettings . SendMode == MailSendMode . LocalDrop )
41
+ {
42
+ _debugMailDispatchSession . Add ( mailMessage ) ;
43
+ return ;
50
44
}
51
45
52
- public async Task FlushAsync ( )
46
+ _mailQueue . Enqueue ( messageToSend ) ;
47
+ }
48
+
49
+ public async Task FlushAsync ( )
50
+ {
51
+ if ( _mailSettings . SendMode == MailSendMode . LocalDrop )
53
52
{
54
- if ( _mailSettings . SendMode == MailSendMode . LocalDrop )
55
- {
56
- await _debugMailDispatchSession . FlushAsync ( ) ;
57
- return ;
58
- }
53
+ await _debugMailDispatchSession . FlushAsync ( ) ;
54
+ return ;
55
+ }
59
56
60
- while ( _mailQueue . Count > 0 )
57
+ while ( _mailQueue . Count > 0 )
58
+ {
59
+ var mailItem = _mailQueue . Dequeue ( ) ;
60
+ if ( mailItem != null && _mailSettings . SendMode != MailSendMode . DoNotSend )
61
61
{
62
- var mailItem = _mailQueue . Dequeue ( ) ;
63
- if ( mailItem != null && _mailSettings . SendMode != MailSendMode . DoNotSend )
64
- {
65
- await _sendGridClient . SendEmailAsync ( mailItem ) ;
66
- }
62
+ await _sendGridClient . SendEmailAsync ( mailItem ) ;
67
63
}
68
64
}
65
+ }
69
66
70
- public void Dispose ( )
71
- {
72
- _debugMailDispatchSession ? . Dispose ( ) ;
73
- }
67
+ public void Dispose ( )
68
+ {
69
+ _debugMailDispatchSession ? . Dispose ( ) ;
70
+ }
74
71
75
- private SendGridMessage FormatMessage ( MailMessage message )
76
- {
77
- if ( message == null ) throw new ArgumentNullException ( nameof ( message ) ) ;
72
+ private SendGridMessage FormatMessage ( MailMessage message )
73
+ {
74
+ if ( message == null ) throw new ArgumentNullException ( nameof ( message ) ) ;
78
75
79
- var messageToSend = new SendGridMessage ( ) ;
76
+ var messageToSend = new SendGridMessage ( ) ;
80
77
81
- var toAddress = GetMailToAddress ( message ) ;
82
- messageToSend . AddTo ( toAddress ) ;
83
- messageToSend . Subject = message . Subject ;
84
- if ( message . From != null )
85
- {
86
- messageToSend . SetFrom ( CreateMailAddress ( message . From . Address , message . From . DisplayName ) ) ;
87
- }
88
- else
89
- {
90
- messageToSend . SetFrom ( CreateMailAddress ( _mailSettings . DefaultFromAddress , _mailSettings . DefaultFromAddressDisplayName ) ) ;
91
- }
78
+ var toAddress = GetMailToAddress ( message ) ;
79
+ messageToSend . AddTo ( toAddress ) ;
80
+ messageToSend . Subject = message . Subject ;
81
+ if ( message . From != null )
82
+ {
83
+ messageToSend . SetFrom ( CreateMailAddress ( message . From . Address , message . From . DisplayName ) ) ;
84
+ }
85
+ else
86
+ {
87
+ messageToSend . SetFrom ( CreateMailAddress ( _mailSettings . DefaultFromAddress , _mailSettings . DefaultFromAddressDisplayName ) ) ;
88
+ }
92
89
93
- SetMessageBody ( messageToSend , message . HtmlBody , message . TextBody ) ;
90
+ SetMessageBody ( messageToSend , message . HtmlBody , message . TextBody ) ;
94
91
95
- return messageToSend ;
96
- }
92
+ return messageToSend ;
93
+ }
97
94
98
- private EmailAddress GetMailToAddress ( MailMessage message )
95
+ private EmailAddress GetMailToAddress ( MailMessage message )
96
+ {
97
+ EmailAddress toAddress ;
98
+ if ( _mailSettings . SendMode == MailSendMode . SendToDebugAddress )
99
99
{
100
- EmailAddress toAddress ;
101
- if ( _mailSettings . SendMode == MailSendMode . SendToDebugAddress )
100
+ if ( string . IsNullOrEmpty ( _mailSettings . DebugEmailAddress ) )
102
101
{
103
- if ( string . IsNullOrEmpty ( _mailSettings . DebugEmailAddress ) )
104
- {
105
- throw new Exception ( "MailSendMode.SendToDebugAddress requested but Cofoundry:Mail:DebugEmailAddress setting is not defined." ) ;
106
- }
107
- toAddress = CreateMailAddress ( _mailSettings . DebugEmailAddress , message . To . DisplayName ) ;
102
+ throw new Exception ( "MailSendMode.SendToDebugAddress requested but Cofoundry:Mail:DebugEmailAddress setting is not defined." ) ;
108
103
}
109
- else
110
- {
111
- toAddress = new EmailAddress ( message . To . Address , message . To . DisplayName ) ;
112
- }
113
- return toAddress ;
104
+ toAddress = CreateMailAddress ( _mailSettings . DebugEmailAddress , message . To . DisplayName ) ;
114
105
}
115
-
116
- private EmailAddress CreateMailAddress ( string email , string displayName )
106
+ else
117
107
{
118
- // In other libraries we catch validation exceptions here, but SendGrid does not throw any so it is omitted
119
- if ( string . IsNullOrEmpty ( displayName ) )
120
- {
121
- return new EmailAddress ( email ) ;
122
- }
123
-
124
- return new EmailAddress ( email , displayName ) ;
108
+ toAddress = new EmailAddress ( message . To . Address , message . To . DisplayName ) ;
125
109
}
110
+ return toAddress ;
111
+ }
126
112
127
- private void SetMessageBody ( SendGridMessage message , string bodyHtml , string bodyText )
113
+ private EmailAddress CreateMailAddress ( string email , string displayName )
114
+ {
115
+ // In other libraries we catch validation exceptions here, but SendGrid does not throw any so it is omitted
116
+ if ( string . IsNullOrEmpty ( displayName ) )
128
117
{
129
- var hasHtmlBody = ! string . IsNullOrWhiteSpace ( bodyHtml ) ;
130
- var hasTextBody = ! string . IsNullOrWhiteSpace ( bodyText ) ;
131
- if ( ! hasHtmlBody && ! hasTextBody )
132
- {
133
- throw new ArgumentException ( "An email must have either a html or text body" ) ;
134
- }
118
+ return new EmailAddress ( email ) ;
119
+ }
120
+
121
+ return new EmailAddress ( email , displayName ) ;
122
+ }
135
123
136
- message . HtmlContent = bodyHtml ;
137
- message . PlainTextContent = bodyText ;
124
+ private void SetMessageBody ( SendGridMessage message , string bodyHtml , string bodyText )
125
+ {
126
+ var hasHtmlBody = ! string . IsNullOrWhiteSpace ( bodyHtml ) ;
127
+ var hasTextBody = ! string . IsNullOrWhiteSpace ( bodyText ) ;
128
+ if ( ! hasHtmlBody && ! hasTextBody )
129
+ {
130
+ throw new ArgumentException ( "An email must have either a html or text body" ) ;
138
131
}
132
+
133
+ message . HtmlContent = bodyHtml ;
134
+ message . PlainTextContent = bodyText ;
139
135
}
140
136
}
0 commit comments