使用来自 IE 网站的 html 正文在 Outlook 中打开新电子邮件 [英] Open new email in outlook with html body from IE web site

查看:23
本文介绍了使用来自 IE 网站的 html 正文在 Outlook 中打开新电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在公司环境中使用内部使用的 Web 应用程序工作,并且需要在用户 Outlook 中生成一封保留其签名的电子邮件,以便他们可以根据需要进行修改并自行发送.

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.

所有用户都使用 IE8+,并且该站点是启用 ActiveX 对象的受信任站点的一部分,因此我希望使用 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.

这里是我的要求的快速摘要,以将其与现有问题区分开来.

Here is a quick summary of my requirements to differentiate this from existing questions.

  • 只需要支持 IE8+ 和 Outlook
  • HTML 正文格式支持
  • 附件支持
  • 必须保留用户配置的签名

推荐答案

如果站点是受信任站点并且启用了 ActiveX 对象,则可以在 IE 中使用 JavaScript 来实现.早在 IE6 和 IE10 中我就已经使用过这个脚本,我不确定它是否支持 IE11.

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.

关于下面脚本的一个重要点是,在尝试从中提取签名或尝试设置其 HTMLBody 之前,您必须在电子邮件上调用 Display 否则您将丢失签名信息.

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.

try {

    //get outlook and create new email
    var outlook = new ActiveXObject('Outlook.Application');
    var email = outlook.CreateItem(0);

    //add some recipients
    email.Recipients.Add('user1@company.com').Type = 1; //1=To
    email.Recipients.Add('user2@company.com').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.$1;
    var signature = RegExp.$2;

    //set the html body of the email
    email.HTMLBody = beforeSignature + '<h1>Our Custom Body</h1>' + signature;

} catch(ex) {
    //something went wrong
}

这篇关于使用来自 IE 网站的 html 正文在 Outlook 中打开新电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆