2

我有一个 asp.net MVC 应用程序,下面的代码工作文件。

但是代码是,当在浏览器中导航到电子邮件操作时,会下载一个 EML 文件,然后当我们单击该文件时,该文件将使用 Outlook 打开。

有没有可能,当action调用时,EML文件会直接用outlook打开,而不是下载然后点击打开?

代码

public async Task<FileStreamResult> Email()
{
string dummyEmail = "test@localhost.com";

var mailMessage = new MailMessage();

mailMessage.From = new MailAddress(dummyEmail);
mailMessage.To.Add("dejan.caric@gmail.com");
mailMessage.Subject = "Test subject";
mailMessage.Body = "Test body";

// mark as draft
mailMessage.Headers.Add("X-Unsent", "1");

// download image and save it as attachment
using (var httpClient = new HttpClient())
{
    var imageStream = await httpClient.GetStreamAsync(new Uri("http://dcaric.com/favicon.ico"));
    mailMessage.Attachments.Add(new Attachment(imageStream, "favicon.ico"));
}

var stream = new MemoryStream();
ToEmlStream(mailMessage, stream, dummyEmail);

stream.Position = 0;

return File(stream, "message/rfc822", "test_email.eml");

}

private void ToEmlStream(MailMessage msg, Stream str, string dummyEmail)
{
using (var client = new SmtpClient())
{
    var id = Guid.NewGuid();

    var tempFolder = Path.Combine(Path.GetTempPath(), Assembly.GetExecutingAssembly().GetName().Name);

    tempFolder = Path.Combine(tempFolder, "MailMessageToEMLTemp");

    // create a temp folder to hold just this .eml file so that we can find it easily.
    tempFolder = Path.Combine(tempFolder, id.ToString());

    if (!Directory.Exists(tempFolder))
    {
        Directory.CreateDirectory(tempFolder);
    }

    client.UseDefaultCredentials = true;
    client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
    client.PickupDirectoryLocation = tempFolder;
    client.Send(msg);

    // tempFolder should contain 1 eml file
    var filePath = Directory.GetFiles(tempFolder).Single();

    // create new file and remove all lines that start with 'X-Sender:' or 'From:'
    string newFile = Path.Combine(tempFolder, "modified.eml");
    using (var sr = new StreamReader(filePath))
    {
        using (var sw = new StreamWriter(newFile))
        {
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                if (!line.StartsWith("X-Sender:") &&
                    !line.StartsWith("From:") &&
                    // dummy email which is used if receiver address is empty
                    !line.StartsWith("X-Receiver: " + dummyEmail) &&
                    // dummy email which is used if receiver address is empty
                    !line.StartsWith("To: " + dummyEmail))
                {
                    sw.WriteLine(line);
                }
            }
        }
    }

    // stream out the contents
    using (var fs = new FileStream(newFile, FileMode.Open))
    {
        fs.CopyTo(str);
    }
}
}
4

2 回答 2

0

使用 Chrome,您可以让它在下载某些文件后自动打开它们。

.EML 应尝试在 Outlook 中打开。

我不确定其他浏览器,但 Chrome 似乎是唯一具有此选项的浏览器。

这不是一个完美的解决方案,因为如果有人从 Chrome 中的另一个网站下载了 .EML,它也会自动打开。

我建议让 Chrome 专用于您的 Web 应用程序。

于 2015-07-05T12:25:01.683 回答
0

您肯定可以使用 Outlook 打开本地 .eml 文件。

但是在 Web 应用程序的上下文中,您必须首先下载它。

于 2016-02-08T10:08:19.720 回答