开发者

How to write email on disk instead of sending to real address in asp.net?

开发者 https://www.devze.com 2023-02-04 19:41 出处:网络
How to write email (.eml file) on 开发者_高级运维disk instead of sending to real address in asp.net? Thanks in advance.using (var client = new SmtpClient(\"somehost\"))

How to write email (.eml file) on 开发者_高级运维disk instead of sending to real address in asp.net? Thanks in advance.


using (var client = new SmtpClient("somehost"))
{
    client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
    client.PickupDirectoryLocation = @"C:\somedirectory";
    client.Send(message);
}

or using the config file:

<configuration>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="SpecifiedPickupDirectory">
        <specifiedPickupDirectory pickupDirectoryLocation="C:\somedirectory" />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>


You can configure the SmtpClient to put emails into a configured directory instead of sending them. To do this, you need to set the DeliveryMethod to SpecifiedPickupDirectory and set the PickupDirectoryLocation:

<system.net>
    <mailSettings>
        <smtp deliveryMethod="SpecifiedPickupDirectory">
            <specifiedPickupDirectory pickupDirectoryLocation="C:\emails" />
        </smtp>
    </mailSettings>
</system.net>

When you send emails using the standard SmtpClient, they will now get saved to the specified directory instead of actually being sent.


I'm assuming that you are using SmtpClient since that's fairly standard.

Write your own SMTP implementation (it's very easy) that writes the messages that are sent through it to disk instead of actually emailing them.

Then

// mailMessage is MailMessage
var client = new SmtpClient("address.of.your.smtp.implementation");
client.Send(mailMessage);

Now your server will intercept this send request and write it to disk.

0

精彩评论

暂无评论...
验证码 换一张
取 消