C# 将Selenium图像读入字节数组,并作为内联图像附件发送到电子邮件

C# 将Selenium图像读入字节数组,并作为内联图像附件发送到电子邮件,c#,selenium-webdriver,C#,Selenium Webdriver,我想阅读一个网页截图,然后用C#将其发送到电子邮件,而不将其另存为文件。现在,我的电子邮件正文只显示以下消息:“这是WebDriver服务器的初始起始页”,没有图像。显示图像的解决方案是什么?谢谢 主要内容: 发送电子邮件方法 //write byte[] to email static void SendEmail(byte[] stream, string pageName, DateTime screenshotTime) { byt

我想阅读一个网页截图,然后用C#将其发送到电子邮件,而不将其另存为文件。现在,我的电子邮件正文只显示以下消息:“这是WebDriver服务器的初始起始页”,没有图像。显示图像的解决方案是什么?谢谢

主要内容:

发送电子邮件方法

//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;
            }
        }
//将字节[]写入电子邮件
静态void sendmail(字节[]流,字符串pageName,日期时间screenshotTime)
{
字节[]图像=流;
附件att=新附件(新内存流(流),页面名称);
att.ContentDisposition.Inline=true;
att.ContentId=Guid.NewGuid().ToString();
att.ContentType.MediaType=“image/png”;
//寄信
SmtpClient=新的SmtpClient(myMailServer);
MailMessage MailMessage=新建MailMessage();
mailMessage.IsBodyHtml=true;
mailMessage.From=新邮件地址(From);
mailMessage.To.Add(新邮件地址(To));
//发送消息
mailMessage.Subject=“网站截图”;
mailMessage.Body+=“网站名称:”+pageName+Environment.NewLine+Environment.NewLine;
mailMessage.Body+=“截屏时间:”+screenshotTime+Environment.NewLine+Environment.NewLine;
mailMessage.Body=String.Format(“屏幕截图”+@”,att.ContentId);
mailMessage.Attachments.Add(附件);
尝试
{
client.Send(mailMessage);
}
捕获(例外情况除外)
{
掷骰子;
}
}

我不知道,但我确实知道
throw ex
是个坏主意-“throw ex重置堆栈跟踪(因此您的错误似乎源自HandleException)”-来自。如果您不打算在catch中执行任何操作,那么它没有任何意义(我想调试时除外),但是如果您保留它,请将其更改为仅抛出
谢谢。我删除了catch-ex,但它仍然不起作用。我的代码有效。问题是我需要在服务器上的IE浏览器中打开许多安全设置。:)我不知道,但我知道,
throw-ex
是一个坏主意——“throw-ex重置堆栈跟踪(因此您的错误似乎源于HandleException)”。如果您不打算在catch中执行任何操作,那么它没有任何意义(我想调试时除外),但是如果您保留它,请将其更改为仅抛出
谢谢。我删除了catch-ex,但它仍然不起作用。我的代码有效。问题是我需要在服务器上的IE浏览器中打开许多安全设置。:)
//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;
            }
        }