I've built my first outlook.web.addin using office.js,
But I need a way to send a predefined mail to a certain recipient without showing the user a 'message compose ' screen...
Below code opens compose screen, but I can't send without forcing the user to push the send button.
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: ['[email protected]'],
ccRecipients: ['[email protected]'],
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;
}
}
I need to modify the above code to be something like:
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(["[email protected]"]);
newItem.body.setAsync(["This is a test message"]);
newItem.addItemAttachmentAsync(
itemId,
"Welcome email"
);
newItem.saveAsync(
function callback(result) {
alert(result);
});
} else {
return;
}
}
I expect to send the message without allowing the user to change any details in the message.
You can accomplish something like this by making a CreateItem EWS request using MakeEWSREquestAsync. The sample below will send an e-mail to yourself, but you can modify as you need.
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!");
}
});