1+ using System ;
2+ using System . Net ;
3+ using System . Net . Mail ;
4+ using mailslurp . Api ;
5+ using mailslurp . Client ;
6+ using mailslurp . Model ;
7+ using Xunit ;
8+
9+ namespace SmtpService . Tests
10+ {
11+ public class UnitTest1
12+ {
13+ [ Fact ]
14+ public void CanSendEmailWithMailSlurpSmtp ( )
15+ {
16+ var apiKey = Environment . GetEnvironmentVariable ( "API_KEY" )
17+ ?? throw new ArgumentNullException ( "Missing API_KEY environment variable containing MailSlurp key" ) ;
18+
19+ // configure client
20+ var config = new Configuration ( ) ;
21+ config . ApiKey . Add ( "x-api-key" , apiKey ) ;
22+ var inboxController = new InboxControllerApi ( config ) ;
23+
24+ // create an smtp inbox
25+ var inbox = inboxController . CreateInboxWithOptions ( new CreateInboxDto (
26+ inboxType : CreateInboxDto . InboxTypeEnum . SMTPINBOX
27+ ) ) ;
28+ Assert . Contains ( "@mailslurp.mx" , inbox . EmailAddress ) ;
29+
30+ // get smtp host, port, password, username etc
31+ var imapSmtpAccessDetails = inboxController . GetImapSmtpAccess ( ) ;
32+ var smtpClient = new SmtpClient ( imapSmtpAccessDetails . SmtpServerHost )
33+ {
34+ Port = imapSmtpAccessDetails . SmtpServerPort ,
35+ Credentials = new NetworkCredential ( userName : imapSmtpAccessDetails . SmtpUsername , password : imapSmtpAccessDetails . SmtpPassword ) ,
36+ // disable ssl recommended
37+ EnableSsl = false
38+ } ;
39+
40+ // send email to inbox
41+ smtpClient . Send ( from : "[email protected] " , recipients : inbox . EmailAddress , subject : "This inbound" , body : "Hello" ) ; 42+
43+ // wait for email to arrive
44+ var waitController = new WaitForControllerApi ( config ) ;
45+ waitController . WaitForLatestEmail ( inboxId : inbox . Id , timeout : 30_000 ) ;
46+ }
47+ }
48+ }
0 commit comments