我试图将Selenium报告发送到我的电子邮件地址。
在我的pom中,我添加了spring-context-support和javax.mail-api。
问题是我不知道如何将HTML页面读入InputStreamSource,因此我可以将其发送到我的电子邮件地址。有人可以给我看看吗?这是我发送电子邮件的方法:
private void sendMail() {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
if (emailTo.contains(",")) {
helper.setTo(emailTo.split(","));
} else {
helper.setTo(emailTo);
}
helper.setFrom("from@domain.com");
helper.setSubject("Selenium: Something went wrong");
helper.setText("Please see the attached report");
// I need help with this:
InputStreamSource iss = getInputSteam("Testresult.html");
helper.addAttachment("Testresult.html", iss,
"text/html; charset=UTF-8");
mailSender.send(message);
} catch (javax.mail.MessagingException me) {
throw new RuntimeException("Messaging exception caught: " + me.getMessage());
}
}
答案 0 :(得分:0)
如果可以在本地访问HTML文件(确保文件的文件路径正确),这应该有效:
InputStreamSource iss = FileSystemResource("path/to/your/file");
或者,如果您可以使用URL(在这种情况下通过HTTP):
InputStreamSource iss = FileSystemResource(new URL("http://my.file.url"));
请注意MimeMessageHelper
有这样的方法:
addAttachment(String attachmentFilename, File file)
如果您不必关心contentType
,那将会更容易。