可以用Outlook直接打开eml,而不是eml文件会下载然后点击它在outlook中打开吗?

时间:2015-04-30 16:04:50

标签: asp.net-mvc eml

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

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

可行,当动作调用时,那么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);
    }
}
}

2 个答案:

答案 0 :(得分:0)

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

.EML应尝试在Outlook中打开。

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

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

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

答案 1 :(得分:0)

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

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