0
点赞
收藏
分享

微信扫一扫

asp.net中模拟测试smtp发信

生态人 2022-12-05 阅读 21


在asp.net 中,有时要测试发信SMTP,但如果在单元测试中,如果没方便好用的
smtp怎么办,其实还是有办法模拟的,下面讲解下:

在web.config 中设置

<system.net> 

<mailSettings>

<smtp deliveryMethod="SpecifiedPickupDirectory"> <specifiedPickupDirectory pickupDirectoryLocation="C:\Mail\"/> </smtp>

</mailSettings>

</system.net>


这样设置的话,将指定c:\mail为指定的收件箱的目录了,
代码可以编写如下:

protected void btnMail_Click(object sender, EventArgs e){    MailMessage message = new MailMessage("abc@somedomain.com","abc@abcdefgh.com",Newsletter", "This is a test mail");   

SmtpClient client = new SmtpClient("localhost");

client.Send(message);}


}


这样的话,会发现运行后,在c:\mail下会有SMTP格式的你发的信了.
这个测试方法其实是很有用的,因为有时要涉及到用SMTP的一些信息交换平台的报文,
用这个测试方法就很好,可以清楚看到报文头和报文主体的内容呢

举报

相关推荐

0 条评论