2015-04-30 42 views
1

我有一个asp.net MVC应用程序,下面的代码作品文件。eml可以直接用outlook打开,相反eml文件会下载然后点击它在outlook中打开?

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

可以,当动作调用的时候,EML文件会直接用outlook打开,而不是下载然后点击打开?

代码

public async Task<FileStreamResult> Email() 
{ 
string dummyEmail = "[email protected]"; 

var mailMessage = new MailMessage(); 

mailMessage.From = new MailAddress(dummyEmail); 
mailMessage.To.Add("[email protected]"); 
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); 
    } 
} 
} 

回答

0

有了Chrome,你可以让它自动打开某些文件,一旦被下载。

.EML应该尝试在Outlook中打开。

我不确定其他浏览器,但Chrome似乎是唯一一个使用此选项的人。

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

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

0

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

但是,在Web应用程序的情况下,您必须首先下载它。

相关问题