![](/img/trans.png)
[英]Embedded images (inline) within html body in email showing as attachment
[英]How do I email an HTML report (as an attachment) with images embedded?
我正在创建一个带有 ExtentReports 的报告,以通过电子邮件发送给域外的团队成员。 我使用截图方法(如下)来保存测试失败的截图。 它们存储在 ExtentReports HTML 报告的子文件夹中。
我将报告附加到电子邮件中,在其中,具有文件夹权限的域中的团队成员可以很好地显示图像。 但是我不知道如何允许该文件夹权限之外的人查看嵌入在报告中的图像。 这是图像的 HTML,直接引用该文件。
<img class="report-img" data-featherlight="file:///\\domain.local\files\QA\Projects\AutomationReports\ExtentScreens\1486487870116.jpg" src="file:///\\domain.local\files\QA\Projects\AutomationReports\ExtentScreens\1486487870116.jpg">
这是我的截图方法。
public static String CaptureScreen(WebDriver driver) {
String ImagesPath = "\\\\domain.local\\files\\QA\\Projects\\AutomationReports\\ExtentScreens\\"
+ new Date().getTime();
TakesScreenshot oScn = (TakesScreenshot) driver;
File oScnShot = oScn.getScreenshotAs(OutputType.FILE);
File oDest = new File(ImagesPath + ".jpg");
// System.out.println(ImagesPath);
try {
FileUtils.copyFile(oScnShot, oDest);
} catch (IOException e) {
System.out.println(e.getMessage());
}
return ImagesPath + ".jpg";
}
关于如何解决这个问题,我有 2 个不相关的想法。 但是我需要一些帮助来开始使用它们中的任何一个。 我愿意接受其他建议。
将图像直接嵌入 HTML 报告或以某种方式发送包含 HTML 报告截图的文件夹。 但是,HTML 仍会引用我的原始位置,并且图像会损坏。
与所有人、来宾和匿名用户共享包含图像的文件夹,以便域外的人可以打开引用此位置的 HTML。 我不知道如何设置这些权限,我什至不确定这样做是否会允许外部用户查看引用该位置的 HTML。
请尝试使用 base64 编码它肯定会起作用。 另外,请检查您的浏览器支持。
尝试这个:
<img src="data:image/jpeg;base64,/9j/4AAQxxxxxxxx...." />
根据您需要支持的浏览器,您可以将图像嵌入 base64。 像这样 :
<img src="data:image/jpeg;base64, LzlqLzRBQ...<!-- base64 data -->" />
您可以将 base64 编码的图像直接嵌入到 HTML 文档中。
<img src="data:image/jpeg;base64,/9j/4AAQxxxxxxxx...." />
您可以创建多部分/报告消息,其中包含作为消息中附加正文部分的图像。 JavaMail 常见问题解答包含以下示例代码:
Multipart multipart = new MimeMultipart("related");
MimeBodyPart htmlPart = new MimeBodyPart();
// messageBody contains html that references image
// using something like <img src="cid:XXX"> where
// "XXX" is an identifier that you make up to refer
// to the image
htmlPart.setText(messageBody, "utf-8", "html");
multipart.addBodyPart(htmlPart);
MimeBodyPart imgPart = new MimeBodyPart();
// imageFile is the file containing the image
imgPart.attachFile(imageFile);
// or, if the image is in a byte array in memory, use
// imgPart.setDataHandler(new DataHandler(
// new ByteArrayDataSource(bytes, "image/whatever")));
// "XXX" below matches "XXX" above in html code
imgPart.setContentID("<XXX>");
multipart.addBodyPart(imgPart);
message.setContent(multipart);
我们应该有一个类文件如下
公共类 GetScreenShort {
public static String capture(WebDriver driver,String screenShotName) 抛出 IOException { TakesScreenshot ts = (TakesScreenshot)driver;
String dest = ts.getScreenshotAs(OutputType.BASE64); return "data:image/jpg;base64, " + dest ; }
}
必须调用相同的类,如下所示
String screenShotPath = GetScreenShort.capture(webdriver, "screenShotName");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.