title: Create an email with an attachment on Server for user to download
body: <p>I have a program on the server that creates pdf sql reports and I need to have them so users can email them out. First off before everything gets automated I want them to be able to view the email and adjust it as needed.</p>
  
  <p>Im been using office.interop.outlook but now that isn’t working on the server as people have told me earlier. To view the documents I put all the selected PDF reports in a .zip file and the user downloads the zip. Now I was thinking of doing the same thing for emails. </p>
  
  <p>Creating a .zip file that contains emails with the pdf reports as the attachment. 
  How could I create am email draft with an attachment the user could download and open on their own machine? I can successfully create a text file and download it. When I try to use Office.Interop.Outlook I cannot create the ‘MailItem’ and convert it to a byte array to put it in return File(). What would you suggest? Locally everything works perfectly.</p>
  
  <p>I also read online about creating an .eml file and people cam open this as if it were and email. Everyone has Outlook in the company. Outlook has .msg files for its email.</p>
  
  <p>Heres some stuff ive tried</p>
  
  <pre><code> public ActionResult Testtxt()
      {
          var data = "Here is a string with information in it";
          var byteArray = System.Text.Encoding.ASCII.GetBytes(data);
          var memorystream = new MemoryStream(byteArray);
          return File(memorystream, "text/plain", "txtname.txt");
      }
      public void OutlookTest() // Errors when creating the email (permission rights)
      {
          ServiceResponse servRespObj = new ServiceResponse();
          //string downloadsPath = new KnownFolder(KnownFolderType.Downloads).Path;
          /// Test Mail on Server     /* mail draft = Untitled.msg */
          /// "application/vsd.ms-outlook" as MIME type.
          /// ".msg" as extension
          /// 1. FilePath, 2. ContentType(MIME), 3.FileName   return File(1,2,3); top=FileResult
          OutlookApp outlookapp = new OutlookApp();
          MailItem mailItem = outlookapp.CreateItem(OlItemType.olMailItem);
          mailItem.Subject = "This is a TEST msg - Subject02";
          mailItem.Body = "msg body02";
          //byte[] fileByteArray = System.IO.File.ReadAllBytes(mailItem);
          var byteArray = System.Text.Encoding.ASCII.GetBytes(mailItem);
          //return File(mailItem, "application/vsd.ms-outlook", "testMessage.msg");
          //mailItem.SaveAs(downloadsPath, OlSaveAsType.olMSG);
          //mailItem.Save();
      }
      public ActionResult EmailDraft2()
      {
          byte[] fileByteArray = null;
          string timeNow = DateTime.Now.ToString("MM-dd-yyyy-HHmmss");
          OutlookApp application = new OutlookApp();
          MailItem mailItem = application.CreateItem(OlItemType.olMailItem);
          mailItem.Subject = "test subject draft";
          mailItem.Body = "test body draft";
  
          using(MemoryStream stream = new MemoryStream())
          {
              /// Creates the .zip file.
              using(ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
              {
                  /// Iterates through the files
                  //foreach
                  /// Creates an archive for the .zip                     // &amp; extension
                  ZipArchiveEntry archiveEntry = archive.CreateEntry("filename" + ".msg");
                  /// Adds the archive to the .zip
                  //using(MemoryStream fileStream = new MemoryStream(mailItem.))
              }
          }
  
          return null;
      }
      public ActionResult CreateEMLemail()
      {
          string senderEmail = "Test@test.test.test";
  
          var mailMessage = new MailMessage();
          mailMessage.From = new MailAddress(senderEmail);
          mailMessage.To.Add("To_EmailSend@msn.com");
          mailMessage.Subject = "EML TEST MAIL SUBJECT";
          mailMessage.Body = "EML TEST MAIL BODY";
  
          var stream = new MemoryStream();
          ToEMLstream(mailMessage, stream, senderEmail);
  
          stream.Position = 0;
          return File(stream, "message/rfc822", "emlTESTmessage.eml");
      }
      private void ToEMLstream(MailMessage msg, Stream stream, string fromSender)
      {
          using(var client = new SmtpClient())
          {
              var id = 0;
              //var tempFolder = Path.Combine(Path.GetTempPath(), Assembly.GetExecutingAssembly().GetName().Name);
              //tempFolder = Path.Combine(tempFolder, "EMLtempFolder");
              var downloads = new KnownFolder(KnownFolderType.Downloads).Path;
              string timeNow = DateTime.Now.ToString("MM-dd-yyyy-HHmmss");
              var newFolder = Path.Combine(downloads, timeNow);
              //if (!Directory.Exists(tempFolder))
              if (!Directory.Exists(newFolder))
              {
                  //Directory.CreateDirectory(tempFolder);
                  Directory.CreateDirectory(newFolder);
              }
              //client.UseDefaultCredentials = true;
              //client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
              //client.PickupDirectoryLocation = tempFolder;
              //client.Send(msg);
  
              //string newFile = Path.Combine(tempFolder, "emlTestmail.eml");
              string newFile = Path.Combine(newFolder, "emlTestmail.eml");
              //using (var streamr = new StreamReader(tempFolder))
              using (var streamr = new StreamReader(newFolder))
              {
                  using (var streamw = new StreamWriter(newFile))
                  {
                      string line;
                      while((line = streamr.ReadLine()) != null)
                      {
                          if(!line.StartsWith("X-Sender:") &amp;&amp; !line.StartsWith("From:") &amp;&amp;
                              !line.StartsWith("X-Receiver: " + fromSender) &amp;&amp;
                              !line.StartsWith("To: " + fromSender))
                          {
                              streamw.WriteLine(line);
                          }
                      }
                  }
              }
              {
                  using(var filesteam = new FileStream(newFile, FileMode.OpenOrCreate))
                  {
                      filesteam.CopyTo(stream);
                  }
              }
          }
      }
  </code></pre>