How do you unit test a mail sending function

后端 未结 6 2119
礼貌的吻别
礼貌的吻别 2021-01-31 17:41

I have a function which creates a report and sends it to a user. What I\'ve done is create a mock for the email function, and verify if the \'send\' function of the email class

6条回答
  •  故里飘歌
    2021-01-31 18:34

    Can be done in following ways.

    Step 1: Navigate to your Web.Config file and add the following tags to it.

    
        
            
                
            
        
    
    

    Make sure the directory you have specified for pickup location must exist.

    Step 2 : Now test your email sending functionality. I have used button_click to test this functionality with the following code.

    SmtpClient smtp = new SmtpClient();
    MailMessage message = new MailMessage("me@gmail.com", "me@yahoo.com","My Message Subject","This is a test message");
    smtp.Send(message);
    

    Output : It will create .eml files inside the folder with a randonly generated GUID name, which is the email that we can see after receiving it. For me it created a file like c127d1d5-255d-4a5a-873c-409e23002eef.eml in E:\MailTest\ folder

    Hope this helps :)

提交回复
热议问题