【问题标题】:Is there a way to send a mail seamlessly using Office.js?有没有办法使用 Office.js 无缝发送邮件?
【发布时间】:2019-07-25 00:53:41
【问题描述】:

我使用 office.js 构建了我的第一个 Outlook.web.addin,

但我需要一种方法来向某个收件人发送预定义的邮件,而不向用户显示“邮件撰写”屏幕...

下面的代码会打开撰写屏幕,但如果不强制用户按下发送按钮,我就无法发送。

function sendMessage() {

    if (Office.context.mailbox.item.itemType === Office.MailboxEnums.ItemType.Message) {
        var mailbox = Office.context.mailbox;
        var item = mailbox.item;
        var itemId = item.itemId;
        if (itemId === null || itemId == undefined) {
            item.saveAsync(function(result) {
                itemId = result.value;
            });
        }

        Office.context.mailbox.displayNewMessageForm(
            {
                // Copy the To line from current item.
                toRecipients: ['xxx@xxx.net'],
                ccRecipients: ['yyy@yyyy.com'],
                subject: 'Outlook add-ins are cool!',
                htmlBody: 'Hello <b>World</b>!<br/><img src="cid:image.png"></i>',
                attachments: [
                    {
                        type: 'item',
                        name: 'Suspected phishing mail',
                        itemId: itemId
                    }
                ]
            });

    } else {
        return;
    }
}

我需要将上面的代码修改为:

function sendMessage() {

    if (Office.context.mailbox.item.itemType === Office.MailboxEnums.ItemType.Message) {
        var mailbox = Office.context.mailbox;
        var item = mailbox.item;
        var itemId = item.itemId;
        if (itemId === null || itemId == undefined) {
            item.saveAsync(function(result) {
                itemId = result.value;
            });
        }

        var newItem = mailbox.item;
        newItem.to.setAsync(["xxx@xxx.net"]);
        newItem.body.setAsync(["This is a test message"]);
        newItem.addItemAttachmentAsync(
            itemId,
            "Welcome email"
            );
        newItem.saveAsync(
            function callback(result) {
                alert(result);
            });
    } else {
        return;
    }
}

我希望在发送消息时不允许用户更改消息中的任何细节。

【问题讨论】:

    标签: office365 office-js office-addins outlook-web-addins


    【解决方案1】:

    您可以通过使用MakeEWSREquestAsync 发出CreateItem EWS 请求来完成类似的操作。下面的示例将向您自己发送一封电子邮件,但您可以根据需要进行修改。

    var request = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
    	'  <soap:Header><t:RequestServerVersion Version="Exchange2010" /></soap:Header>'+
    	'  <soap:Body>'+
    	'    <m:CreateItem MessageDisposition="SendAndSaveCopy">'+
    	'      <m:SavedItemFolderId><t:DistinguishedFolderId Id="sentitems" /></m:SavedItemFolderId>'+
    	'      <m:Items>'+
    	'        <t:Message>'+
    	'          <t:Subject>Hello, Outlook!</t:Subject>'+
    	'          <t:Body BodyType="HTML">Hello World!</t:Body>'+
    	'          <t:ToRecipients>'+
    	'            <t:Mailbox><t:EmailAddress>' + Office.context.mailbox.userProfile.emailAddress + '</t:EmailAddress></t:Mailbox>'+
    	'          </t:ToRecipients>'+
    	'        </t:Message>'+
    	'      </m:Items>'+
    	'    </m:CreateItem>'+
    	'  </soap:Body>'+
    	'</soap:Envelope>';
    
    Office.context.mailbox.makeEwsRequestAsync(request, function (asyncResult) {
      if (asyncResult.status == "failed") {
        showMessage("Action failed with error: " + asyncResult.error.message);
      }
      else {
        showMessage("Message sent!");
      }
    });

    【讨论】:

    • 感谢它神奇地工作,但我怎样才能将当前的电子邮件作为附件发送?
    • 我不确定,您必须查看 EWS 的详细信息才能了解可能的情况。您确实有来自 Office.JS 的当前项目 itemId。
    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 2021-06-12
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    • 2021-11-28
    • 1970-01-01
    相关资源
    最近更新 更多