When I call the code below to create an email and add an attachment, everything works fine the first time. However, when I try to do it again, I encounter a RangeError.
I'm looking to enable the user to input the recipient's information and customize the message content before sending it out.
Can anyone shed light on why the error occurs on the second run of the method?
function NewMailItem(p_recipient, p_subject, p_body, p_file, p_attachmentname)
{
try
{
var objO = new ActiveXObject('Outlook.Application');
var objNS = objO.GetNameSpace('MAPI');
var mItm = objO.CreateItem(0);
mItm.Display();
if (p_recipient.length > 0)
{
mItm.To = p_recipient;
}
mItm.Subject = p_subject;
if (p_file.length > 0)
{
var mAts = mItm.Attachments;
mAts.add(p_file, 1, p_body.length + 1, p_attachmentname);
}
mItm.Body = p_body;
mItm.GetInspector.WindowState = 2;
} catch(e)
{
alert('unable to create new mail item');
}
}
The issue seems to be happening at the `mAts.add` line where it attempts to attach the document.
Additionally, note that the file name (`p_file`) is a direct URL to an image file.