简体   繁体   English

将 email 保存到具有指定文件名的目录在 .NET 5.0 中不起作用

[英]Saving email to the directory with specified file name is not working in .NET 5.0

I am trying to follow the approach suggested in a post by Allan Eagle in code-project.我正在尝试遵循 Allan Eagle 在代码项目中的帖子中建议的方法。 This same approach was working fine up until .NET Core 3.1 , but not with .NET 5.0 ..NET Core 3.1之前,这种方法都可以正常工作,但在.NET 5.0之前就不行了。 Here is the save method I created,这是我创建的保存方法,

 private void Save(MailMessage message, string filePath)
    {
        var assembly = typeof(SmtpClient).Assembly;
        var mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");
        const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic;
        using (var fileStream = new FileStream(filePath, FileMode.Create))
        {
            var mailWriterContructor = mailWriterType.GetConstructors(bindingFlags)[0];
            var mailWriter = mailWriterContructor.Invoke(new object[] { fileStream });//<-- This line throws error saying parameter mismatch
            var sendMethod = typeof(MailMessage).GetMethod("Send", bindingFlags);
            sendMethod.Invoke(message, bindingFlags, null, new[] { mailWriter, true, true }, null);
            var closeMethod = mailWriter.GetType().GetMethod("Close", bindingFlags);
            closeMethod.Invoke(mailWriter, bindingFlags, null, new object[] { }, null);
        }
    }

I checked all the available underlying invoke methods and tried working with them passing needed parameters but non of them worked for me.我检查了所有可用的底层调用方法并尝试使用它们传递所需的参数,但它们都不适合我。

Error Message: "Parameter count mismatch."错误消息:“参数计数不匹配。”
Inner Exception: null内部异常:null
Stack Trace:堆栈跟踪:

 at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
   at..//user written line info  

Any help to solve this issue or new approach to achieve same thing using System.Net.Mail will be highly appreciated.任何解决此问题的帮助或使用System.Net.Mail实现相同目标的新方法将不胜感激。

To explicitly give you an answer based on first comment,根据第一条评论明确地给你一个答案,

var encodeForTransport = false;
var mailWriter = mailWriterContructor.Invoke(new object[] { fileStream, encodeForTransport  });

As I mentioned in the comments, this article is obsolete in 2020. Despite the date, it was actually written in 2009 and stopped working in 2014, when some of the internal methods changed signature.正如我在评论中提到的,这篇文章在 2020 年已经过时了。尽管有日期,但它实际上是在 2009 年编写的,并在 2014 年停止工作,当时一些内部方法更改了签名。 In any case, the System.Net.Mail namespace shouldn't be used because, as Microsoft strongly warns:在任何情况下,都不应使用System.Net.Mail命名空间,因为正如 Microsoft 强烈警告的那样:

Important重要的

We don't recommend that you use the SmtpClient class for new development because SmtpClient doesn't support many modern protocols.我们不建议您使用 SmtpClient class 进行新开发,因为 SmtpClient 不支持许多现代协议。 Use MailKit or other libraries instead.请改用MailKit或其他库。 For more information, see SmtpClient shouldn't be used on GitHub.有关详细信息,请参阅SmtpClient 不应在 GitHub 上使用

MailKit (or rather the MimeKit library it's built upon) already supports saving and loading Mail messages. MailKit(或者更确切地说是它所构建的 MimeKit 库)已经支持保存和加载邮件消息。 From Q: How do I save messages?来自问:我如何保存消息? the answer is a simple:答案很简单:

message.WriteTo("message.eml");

You can use MailKit's POP3 or IMAP4 clients to retrieve messages.您可以使用 MailKit 的 POP3 或 IMAP4 客户端来检索邮件。 The DownloadMessages example show how to download messages from GMail and save them: DownloadMessages示例展示了如何从 GMail 下载消息并保存它们:

        using (var client = new Pop3Client (new ProtocolLogger ("pop3.log"))) {
            client.Connect ("pop.gmail.com", 995, SecureSocketOptions.SslOnConnect);

            client.Authenticate ("username", "password");

            for (int i = 0; i < client.Count; i++) {
                var message = client.GetMessage (i);

                // write the message to a file
                message.WriteTo (string.Format ("{0}.msg", i));

                // mark the message for deletion
                client.DeleteMessage (i);
            }

            client.Disconnect (true);
        }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM