MvcMailer,找到电子邮件的文件名

时间:2018-09-04 09:48:08

标签: c# asp.net-mvc-5 mvcmailer

我正在使用MvcMailer将电子邮件保存到我的asp.net mvc Web应用程序本地的指定目录中。但是,我想将电子邮件的文件名(例如90b871cd-038f-400a-b4d7-01f87e8c3c26.eml)保存在数据库中,以后可以使用另一个exe来访问该电子邮件,以从提取文件夹中发送电子邮件。

请问我如何从邮件对象中检索文件名?

var mail = Mailer.Example_Mail()
mail.To.Add("some@somedomain.com");
mail.Send();

<smtp from="some@somedomain.com" deliveryMethod="SpecifiedPickupDirectory">
    <network host="localhost" />
    <specifiedPickupDirectory pickupDirectoryLocation="c:\temp\" />
</smtp>

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为这对于寻求相同问题答案的人可能会有帮助。我设法克服了编写System.Net.Mail.MailMessage.Send()反射的问题,如下所示。

    public static string SaveToTemp(this MailMessage Message)
    {
        SmtpClient smtp = new SmtpClient();
        string fileName = Guid.NewGuid().ToString() + ".eml";

        string fileNameWithPath = Path.Combine(smtp.PickupDirectoryLocation, fileName);

        Assembly assembly = typeof(SmtpClient).Assembly;
        Type _mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");

        using (FileStream _fileStream = new FileStream(fileNameWithPath, FileMode.Create))
        {
            // Get reflection info for MailWriter contructor
            ConstructorInfo _mailWriterContructor =
                _mailWriterType.GetConstructor(
                    BindingFlags.Instance | BindingFlags.NonPublic,
                    null,
                    new Type[] { typeof(Stream) }, 
                    null);

            // Construct MailWriter object with our FileStream
            object _mailWriter = _mailWriterContructor.Invoke(new object[] { _fileStream });

            // Get reflection info for Send() method on MailMessage
            MethodInfo _sendMethod =
                typeof(MailMessage).GetMethod(
                    "Send",
                    BindingFlags.Instance | BindingFlags.NonPublic);

            // Call method passing in MailWriter
            _sendMethod.Invoke(
                Message,
                BindingFlags.Instance | BindingFlags.NonPublic,
                null,
                new object[] { _mailWriter,true, true },
                null);

            // Finally get reflection info for Close() method on our MailWriter
            MethodInfo _closeMethod =
                _mailWriter.GetType().GetMethod(
                    "Close",
                    BindingFlags.Instance | BindingFlags.NonPublic);

            // Call close method
            _closeMethod.Invoke(
                _mailWriter,
                BindingFlags.Instance | BindingFlags.NonPublic,
                null,
                new object[] { },
                null);
        }
        return fileNameWithPath;
    }     

来电者:

var mail = Mailer.Example_Mail()
mail.To.Add("some@somedomain.com");
var fileName = mail.SaveToTemp(); // Instead of mail.Send();