0
votes

with ActiveX I can create an Outlook instance and start a new HTML e-Mail.

Here is the sample code:

var outlookApp = new ActiveXObject("Outlook.Application");

var nameSpace = outlookApp.getNameSpace("MAPI");

mailFolder = nameSpace.getDefaultFolder(6);

mailItem = mailFolder.Items.add('IPM.Note.FormA');

mailItem.Subject="a subject test";

mailItem.To = "[email protected]";

mailItem.HTMLBody = "<b>bold</b>";

mailItem.display (0);

Is there an equivalent for Firefox. For example with XPCom? Has anyone a sample please?

Thank you!

2

2 Answers

1
votes
//this class emulates the one u used to use
function mailer() {
  this.display = function() {
    var url = 'mailto:'
                       + this.To
                       + '?subject=' + encodeURIComponent(this.Subject)
                       + '&body=' + encodeURIComponent(this.HTMLBody);
    window.location = url;
  }
}

//we instantiate
mailItem = new mailer();


//and then your old code: 
mailItem.Subject="a subject test";
mailItem.To = "[email protected]";
mailItem.HTMLBody = "<b>bold</b>";
mailItem.display (0);

What is being done here is using <a>'s mailto: similar approach:

<a href="mailto:[email protected]?subject=a+subject+test&body=%3Cb%3Ebold%3C/b%3E">email me!</a>
0
votes

Here is an alternative solution very similar to the accepted answer if you're using jQuery and would like to extend its functionality to accomplish this. Just as good as the other answer but thought I'd throw out an alternative solution that I use. So far I haven't had any issues with cross browser usage. I didn't go back and try older versions of IE though, just used the more current version of each browser to test this.

Extending jQuery to include your custom mailer function

$(function () {
    $.fn.myMailer = function () {
        this.display = function() {
            var url = 'mailto:' + this.To + 
                      '?subject=' + encodeURIComponent(this.Subject) + 
                      '&body=' + encodeURIComponent(this.HTMLBody);
            window.location = url;
            return true;
        }
    }
});

Example Usage:

var myMailItem = new $.fn.myMailer();
myMailItem.To = '[email protected]';
myMailItem.Subject = 'Your Subject';
myMailItem.HTMLBody = 'Whatever you want your E-Mail to say';
myMailItem.display(0);

Furthermore if you'd like to add CC or BCC recipients then follow the structure above to add them to the mailto query string as well.

I also tried to get an attachment in an Outlook E-Mail on the clients computer

$(function () {
    $.fn.myMailer = function () {
        this.display = function() {
            var url = 'mailto:' + this.To + '?content-type=' +
                       encodeURIComponent('multipart/form-data') + 
                      '&subject=' + encodeURIComponent(this.Subject) + 
                      '&body=' + encodeURIComponent(this.HTMLBody) +  
                      '&attachment=' + encodeURIComponent(this.Attachment);
            window.location = url;
            return true;
        }
    }
});

I failed with this attempt. I'm not sure this is possible solely from the use of client side code. I could be wrong. This could also pose a security threat I'd think if it is possible.

I'd say the best bet here would be to use client scripting to invoke some server side code however you prefer to go about that and have the server send out the E-Mail if applicable.

I however needed the attachment added to an Outlook E-Mail which would pop-up on the clients computer so I'm still working out the final details of how to handle adding attachments for my situation. Using .NET I think there should be some way to handle this I just haven't had time to implement the server side handler to accomplish it yet. If someone else is trying to accomplish the same sort of thing using .NET here is a good link to get started with Outlook 2013 MailItem properties.