NotifyIcon is not shown when Windows Service is installed

632 Views Asked by At

I have Created a Windows Service coded same as below. The Service works great but the Problem only is NotifyIcon is not Shown on WindowsTray. OnStart service I have called Timer and timer_Tick Event with INBOX and Sent is Called. When I debug the code using IF DEBUG ELSE ..... The NotifyIcon is shown on Tray but When I Install Service then, It is not showing. Dont know ere the problem is?

public partial class Service1 : ServiceBase
    {
        private Timer timer1 = null;

    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        timer1 = new Timer();
        this.timer1.Interval = 15000;
        this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, ElapsedEventArgs e)
    {
        GetSentItems();
        GetInbox();
    }

    protected override void OnStop()
    {
        timer1.Enabled = false;
    }

    #region GET SENT ITEMS
    public void GetSentItems()
    {
        var client = new ImapClient("imap.xyz.com", false);
        var folderPath = "";
        if (client.Connect())
        {
            if (client.Login("[email protected]", "qwerty@123"))
            {
                var folder = client.Folders.Sent;
                var messages = folder.Search("ALL");
                var fileName = "";
                var count = messages.Length;
                foreach (var x in messages)
                {
                    //if (x.Subject.Contains("Request"))
                    //{
                    folderPath = @"C:\Attachment\SentItems";
                    if (!Directory.Exists(folderPath))
                    {
                        Directory.CreateDirectory(folderPath);
                    }
                    foreach (var file in x.Attachments)
                    {
                        //file.Download(); // Downloading the attachment                    
                        fileName = file.FileName;
                        //file.Save(folderPath);
                    }
                    var Date = x.Date.Value;
                    var From = x.From.Address;
                    var to = x.To[0].Address;
                    var subject = x.Subject;
                    // var attchment = x.Attachments[0].FileData;
                    var Body = x.BodyParts[0].ContentStream;
                    SqlConnection con = new SqlConnection();
                    con.ConnectionString = "#constring";
                    string textbody = Body;
                    textbody = "'" + textbody.Replace("'", "") + "'";
                    string query1 = "SP_SentEmail";
                    SqlCommand cmd1 = new SqlCommand(query1, con);
                    cmd1.CommandType = CommandType.StoredProcedure;
                    cmd1.Parameters.AddWithValue("@E_Date", Date);
                    cmd1.Parameters.AddWithValue("@E_From", From);
                    cmd1.Parameters.AddWithValue("@E_TextBody", textbody);
                    cmd1.Parameters.AddWithValue("@Subject", subject);
                    cmd1.Parameters.AddWithValue("@E_attachments", fileName);
                    con.Open();
                    cmd1.ExecuteNonQuery();
                    con.Close();


                }
            }
        }
    }
    #endregion

    #region GET INBOX
    public void GetInbox()
    {
        var client = new ImapClient("imap.xyz.com", false);
        var folderPath = "";
        if (client.Connect())
        {
            if (client.Login("[email protected]", "qwerty@123"))
            {
                var folderInbox = client.Folders.Inbox;
                var messagesInbox = folderInbox.Search("UNSEEN");
                var fileNameInbox = "";
                var countInbox = messagesInbox.Length;
                foreach (var x in messagesInbox)
                {
                    //if (x.Subject.Contains("Request"))
                    //{
                    folderPath = @"C:\Attachment\Inbox";
                    if (!Directory.Exists(folderPath))
                    {
                        Directory.CreateDirectory(folderPath);
                    }
                    foreach (var file in x.Attachments)
                    {
                        //file.Download(); // Downloading the attachment                    
                        fileNameInbox = file.FileName;
                        //file.Save(folderPath);
                    }
                    var Date = x.Date.Value;
                    var From = x.From.Address;
                    var to = x.To[0].Address;
                    var subject = x.Subject;
                    var Body = x.BodyParts[0].ContentStream;
                    SqlConnection con = new SqlConnection();
                    con.ConnectionString = "Data Source....";
                    string textbody = Body;
                    textbody = "'" + textbody.Replace("'", "") + "'";
                    string query2 = "SP_EmailInbox";
                    SqlCommand cmd2 = new SqlCommand(query2, con);
                    cmd2.CommandType = CommandType.StoredProcedure;
                    cmd2.Parameters.AddWithValue("@E_Date", Date);
                    cmd2.Parameters.AddWithValue("@E_From", From);
                    cmd2.Parameters.AddWithValue("@E_TextBody", textbody);
                    cmd2.Parameters.AddWithValue("@Subject", subject);
                    cmd2.Parameters.AddWithValue("@E_attachments", fileNameInbox);
                    cmd2.Parameters.Add("@Inserted", SqlDbType.Bit).Direction = ParameterDirection.Output;
                    con.Open();
                    cmd2.ExecuteNonQuery();
                    con.Close();
                    bool IsInserted = Convert.ToBoolean(cmd2.Parameters["@Inserted"].Value);
                    if (IsInserted == true)
                    {
                        notifyIcon1.Icon = SystemIcons.Application;
                        notifyIcon1.BalloonTipTitle = "Mail Application";
                        notifyIcon1.BalloonTipText = "New Mail: " + subject.Substring(0, subject.Length);
                        notifyIcon1.ShowBalloonTip(1000);
                    }
                }
            }
        }
    #endregion


    }
}
0

There are 0 best solutions below