'Add attachment by url to Outlook mail

The context

There is a button on the homepage of each document set in a document library on a SharePoint Online environment. When the button is clicked, an Outlook window opens with the title and body set and all the files in the document set should be added as the attachments.

The code

Here's the code I have so far:

var olApp = new ActiveXObject("Outlook.Application");
var olNs = olApp.GetNameSpace("MAPI");
var olItem = olApp.CreateItem(0);

var signature = olItem.HTMLBody;
signature.Importance = 2;

olItem.To = "";
olItem.Cc = "";
olItem.Bcc = "";
olItem.Subject = "Pre filled title";
olItem.HTMLBody = 
        "<span style='font-size:11pt;'>" +
        "<p>Pre filled body</p>" +
        "</span>";
olItem.HTMLBody += signature;

olItem.Display();
olItem.GetInspector.WindowState = 2;

var docUrl = "https://path_to_site/Dossiers/13245_kort titel/New Microsoft Word Document.docx";
olItem.Attachments.Add(docUrl);

The Problem

When I run this code, an Outlook window opens with everything set correctly. But on the line where the attachment is added I get following very vague error message:

SCRIPT8: The operation failed.

I thought it could be the spaces in the url so I replaced them:

docUrl = docUrl.replace(/ /g, "%20");

Also didn't work (same error) and providing all parameters like this also didn't work:

olItem.Attachments.Add(docUrl, 1, 1, "NewDocument");

Passing a path to a local file (e.g. C:/folder/file.txt) or a publicly available url to an image does work. So my guess is it has something to do with permissions or security. Does anybody know how to solve this?

PS: I know using an ActiveX control is not the ideal way of working (browser limitations, security considerations, ...) but the situation is what it is and not in my power to change.



Solution 1:[1]

You cannot pass a url to MailItem.Attachments.Add in OOM (it does work in Redemption - I am its author - for RDOMail.Attachments.Add). Outlook Object Model only allows a fully qualified path to a local file or a pointer to another item (such as MailItem).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1