javascript 使用来自 IE 网站的 html 正文在 Outlook 中打开新电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20509994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open new email in outlook with html body from IE web site
提问by David Ewen
I am working in a corporate environment on an internal use web application and have a requirement to generate an email in the users Outlook retaining their signature so that they can then modify if required and send it themselves.
我在公司环境中使用内部使用的 Web 应用程序工作,并且需要在用户 Outlook 中生成一封电子邮件,保留他们的签名,以便他们可以根据需要进行修改并自行发送。
All users are on IE8+ and the site is a part of Trusted Sites with ActiveX objects enabled so I was hoping to use outlook automation to achieve this.
所有用户都在 IE8+ 上,并且该站点是启用 ActiveX 对象的受信任站点的一部分,所以我希望使用 Outlook 自动化来实现这一点。
Here is a quick summary of my requirements to differentiate this from existing questions.
这是我的要求的快速摘要,以将其与现有问题区分开来。
- Only needs to support IE8+ and Outlook
- HTML body formatting support
- Attachment support
- Must retain the users configured signature
- 只需要支持IE8+和Outlook
- HTML 正文格式支持
- 附件支持
- 必须保留用户配置的签名
回答by David Ewen
This can be achieved using JavaScript in IE if the site is a Trusted Site and ActiveX objects are enabled. I have had this script work as far back as IE6 and tested up to IE10 I am unsure about its support in IE11.
如果站点是受信任站点并且启用了 ActiveX 对象,则可以在 IE 中使用 JavaScript 来实现这一点。我早在 IE6 就已经使用过这个脚本,并且测试到 IE10 我不确定它在 IE11 中的支持。
An important point about the script below is that you must call Display
on the email before trying to extract the signature from it or trying to set its HTMLBody
otherwise you will lose the signature information.
关于下面脚本的一个重点是,您必须Display
在尝试从中提取签名或尝试设置其之前调用电子邮件,HTMLBody
否则您将丢失签名信息。
try {
//get outlook and create new email
var outlook = new ActiveXObject('Outlook.Application');
var email = outlook.CreateItem(0);
//add some recipients
email.Recipients.Add('[email protected]').Type = 1; //1=To
email.Recipients.Add('[email protected]').Type = 2; //2=CC
//subject and attachments
email.Subject = 'A Subject';
//email.Attachments.Add('URL_TO_FILE', 1); //1=Add by value so outlook downloads the file from the url
// display the email (this will make the signature load so it can be extracted)
email.Display();
//use a regular expression to extract the html before and after the signature
var signatureExtractionExpression = new RegExp('/[^~]*(<BODY[^>]*>)([^~]*</BODY>)[^~]*/', 'i');
signatureExtractionExpression.exec(email.HTMLBody);
var beforeSignature = RegExp.;
var signature = RegExp.;
//set the html body of the email
email.HTMLBody = beforeSignature + '<h1>Our Custom Body</h1>' + signature;
} catch(ex) {
//something went wrong
}