C# 使用 SmtpClient 时如何保存电子邮件而不是发送?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/567765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:31:20  来源:igfitidea点击:

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

c#.netemailsmtp

提问by user17510

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.

我正在使用 SmtpClient 发送带有附件的电子邮件。但是对于某个批次,我们需要以某种方式保存 MailMessage 而不是发送它们。然后我们考虑/希望手动将消息上传到用户草稿文件夹。

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.

如果有人对此有任何经验,我将非常感谢您的帮助或指点。

采纳答案by Leah

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.configsettings like this for your batch?

在 ASP.NET 中进行测试时,我们将电子邮件保存到一个文件夹中,而不是通过电子邮件服务器发送它们。也许您可以web.config为您的批次更改这样的设置?

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

Additional Info:

附加信息:

回答by dotjoe

You can configure this with the system.netsetting in your web.config/ app.configfile.

您可以使用/文件中的system.net设置进行配置。web.configapp.config

<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.Mailto System.Net.Mail.

此外,这里有一个链接,其中包含有关从 迁移System.Web.Mail到 的信息System.Net.Mail

回答by Avram

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.

这可以提供帮助 -将 Save() 功能添加到 Microsoft.Net.Mail.MailMessage
主要思想是对 MailMessage 进行扩展,即通过反射制作保存方法。

回答by Chris S

As well as the SpecifiedPickupDirectoryinformation 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:

以及SpecifiedPickupDirectory其他答案的信息,如果您想确保您的电子邮件发送到相对于站点根目录的文件夹 - 在您不知道路径的构建服务器上进行测试时很方便 - 您可以添加快速签入您的电子邮件发送代码:

    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):

你的测试看起来像这样(确保你使用 App_Data 以便 IIS 可以写入文件夹):

    // 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));

回答by devjin

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>