none
Window Service started but not working RRS feed

  • Question

  • protected override void OnStart(string[] args)
            {
                    //// Create the Outlook application by using inline initialization.
                Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();

                ////Create the new message by using the simplest approach.
                Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

                //Add a recipient.
                // TODO: Change the following recipient where appropriate.
                Microsoft.Office.Interop.Outlook.Recipients oRecips = oMsg.Recipients;
                List<string> oTORecip = new List<string>();
                List<string> oCCRecip = new List<string>();

                oTORecip.Add("Mymailid");
                //oCCRecip.Add("example@test.com");
                foreach (string t in oTORecip)
                {
                    Microsoft.Office.Interop.Outlook.Recipient oTORecipt = oRecips.Add(t);
                    oTORecipt.Type = (int)Microsoft.Office.Interop.Outlook.OlMailRecipientType.olTo;
                    oTORecipt.Resolve();
                }

                foreach (string t in oCCRecip)
                {
                    Microsoft.Office.Interop.Outlook.Recipient oCCRecipt = oRecips.Add(t);
                    oCCRecipt.Type = (int)Microsoft.Office.Interop.Outlook.OlMailRecipientType.olCC;
                    oCCRecipt.Resolve();
                }

                //Set the basic properties.
                oMsg.Subject = "RH Reminder- " + DateTime.Today.ToString("MM/dd/yyyy");
                oMsg.HTMLBody = "Content"
                string date = DateTime.Today.ToString("MM-dd-yyyy");

                //Add an attachment.
                // TODO: change file path where appropriate
                String sSource = "RHCalender.png";
                String sDisplayName = "RHCalender";
                int iPosition = (int)oMsg.Body.Length + 1;
                int iAttachType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue;
                Microsoft.Office.Interop.Outlook.Attachment oAttach ;//= oMsg.Attachments.Add(sSource, iAttachType, iPosition, sDisplayName);

                // If you want to, display the message.
                // oMsg.Display(true);  //modal

                //Send the message.
                oMsg.Save();
                oMsg.Send();

                //Explicitly release objects.
                oTORecip = null;
                oCCRecip = null;
                oAttach = null;
                oMsg = null;
                oApp = null;

            }

    My windows service started successfully but not sending any mail. I have tested this code seperately its woking fine, but not with windows service. No error log in Event Viewer. What ll be issue?

    • Moved by Jiayi Li Thursday, October 15, 2015 2:38 AM
    Wednesday, October 14, 2015 10:19 AM

Answers

All replies

  • Hi Nandhan,

    Thank you for posting in MSDN forum.

    This forum is discuss about .net framework setup and servicing.

    As your issue is related to office, I help you move this case to Msdn forums - Outlook for Developers - Microsoft for dedicated support.

    Thank you for your understanding.

    Best Regards,

    Wen Xu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    Click HERE to participate the survey.



    • Edited by 宝宝徐 Thursday, October 15, 2015 2:36 AM
    Thursday, October 15, 2015 2:32 AM
  • Hi,

    Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    You can get more detail about this topic from link below:Considerations for server-side Automation of Office

    As a alternative, I suggest that you consider other way to send the emails as using SmtpClient .

    Hope it is helpful.

    Regards & Fei

     

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 16, 2015 1:42 AM
    Moderator
  • If you deal only with Exchange accounts consider using EWS, see EWS Managed API, EWS, and web services in Exchange for more information. If not, you can classes from BCL (System.Net) for creating and sending emails. 

     
    Friday, October 16, 2015 6:11 AM