How can I save an email instead of sending when using SmtpClient?

风格不统一 提交于 2019-12-17 10:17:06

问题


I am using SmtpClient to send an email with an attachment. However for a certain batch we need to somehow save the MailMessage instead of sending them. We are then thinking/hoping to manually upload the messages to the users drafts folder.

Is it possible to save these messages with the attachment intact (impossible, I would have thought). Or alternatively upload the messages to a folder in the users account?

If anyone has any experience of this, I'd much appreciate a bit of help or a pointer.


回答1:


When testing in ASP.NET we save our emails to a folder rather then send them through an email server. Maybe you could change yourweb.config settings like this for your batch?

<system.net>
  <mailSettings>
    <smtp deliveryMethod="SpecifiedPickupDirectory">
      <specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\mail\"/>
    </smtp>
  </mailSettings>
</system.net>

Additional Info:

  • MSDN: <specifiedPickupDirectory> Element (Network Settings)
  • Configuring SmtpClient to drop emails in a folder on disk



回答2:


As well as the SpecifiedPickupDirectory information of the other answers, if you want to ensure your emails are sent to a folder relative to the site root - handy in testing on build servers where you don't know the paths - you can add a quick check in your email sending code:

    SmtpClient client = new SmtpClient();
    ...

    // Add "~" support for pickupdirectories.
    if (client.DeliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory && client.PickupDirectoryLocation.StartsWith("~"))
    {
        string root = AppDomain.CurrentDomain.BaseDirectory;
        string pickupRoot = client.PickupDirectoryLocation.Replace("~/", root);
        pickupRoot = pickupRoot.Replace("/",@"\");
        client.PickupDirectoryLocation = pickupRoot;
    }

And your tests will look something like this (make sure you use App_Data so IIS can write to the folder):

    // Arrange - get SitePath from AppDomain.Current.BaseDirectory + ..\
    string pickupPath = Path.Combine(SitePath, "App_Data", "TempSmtp");
    if (!Directory.Exists(pickupPath))
        Directory.CreateDirectory(pickupPath);

    foreach (string file in Directory.GetFiles(pickupPath, "*.eml"))
    {
        File.Delete(file);
    }

    // Act (send some emails)

    // Assert
    Assert.That(Directory.GetFiles(pickupPath, "*.eml").Count(), Is.EqualTo(1));



回答3:


This can help - Adding Save() functionality to Microsoft.Net.Mail.MailMessage
The main ideia, make an extension to MailMessage ,that by reflection making a save method.




回答4:


You can configure this with the system.net setting in your web.config / app.config file.

<system.net>
  <mailSettings>
    <smtp deliveryMethod="Network">
      <network host="mail.mydomain.com" port="25" />
    </smtp>
    <!-- Use this setting for development
    <smtp deliveryMethod="SpecifiedPickupDirectory">
      <specifiedPickupDirectory pickupDirectoryLocation="C:\Temp" />
    </smtp>
    -->
  </mailSettings>
</system.net>

Also, here's a link with info on migrating from System.Web.Mail to System.Net.Mail.




回答5:


A bug also requires adding as a workaround in some versions of the framework. So the completed version looks like:

<system.net>
  <mailSettings>
    <smtp deliveryMethod="SpecifiedPickupDirectory">
        <specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\mail\"/>
        <network host="localhost" />
    </smtp>
  </mailSettings>
</system.net>


来源:https://stackoverflow.com/questions/567765/how-can-i-save-an-email-instead-of-sending-when-using-smtpclient

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!