@@ -75,14 +75,27 @@ private static SmtpClient CreateClient(EmailConfiguration config)
7575 return client ;
7676 }
7777
78- private static async Task ConnectAndAuthAsync ( SmtpClient client , EmailConfiguration config , CancellationToken ct )
78+ private static async Task ConnectAndAuthAsync ( SmtpClient client ,
79+ EmailConfiguration config ,
80+ CancellationToken ct )
7981 {
80- var socketOptions = ResolveSocketOptions ( config . SmtpPort ) ;
81- await client . ConnectAsync ( config . SmtpServer , config . SmtpPort , socketOptions , ct ) ;
82+ await client . ConnectAsync (
83+ config . SmtpServer ,
84+ config . SmtpPort ,
85+ ResolveSocketOptions ( config . SmtpPort ) ,
86+ ct ) ;
8287
83- if ( ! string . IsNullOrWhiteSpace ( config . SmtpUsername ) )
88+ var hasUser = ! string . IsNullOrWhiteSpace ( config . SmtpUsername ) ;
89+ var hasPass = ! string . IsNullOrWhiteSpace ( config . SmtpPassword ) ;
90+
91+ if ( hasUser != hasPass )
8492 {
85- await client . AuthenticateAsync ( config . SmtpUsername , config . SmtpPassword , ct ) ;
93+ throw new InvalidOperationException ( "SMTP username and password must both be set or both be empty." ) ;
94+ }
95+
96+ if ( hasUser )
97+ {
98+ await client . AuthenticateAsync ( config . SmtpUsername ! , config . SmtpPassword ! , ct ) ;
8699 }
87100 }
88101
@@ -98,36 +111,25 @@ private static SecureSocketOptions ResolveSocketOptions(int port)
98111
99112 private static MimeMessage CreateMimeMessage ( EmailConfiguration config , EmailMessage emailMessage )
100113 {
101- var senderEmail = ! string . IsNullOrWhiteSpace ( config . SenderEmail ) ? config . SenderEmail : config . SmtpUsername ;
114+ if ( string . IsNullOrWhiteSpace ( config . SenderEmail ) )
115+ {
116+ throw new InvalidOperationException ( "SenderEmail is required." ) ;
117+ }
102118
103119 var message = new MimeMessage ( ) ;
104120
105- if ( ! string . IsNullOrWhiteSpace ( config . SenderName ) && ! string . IsNullOrWhiteSpace ( senderEmail ) )
106- {
107- message . From . Add ( new MailboxAddress ( config . SenderName , senderEmail ) ) ;
108- }
109- else if ( ! string . IsNullOrWhiteSpace ( senderEmail ) )
110- {
111- message . From . Add ( MailboxAddress . Parse ( senderEmail ) ) ;
112- }
113- else
114- {
115- throw new InvalidOperationException ( "SenderEmail (or SmtpUsername fallback) is required." ) ;
116- }
121+ message . From . Add ( ! string . IsNullOrWhiteSpace ( config . SenderName )
122+ ? new MailboxAddress ( config . SenderName , config . SenderEmail )
123+ : MailboxAddress . Parse ( config . SenderEmail ) ) ;
117124
118125 message . To . AddRange ( ParseDistinct ( emailMessage . Recipients ) ) ;
119126 message . Subject = emailMessage . Subject ;
120127
121- var builder = new BodyBuilder ( ) ;
122-
123- if ( emailMessage . IsBodyHtml )
128+ var builder = new BodyBuilder
124129 {
125- builder . HtmlBody = emailMessage . Body ;
126- }
127- else
128- {
129- builder . TextBody = emailMessage . Body ;
130- }
130+ HtmlBody = emailMessage . IsBodyHtml ? emailMessage . Body : null ,
131+ TextBody = emailMessage . IsBodyHtml ? null : emailMessage . Body
132+ } ;
131133
132134 if ( emailMessage . Attachments is { Count : > 0 } )
133135 {
0 commit comments