有没有办法使用Office.js无缝发送邮件? [英] Is there a way to send a mail seamlessly using Office.js?

查看:39
本文介绍了有没有办法使用Office.js无缝发送邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用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;
    }
}

我希望发送邮件时不允许用户更改邮件中的任何详细信息。

ews

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

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!");
  }
});

这篇关于有没有办法使用Office.js无缝发送邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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