locked
Help using Email Helper Class RRS feed

  • Question

  • User-718146471 posted

    Hey, I am trying to get this code to work but I do not know what it is expecting me to pass to it.  My machine was rebuilt but the most recent back up I have is a few weeks ago.  Here is my code:

    protected void btnSubmit_Click(object sender, EventArgs e)
            {
                string htmlBody = "Hello, we found the following issues." + Label1.Text.ToString() + "<br><br>Please correct these issues and resubmit.  Thank you!";
                
                MailMessage mailMessage = new MailMessage();
                mailMessage.From = new MailAddress("*********************", "*********************");
                mailMessage.To.Add("me@me.com");
                mailMessage.CC.Add(new MailAddress("*********************", "*********************"));
                mailMessage.Subject = "Application Evaluation: " + txtAppName.Text.ToString();
                mailMessage.Body = htmlBody.ToString();
                mailMessage.IsBodyHtml = true;
                byte[] byteArray = Encoding.UTF8.GetBytes(htmlBody);
                MemoryStream htmlStream = new MemoryStream(byteArray);
                emailHelper.ToEMLStream(mailMessage, htmlStream); 
    
        public static class emailHelper
        {
            public static void ToEMLStream(this MailMessage msg, Stream str)
            {
                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();
    
                    // stream out the contents
                    using (var fs = new FileStream(filePath, FileMode.Open))
                    {
                        fs.CopyTo(str);
                    }
                }
            }    
        }
    }

    and the associated article that shows this helper method: http://www.webr2.com/how-to-save-mailmessage-object-to-disk-as-eml-or-msg-file/

    Monday, November 2, 2015 10:38 AM

Answers

  • User-821857111 posted

    The ToEMLStream method is an extension method. It only expects you to pass in the second parameter - the stream. The first parameter is prefixed with the 'this'  keyword (which is what denotes it as an extension method). It specifies the type that the method is an extension of (the MailMessage type). Therefore you should call the method like this:

    emailHelper.ToEMLStream(htmlStream);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 4, 2015 2:38 AM
  • User-821857111 posted

    Have you tried debugging to ensure that everything is where and what you expect it to be?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 4, 2015 2:59 PM

All replies

  • User-821857111 posted

    The ToEMLStream method is an extension method. It only expects you to pass in the second parameter - the stream. The first parameter is prefixed with the 'this'  keyword (which is what denotes it as an extension method). It specifies the type that the method is an extension of (the MailMessage type). Therefore you should call the method like this:

    emailHelper.ToEMLStream(htmlStream);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 4, 2015 2:38 AM
  • User-718146471 posted

    Mike, I am close, it is sending an EML file to the client but instead of the one saved to the path, it streams an empty .eml file.  Here is the code:

                    var filePath = Directory.GetFiles(tempFolder).Single();
    
                    // stream out the contents
                    using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                    {
                        try
                        {
                            // TODO: stream as mime type to client - should open in Outlook directly
                            HttpContext.Current.Response.ContentType = "application/octet-stream";
                            HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", filePath));
                            HttpContext.Current.Response.AddHeader("Content-Length", filePath.Length.ToString());
                            HttpContext.Current.Response.WriteFile(filePath.ToString());
                            HttpContext.Current.Response.End();
                        }
                        catch (Exception ex)
                        {
                            HttpContext.Current.Response.Write(ex.ToString());
                        }
                    }
    

    Wednesday, November 4, 2015 1:58 PM
  • User-821857111 posted

    Have you tried debugging to ensure that everything is where and what you expect it to be?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 4, 2015 2:59 PM
  • User-718146471 posted

    I got it working Mike, I'm all set, points are coming your way now :)

    Wednesday, November 4, 2015 3:48 PM