1

I would like to read a webpage screenshot and send it to email in C# without save it as File. Right now my email message body only shows this message: "This is the initial start page for the WebDriver server", no image. What is the solution to display the image? Thanks.

Main:

 static void Main(string[] args)
    {

        //FOR IE
        InternetExplorerOptions options = new InternetExplorerOptions();
        options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
        IWebDriver driver = new InternetExplorerDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options);
        // end of IE driver
             driver.Navigate().GoToUrl("https://www.google.com");
            var screenshot = (driver as ITakesScreenshot).GetScreenshot();
           //send email
            SendEmail(screenshot.AsByteArray, "google", DateTime.Now);   
        driver.Close();
        driver.Quit();
    }

sendemail method

//write byte[] to email
        static void SendEmail(byte[] stream, string pageName, DateTime screenshotTime)
        {
            byte[] image = stream;

            Attachment att = new Attachment(new MemoryStream(stream), pageName);
            att.ContentDisposition.Inline = true;

            att.ContentId = Guid.NewGuid().ToString();
            att.ContentType.MediaType = "image/png";

            //send mail
            SmtpClient client = new SmtpClient(myMailServer);

            MailMessage mailMessage = new MailMessage();


            mailMessage.IsBodyHtml = true;

            mailMessage.From = new MailAddress(from);

            mailMessage.To.Add(new MailAddress(to));

            //send message

            mailMessage.Subject = "Website Screenshot";
            mailMessage.Body += "Website Name: " + pageName + Environment.NewLine + Environment.NewLine;
            mailMessage.Body += "Screenshot Time: " + screenshotTime + Environment.NewLine + Environment.NewLine;
            mailMessage.Body = String.Format( "<h3>Screenshot</h3>" + @"<img src=""cid:{0}"" />", att.ContentId);
            mailMessage.Attachments.Add(att);

            try
            {
                client.Send(mailMessage);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
user788448
  • 779
  • 1
  • 14
  • 36
  • I don't know, but I do know that `throw ex` is a bad idea - _"throw ex resets the stack trace (so your errors would appear to originate from HandleException)"_ - from [this question](https://stackoverflow.com/questions/730250/is-there-a-difference-between-throw-and-throw-ex). If you're not going to do anything in the catch, there's no point to it (except I suppose in debugging) but if you do keep it change that to just `throw` – stuartd Feb 29 '20 at 20:14
  • Thanks. I removed the catch ex and it still does not work. – user788448 Feb 29 '20 at 23:14
  • 1
    My code works. The issue was that I need to turn on a lot of security settings in IE browser on server. :) – user788448 Mar 01 '20 at 00:32

0 Answers0