我想写一个服务,通过它我可以创建一个指定的(收件人,抄送,密件抄送,主题,正文)的电子邮件。然后,我需要将此电子邮件返回到前端并以“.oft”格式下载,这样当我单击下载的文件时,文件应打开,并填充所有字段(收件人、抄送、密件抄送、主题、正文)。
我使用Java作为后端技术,angular5作为前端。
我试过用javax.mail实用程序来创建电子邮件并将其作为字节数组返回。比如:
Properties prop = System.getProperties();
Session session = Session.getDefaultInstance(prop, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("emailAddr@domain.com"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("emailAddr@domain.com"));
msg.setSentDate(new Date());
msg.setSubject("subject");
msg.setText("text of msg");
//return it from service API as
response.getOutputStream().write(msg.toString().getBytes());
//function gets called on button click
createEmailTemplate():void{
this.httpService.getEmail('serviceUrl')
.subscribe(
email => {
let filename = "SampleMailFile.oft";
let linkElement = document.createElement('a');
let blob = new Blob([email], { type: "message/rfc822"});
let url = window.URL.createObjectURL(blob);
linkElement.setAttribute('href', url);
linkElement.setAttribute("download", filename);
let clickEvent = new MouseEvent("click", {
"view": window,
"bubbles": true,
"cancelable": false
});
linkElement.dispatchEvent(clickEvent);
}
);
}