@@ -11,6 +11,21 @@ public class EmailService
1111 private readonly EmailAccountConfig _emailAccountConfig ;
1212 private readonly ILogger < EmailService > _logger ;
1313
14+ private static string MaskEmail ( string email )
15+ {
16+ if ( string . IsNullOrWhiteSpace ( email ) ) return string . Empty ;
17+
18+ var atIndex = email . IndexOf ( '@' ) ;
19+ if ( atIndex <= 0 || atIndex == email . Length - 1 ) return "***" ;
20+
21+ var localPart = email . Substring ( 0 , atIndex ) ;
22+ var domainPart = email . Substring ( atIndex + 1 ) ;
23+
24+ if ( localPart . Length <= 1 )
25+ return "*@" + domainPart ;
26+
27+ return localPart [ 0 ] + new string ( '*' , Math . Max ( 1 , localPart . Length - 1 ) ) + "@" + domainPart ;
28+ }
1429
1530 public EmailService ( ILogger < EmailService > logger , IOptions < EmailAccountConfig > options )
1631 {
@@ -21,7 +36,8 @@ public EmailService(ILogger<EmailService> logger, IOptions<EmailAccountConfig> o
2136 public async Task < MessageSentEventArgs > SendEmailAsync ( string subject , string body , string toName , string toAddress )
2237 {
2338 var sanitizedToAddress = toAddress . Replace ( Environment . NewLine , "" ) . Replace ( "\n " , "" ) . Replace ( "\r " , "" ) ;
24- _logger . LogDebug ( "Sending email, subject: {Subject}, recipient: {ToAddress}" , subject , sanitizedToAddress ) ;
39+ var maskedToAddress = MaskEmail ( sanitizedToAddress ) ;
40+ _logger . LogDebug ( "Sending email, subject: {Subject}, recipient: {ToAddress}" , subject , maskedToAddress ) ;
2541 body += $ "<br><p>This message was automatically sent by { BlogLink } , no need to reply.</p>";
2642 return await EmailUtils . SendEmailAsync ( _emailAccountConfig , subject , body , toName , toAddress ) ;
2743 }
0 commit comments