简体   繁体   English

发送到 System.Net.Mail.SmtpClient 等文件夹

[英]Send to folder like System.Net.Mail.SmtpClient

I'm using MailKit.Net.Smtp.SmtpClient and I'm trying to figure out if there is a way to configure this client so that it drop emails to a folder much like in way that the PickupDirectoryLocation property affects a System.Net.Mail.SmtpClient dotNet instance.我正在使用 MailKit.Net.Smtp.SmtpClient 并且我试图弄清楚是否有一种方法可以配置此客户端,以便它将电子邮件放入文件夹,就像PickupDirectoryLocation属性影响 System.Net 的方式一样。 Mail.SmtpClient dotNet 实例。 The need here is for development and debugging so we can safely capture the messages that would have gone out.这里需要进行开发和调试,以便我们可以安全地捕获可能会发出的消息。

I'm open to another technique for debugging emails generated by MailKit so long as it does not involve any external services like an SMTP server.我愿意接受另一种调试 MailKit 生成的电子邮件的技术,只要它不涉及任何外部服务,如 SMTP 服务器。

The way to do this is to do the following:这样做的方法是执行以下操作:

public static void SaveToPickupDirectory (MimeMessage message, string pickupDirectory)
{
    do {
        // Generate a random file name to save the message to.
        var path = Path.Combine (pickupDirectory, Guid.NewGuid ().ToString () + ".eml");
        Stream stream;

        try {
            // Attempt to create the new file.
            stream = File.Open (path, FileMode.CreateNew);
        } catch (IOException) {
        // If the file already exists, try again with a new Guid.
            if (File.Exists (path))
                continue;

            // Otherwise, fail immediately since it probably means that there is
            // no graceful way to recover from this error.
            throw;
        }

        try {
            using (stream) {
                // IIS pickup directories expect the message to be "byte-stuffed"
                // which means that lines beginning with "." need to be escaped
                // by adding an extra "." to the beginning of the line.
                //
                // Use an SmtpDataFilter "byte-stuff" the message as it is written
                // to the file stream. This is the same process that an SmtpClient
                // would use when sending the message in a `DATA` command.
                using (var filtered = new FilteredStream (stream)) {
                    filtered.Add (new SmtpDataFilter ());

                    // Make sure to write the message in DOS (<CR><LF>) format.
                    var options = FormatOptions.Default.Clone ();
                    options.NewLineFormat = NewLineFormat.Dos;

                    message.WriteTo (options, filtered);
                    filtered.Flush ();
                    return;
                }
            }
        } catch {
            // An exception here probably means that the disk is full.
            //
            // Delete the file that was created above so that incomplete files are not
            // left behind for IIS to send accidentally.
            File.Delete (path);
            throw;
        }
    } while (true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Xamarin中使用System.Net.Mail.SmtpClient发送邮件 - How to send a mail in Xamarin using System.Net.Mail.SmtpClient System.Net.Mail.SmtpClient 在 4.7 中过时了吗? - Is System.Net.Mail.SmtpClient obsolete in 4.7? 无法使用 System.Net.Mail.SmtpClient 将 email 发送到其他域 - Unable to send email to other domains using System.Net.Mail.SmtpClient 使用System.Net.Mail.SmtpClient发送的电子邮件的字体大小 - Font size for an email sent using System.Net.Mail.SmtpClient 第三方与System.Net.Mail.SmtpClient - Third-party vs System.Net.Mail.SmtpClient 使用System.Net.Mail.SmtpClient将电子邮件发送到通讯组列表 - Sending email to a distribution list using System.Net.Mail.SmtpClient 如何为电子邮件(System.Net.Mail.SmtpClient)附件设置一个好名称 - How to set a nice name for an e-mail (System.Net.Mail.SmtpClient) attachment System.Net.Mail.SMTPClient如何执行其本地IP绑定 - How does System.Net.Mail.SMTPClient do its local IP binding 如何使用TLS和System.Net.Mail.SMTPCLient连接到googlemail.com? - How can I connect to googlemail.com using TLS and System.Net.Mail.SMTPCLient? 异步方法所需的响应时间与System.Net.Mail.SmtpClient的同步方法所需的时间差不多。 - Async method takes about same response time as sync method for System.Net.Mail.SmtpClient?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM