52

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.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
user17510
  • 1,549
  • 5
  • 20
  • 37

5 Answers5

82

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:

Alexander
  • 2,320
  • 2
  • 25
  • 33
Leah
  • 3,322
  • 2
  • 31
  • 33
  • Thanks worked a treat. Also Avram (see below) posted a useful link which discusses changing the email message name from the auto generated GUID to your own value. – user17510 Feb 20 '09 at 00:57
  • I have tried this method, and I get an error that a "From" address is not specified. Is there a way to specify this for SpecifiedPickupDirectory in web.config? When you do network settings there is – codeMonkey Jan 19 '18 at 19:01
13

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));
Chris S
  • 64,770
  • 52
  • 221
  • 239
7

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.

Avram
  • 4,267
  • 33
  • 40
6

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.

Alexander
  • 2,320
  • 2
  • 25
  • 33
dotjoe
  • 26,242
  • 5
  • 63
  • 77
1

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>
Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
devjin
  • 51
  • 2