Microsoft Outlook with attached pdf not working on click RRS feed

  • Question

  • after i pushed my email feature to test, outlook is not working as expected:


     From my code, on click, the html page is converted to the pdf and can be downloaded but outlook is not popping up with the attached pdf.


    But outlook was displaying on click on email with the attached pdf on my local server but it does not display anymore after i pushed to test, even though on test, the pdf can be downloaded


    Kindly check the code if there's anything i am missing and why the outlook is not popping up. I have the smtp settings for outlook in the web.config as below




          <smtp from="[email_address_here]">​

            <network host="" port="587" userName="[username]" password="[password]" enableSsl="true" />​




     string strHtml = string.Empty;
                    string pdfFileName = Request.PhysicalApplicationPath + "\\/App/Docs\\" + "workorder.pdf";
                    StringWriter sw = new StringWriter();
                    HtmlTextWriter hw = new HtmlTextWriter(sw);
                    StringReader sr = new StringReader(sw.ToString());
                    strHtml = sr.ReadToEnd();

                    CreatePDFFromHTMLFile(strHtml, pdfFileName);
                    MemoryStream ms = new MemoryStream();
                    Response.ContentType = "application/x-download";
                    Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", "workorder.pdf"));
                    ms.Seek(0, SeekOrigin.Begin);

                    SmtpClient client = new SmtpClient();

                    client.Port = 587;
                    client.EnableSsl = true;
                    client.Credentials = new NetworkCredential("username", "password");
                    client.UseDefaultCredentials = false;
                    client.Timeout = 50000;

                    // Create the Outlook application.
                    Application oApp = new Application();

                    //string fromEmail =["from"].ToString();
                    MailItem oMsg = (MailItem)oApp.CreateItem(OlItemType.olMailItem);
                    Recipients oRecips = oMsg.Recipients;

                    // Change the recipient in the next line if necessary.
                    Recipient oRecip = oRecips.Add("");
                    oMsg.Subject = "Testing emailing work order";
                    oMsg.HTMLBody = "Type your message here";

                    //Add an attachment.
                    string sDisplayName = "WorkOrder";
                    int iPosition = oMsg.Body.Length + 1;
                    int iAttachType = (int)OlAttachmentType.olByValue;

                    //now attached the file
                    Microsoft.Office.Interop.Outlook.Attachment oAttach = oMsg.Attachments.Add(pdfFileName, iAttachType, iPosition, sDisplayName);
                    // Send.
                    // Clean up.
                    // writer.Flush();
                    oRecip = null;
                    oRecips = null;
                    oMsg = null;
                    oApp = null;


    • Moved by Grace MacJones-MSFT Thursday, October 31, 2019 10:58 PM question related to Microsoft outlook
    Friday, October 25, 2019 5:59 PM

All replies

  • Hi Aba,

    Apologies for the long delay. I'm assuming this code works locally. I don't believe it will work when deployed to an Azure App Service because the PaaS doesn't have Office installed on it. I can think of two workarounds off the top of my head.

    1.  Use Microsoft Graph API to create and send the email from your app. See for more details.
    2.  Configure your app to run inside a docker container. This way you can registered the necessary Office SDK binaries inside your image and deploy your image to your app service. See for more details

    Let me know if this helps.

    Thanks in advance, Ryan

    Monday, November 4, 2019 12:22 AM
  • @RyanHill-MSFT thanks for your response. I will look into your suggestions and revert.

    Also do you think this can also work using sendgrid?


    Monday, November 4, 2019 5:08 AM
  • @RyanHill-MSFT thanks for your response. I will look into your suggestions and revert.

    Also do you think this can also work using sendgrid?


    Yes it will. Sendgrid has a fairly robust SDK that will make it pretty seamless to use with a Sendgrid resource created in your Azure subscription. You can check it out here.

    Thanks in advance, Ryan

    Monday, November 4, 2019 5:02 PM
  • @RyanHill-MSFT

    It looks like sendgrid is only for clients to send emails to their customers from the app.

    but what we are looking for is for our customers using our web app to be able to send an email with attached converted report to pdf to their workers.

    Do you think sendgrid will allow our clients/customer to send email from the web app to their workers?

    Also, if I use a different smtp host like Gmail or hotmail , is it going to work on the azure app services?


    Wednesday, November 6, 2019 4:25 PM