22using System . Collections . Generic ;
33using System . IO ;
44using System . Linq ;
5+ using System . Net . Sockets ;
56using System . Threading ;
67using System . Threading . Tasks ;
78using NLog . Config ;
@@ -80,10 +81,9 @@ public void SendMailWithHeaderFooter()
8081 mailTarget . Footer = " *** End *** " ;
8182 } , 1 ) ;
8283
83- var mailMessage = transactions . LastOrDefault ( ) ? . Message as SmtpServer . Mail . ITextMessage ;
84- Assert . NotNull ( mailMessage ) ;
85- mailMessage . Content . Position = 0 ;
86- var mailBody = new StreamReader ( mailMessage . Content ) . ReadToEnd ( ) ;
84+ var receivedMessage = transactions . LastOrDefault ( ) ;
85+ Assert . NotNull ( receivedMessage ) ;
86+ var mailBody = receivedMessage . GetBodyAsString ( ) ;
8787 Assert . NotNull ( mailBody ) ;
8888 Assert . Contains ( "*** Begin ***" , mailBody ) ;
8989 Assert . Contains ( "*** End ***" , mailBody ) ;
@@ -131,16 +131,15 @@ public void SendMailBatch()
131131 logger . Info ( "hello starting mail!" ) ;
132132 } , 1 ) ;
133133
134- var mailMessage = transactions . LastOrDefault ( ) ? . Message as SmtpServer . Mail . ITextMessage ;
135- Assert . NotNull ( mailMessage ) ;
136- mailMessage . Content . Position = 0 ;
137- var mailBody = new StreamReader ( mailMessage . Content ) . ReadToEnd ( ) ;
134+ var receivedMessage = transactions . LastOrDefault ( ) ;
135+ Assert . NotNull ( receivedMessage ) ;
136+ var mailBody = receivedMessage . GetBodyAsString ( ) ;
138137 Assert . NotNull ( mailBody ) ;
139138 Assert . Contains ( "hello starting mail!" , mailBody ) ;
140139 Assert . Contains ( "hello first mail!" , mailBody ) ;
141140 }
142141
143- private static IList < IMessageTransaction > SendTest ( Action createConfig , int toCount )
142+ private static IList < ReceivedMessage > SendTest ( Action createConfig , int toCount )
144143 {
145144 var countdownEvent = new CountdownEvent ( 1 ) ;
146145
@@ -151,6 +150,8 @@ private static IList<IMessageTransaction> SendTest(Action createConfig, int toCo
151150
152151 Task . Run ( async ( ) => await smtpServer . StartAsync ( cancellationToken . Token ) , cancellationToken . Token ) ;
153152
153+ WaitForSmtpServer ( ) ;
154+
154155 createConfig ( ) ;
155156
156157 var logger = LogManager . GetLogger ( "logger1" ) ;
@@ -178,14 +179,26 @@ private static void AssertMailBox(string expected, IMailbox mailbox)
178179 Assert . Equal ( expected , mailbox . User + "@" + mailbox . Host ) ;
179180 }
180181
181- private static string GetReceivedBody ( IMessageTransaction receivedMessage )
182+ private static string GetReceivedBody ( ReceivedMessage receivedMessage )
182183 {
183- if ( receivedMessage . Message is ITextMessage textMessage )
184+ return receivedMessage . GetBodyAsString ( ) ;
185+ }
186+
187+ private static void WaitForSmtpServer ( int port = 587 , int maxAttempts = 50 )
188+ {
189+ for ( var i = 0 ; i < maxAttempts ; i ++ )
184190 {
185- var streamReader = new StreamReader ( textMessage . Content ) ;
186- return streamReader . ReadToEnd ( ) ;
191+ try
192+ {
193+ using var client = new TcpClient ( ) ;
194+ client . Connect ( "127.0.0.1" , port ) ;
195+ return ;
196+ }
197+ catch ( SocketException )
198+ {
199+ Thread . Sleep ( 100 ) ;
200+ }
187201 }
188- throw new NotSupportedException ( "only ITextMessage supported" ) ;
189202 }
190203
191204 private static SmtpServer . SmtpServer CreateSmtpServer ( MessageStore store )
@@ -197,12 +210,13 @@ private static SmtpServer.SmtpServer CreateSmtpServer(MessageStore store)
197210 var options = new SmtpServerOptionsBuilder ( )
198211 . ServerName ( "localhost" )
199212 . Port ( 25 , 587 )
200- . MessageStore ( store )
201- . UserAuthenticator ( userAuthenticatorFactory )
202213 . Build ( ) ;
203214
204- var smtpServer = new SmtpServer . SmtpServer ( options ) ;
205- return smtpServer ;
215+ var serviceProvider = new SmtpServer . ComponentModel . ServiceProvider ( ) ;
216+ serviceProvider . Add ( ( SmtpServer . Storage . IMessageStoreFactory ) store ) ;
217+ serviceProvider . Add ( userAuthenticatorFactory ) ;
218+
219+ return new SmtpServer . SmtpServer ( options , serviceProvider ) ;
206220 }
207221
208222 private static MailTarget CreateNLogConfig ( )
0 commit comments