Outlook Mail at client side with attachment from web server


  • Hi

    I have a requirement where I have to create a PDF report on the web server and to create a mail in the draft folder in the outlook mail client on the client side with the PDF on the server attached to it. For this I have used javascript code like this .....


           try {
                objOutlookExpress = new ActiveXObject("Outlook.Application");
            catch (err) {
                alert(err.message +
                        "\n Ensure that the 'scripting of ActiveX controls not marked safe' is enabled for the browser");


            objOutlookMsg = objOutlookExpress.CreateItem(0)
            objOutlookMsg.To = "abc@xyz.com";
            objOutlookMsg.Subject = "subject";
            objOutlookMsg.HTMLBody = "Mail Body";
                objOutlookMsg.Attachments.Add("", 1, 1, "Attachment");

                alert("An email with the quotation is saved in the Outlook draft folder");

            catch (err) {


    I have used the URI of the generated file on the web server as the mail attachment file path. The web application uses windows authentication. On some installations it works OK, but in some other it says that the "file name or directory name is invalid". What could be the possible problem for this and how to get around this problem 

    • Moved by Leo Liu - MSFTModerator Tuesday, January 24, 2012 6:36 AM Moved for better support. (From:Visual C# General)
    Saturday, January 21, 2012 7:45 AM


All replies

  • Have you tried with Internet Explorer of Firefox? Does the error appear only on certain web browsers?

    Marco Minerva [MCPD]
    Blog: http://blogs.ugidotnet.org/marcom
    Twitter: @marcominerva
    Saturday, January 21, 2012 12:40 PM
  • Do double check with  this.

    Sending Email with attachment in ASP.NET using SMTP Server



    Saturday, January 21, 2012 5:09 PM
  • I have tested this with IE 8.0. With one web application deployment it works, and does not work for another deployment (both use IIS 6.0, windows 2003 serer and windows authentication)
    Monday, January 23, 2012 6:07 AM
  • As you use Javascript, the script runs on client, so the problem resides on the client, not on the server. Have you verified that the URL of the PDF that you're trying to attach is reachble, for example if you copy the URL in the browser address bar?

    Marco Minerva [MCPD]
    Blog: http://blogs.ugidotnet.org/marcom
    Twitter: @marcominerva
    Monday, January 23, 2012 8:48 AM
  • Hi Debjit,

    I am moving your thread into the JScript for the .NET Framework Forum for dedicated support.
    Have a nice day, 
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 24, 2012 6:37 AM
  • Yes, the file opens. If I type "http://Server/Folder/File.pdf the PDF opens. However it pops up a domain user id & password window(as the web application and IIS is configured to use windows authentication) . If I enter my domain user id and password the PDF file opens. Could this be the problem ? The Outlook ActieX is trying to access the file on the server and with no way of entering user id and pasword the server rejects the request which causes the Outlook ActiveX to fail to attach the file from the server.
    Tuesday, January 31, 2012 8:44 AM
  • Any ideas anyone on the possible reason(s) for this problem? I would appreciate your help on this.



    Tuesday, February 07, 2012 6:19 AM
  • Hi Debijt...

    I ave the same Problem using javascript tryng to get a document as an email attachment from sharepoint..

    the problem seems to be that  either - outlook has no rights to download eventhough IE starts it and the List has been added to synchronized with outlook...
    OR the síte is not added to trusted sites... something like that..
    have you solved your problem meanwhile ?

    and when... how ? ;))




    Friday, June 22, 2012 10:04 AM
  • Hi Debijt...

    I found the solution.. the error ist that you use "  in

    objOutlookMsg.Attachments.Add("", 1, 1, "Attachment");

    Add .. try ' instead of this..

    var outlookApp = new ActiveXObject("Outlook.Application");
    var nameSpace = outlookApp.getNameSpace("MAPI");
    mailFolder = nameSpace.getDefaultFolder(6);
    theMailItem = mailFolder.Items.add('IPM.NotemA');
    //var theMailItem = outlookApp.CreateItem(0);
    // theMailItem.Subject = "";
    // theMailItem.To = "";
    // theMailItem.HTMLBody = "";

    Friday, June 22, 2012 10:24 AM