1111
1212namespace MustMail . App . Services . MailProcessing ;
1313
14- public partial class MessageHandler ( ILogger < MessageHandler > logger , GraphServiceClient graphClient , IOptionsMonitor < Configuration > config , RecipientResolver recipientsResolver , SenderResolver senderResolver , AttachmentHandler attachmentHandler , MessageStorage messageStorage ) : MessageStore
14+ public partial class MessageHandler ( ILogger < MessageHandler > logger , GraphServiceClient graphClient , IOptionsMonitor < Configuration > config , RecipientResolver recipientsResolver , SenderResolver senderResolver , SmtpAccountAuthorization smtpAccountAuthorization , AttachmentHandler attachmentHandler , MessageStorage messageStorage ) : MessageStore
1515{
1616 public override async Task < SmtpResponse > SaveAsync ( ISessionContext context , IMessageTransaction transaction , ReadOnlySequence < byte > buffer , CancellationToken cancellationToken )
1717 {
@@ -54,6 +54,13 @@ public override async Task<SmtpResponse> SaveAsync(ISessionContext context, IMes
5454 return SmtpResponse . NoValidRecipientsGiven ;
5555 }
5656
57+ bool recipientsAllowed = await smtpAccountAuthorization . CheckRecipientIsAllowed ( context . Authentication . User , recipients . All ) ;
58+ if ( ! recipientsAllowed )
59+ {
60+ LogRecipientsNotAllowed ( context . Authentication . User ) ;
61+ return SmtpResponse . NoValidRecipientsGiven ;
62+ }
63+
5764 // Get sender from message and SMTP transaction
5865 ResolvedSender sender = await senderResolver . ResolveSender ( transaction , message ) ;
5966
@@ -69,6 +76,13 @@ public override async Task<SmtpResponse> SaveAsync(ISessionContext context, IMes
6976 return SmtpResponse . SyntaxError ;
7077 }
7178
79+ bool senderAllowed = await smtpAccountAuthorization . CheckSenderIsAllowed ( context . Authentication . User , sender . Address ) ;
80+ if ( ! senderAllowed )
81+ {
82+ LogSenderNotAllowed ( context . Authentication . User , sender . Address ) ;
83+ return SmtpResponse . MailboxNameNotAllowed ;
84+ }
85+
7286 List < Attachment > attachments = [ ] ;
7387
7488 // If message contains attachments then extract them from the message
@@ -180,13 +194,19 @@ public override async Task<SmtpResponse> SaveAsync(ISessionContext context, IMes
180194 [ LoggerMessage ( EventId = 1104 , Level = LogLevel . Warning , Message = "Message rejected: no valid recipients were found" ) ]
181195 private partial void LogNoRecipients ( ) ;
182196
183- [ LoggerMessage ( EventId = 1105 , Level = LogLevel . Debug , Message = "Sending email via Microsoft Graph: \n {Message}" ) ]
197+ [ LoggerMessage ( EventId = 1105 , Level = LogLevel . Warning , Message = "Message rejected: The SMTP account: {Account} is not allowed to send mail to one or more of the recipients" ) ]
198+ private partial void LogRecipientsNotAllowed ( string account ) ;
199+
200+ [ LoggerMessage ( EventId = 1106 , Level = LogLevel . Warning , Message = "Message rejected: The SMTP account: {Account} is not allowed to send mail from {Sender}" ) ]
201+ private partial void LogSenderNotAllowed ( string account , string sender ) ;
202+
203+ [ LoggerMessage ( EventId = 1107 , Level = LogLevel . Debug , Message = "Sending email via Microsoft Graph: \n {Message}" ) ]
184204 private partial void LogGraphSendAttempt ( string message ) ;
185205
186- [ LoggerMessage ( EventId = 1106 , Level = LogLevel . Error , Message = "Failed to send email via Microsoft Graph for sender {Sender}" ) ]
206+ [ LoggerMessage ( EventId = 1108 , Level = LogLevel . Error , Message = "Failed to send email via Microsoft Graph for sender {Sender}" ) ]
187207 private partial void LogGraphSendFailed ( Exception exception , string sender ) ;
188208
189- [ LoggerMessage ( EventId = 1107 , Level = LogLevel . Information , Message = "Email forwarded successfully. Subject: {Subject}, Sender: {Sender} as the User(UPN): {User}, Recipients; To: {To}, Cc: {Cc}, Bcc: {Bcc}" ) ]
209+ [ LoggerMessage ( EventId = 1109 , Level = LogLevel . Information , Message = "Email forwarded successfully. Subject: {Subject}, Sender: {Sender} as the User(UPN): {User}, Recipients; To: {To}, Cc: {Cc}, Bcc: {Bcc}" ) ]
190210 private partial void LogEmailForwarded ( string ? subject , string sender , string user , IEnumerable < string > to , IEnumerable < string > cc , IEnumerable < string > bcc ) ;
191211
192212
0 commit comments