Hi ,
This is Sundar , i want to develope one small Application in windows or web ,that should send the mail continiously according to at what time scheduled in the application and that should Running in the Background always like windows service,SQL server ........
so , please help me ......................
![]() |
0 |
![]() |
what;s the problem if you develop an windows service and install it in your system.
http://www.15seconds.com/issue/021007.htm
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
![]() |
-1 |
![]() |
see, i have created the Windows services and which is running in the Background also but the mail is not going, if i manualy click that .EXE file ,then Mails are going .... so what is the Problems................
Code is Here,
---------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net.Mail;
using System.Web.Mail;
//using System.Windows.Forms;
using System.Net;
namespace WindowsService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1.Enabled = true;
timer1.Start();
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
timer1.Enabled = false;
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient("smtp.gmail.com"); //pop.gmail.com smtp.gmail.com
client.EnableSsl = true;
MailAddress from = new MailAddress("sundar@armaninfotech.com", "[ Your full name here]");
MailAddress to = new MailAddress("shailesh@armaninfotech.com", "Your recepient name");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);
message.Body = "This is a test For Checking the Windows Service........ ";
message.Subject = "This is For Checking the Windows Service is Running in the Background..Test By sundar........ ";
NetworkCredential myCreds = new NetworkCredential("sundar@armaninfotech.com", "sundar123", "");
client.Credentials = myCreds;
try
{
client.Send(message);
//MessageBox.Show("Message Has Been Sent Successfully!");
}
catch (Exception ex)
{
// Console.WriteLine("Exception is:" + ex.ToString());
//MessageBox.Show("Exception is:" + ex.ToString());
}
}
}
}-------------------------
then i installed this .exe file using InstallUtil.....then i started my service.................
![]() |
0 |
![]() |
are you sure you started service.
go to start->run-> services.msc->select your service written-> and check its status, if it not started, start it.
and also, you can debug that by writing some sort of checkcommands to eventlog in some methods.
EventLog.WriteEntry(".Service started"); // keep this line in start method
EventLog.WriteEntry(".Timer event fired"); // keep this in timer_tick event
this is my previously written service code part
SqlConnection PubsConn; private System.Timers.Timer timer = null; public RemainderService() // this is the constructor { InitializeComponent(); string servicepollinterval = "10000"; double interval = 10000; try { interval = Convert.ToDouble(servicepollinterval); } catch (Exception) { } timer = new System.Timers.Timer(interval); timer.Elapsed += new System.Timers.ElapsedEventHandler(this.ServiceTimer_Tick); } protected override void OnStart(string[] args) { EventLog.WriteEntry(ServiceName + " Service started1"); EventLog.WriteEntry(ServiceName + " Service started2"); EventLog.WriteEntry(ServiceName + " Before Timer Initialized."); EventLog.WriteEntry(ServiceName + " Timer Initialized."); timer.Enabled = true; } protected override void OnStop() { EventLog.WriteEntry(ServiceName + " Service stopped"); timer.Enabled = false; } protected override void OnPause() { EventLog.WriteEntry(ServiceName + " Service paused"); timer.Enabled = false; } protected override void OnContinue() { EventLog.WriteEntry(ServiceName + " Service continued"); timer.Enabled = true; } private void ServiceTimer_Tick(object sender, System.Timers.ElapsedEventArgs e) { this.timer.Stop(); RunCommands(); this.timer.Start(); }
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
![]() |
0 |
![]() |
Check below link
Haissam Abdul Malak
MCAD.NET
| Blog |
![]() |
0 |
![]() |
you did not get my question proberly... i want to Run my .exe in the Background like windows services..................(eg...Iexplore.exe)....that mean it should executing always in Background of OS ...........
![]() |
0 |
![]() |
see , I have Started My Services also, it is showing like Started ...........but i did not get Mails...............
![]() |
0 |
![]() |
add the debug statements of writing to eventlog in timer start ,stop events and also that timer_tick event as i suggested in my previous post
then check your event log and tell me the results.
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
![]() |
0 |
![]() |
After Added this line only--------> EventLog.WriteEntry(".Service started");
--its working fine , tell me what is the Reason & use of those lines becase i am new to this functionality.................
Thanks ............................
![]() |
0 |
![]() |
first time only mails are going after that my services is stoped Automatically & so this Event (timer1_Tick) is not Running ,what is the problem........
Code : ----------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net.Mail;
using System.Web.Mail;
//using System.Windows.Forms;
using System.Net;
namespace WindowsService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1.Enabled = true;
timer1.Start();
EventLog.WriteEntry("OnStart---->.Service started"); // keep this line in start method
EventLog.WriteEntry("OnStart---->.Timer event fired"); // keep this in timer_tick event
// TODO: Add code here to start your service.
SmtpClient client = new SmtpClient("smtp.gmail.com"); //pop.gmail.com smtp.gmail.com
client.EnableSsl = true;
MailAddress from = new MailAddress("sundar@armaninfotech.com", "[ Your full name here]");
MailAddress to = new MailAddress("shailesh@armaninfotech.com", "Your recepient name");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);
message.Body = "This is a test e-mail message sent using gmail as a relay server ";
message.Subject = "Gmail test email with SSL and Credentials";
NetworkCredential myCreds = new NetworkCredential("sundar@armaninfotech.com", "sundar123", "");
client.Credentials = myCreds;
try
{
client.Send(message);
//MessageBox.Show("Message Has Been Sent Successfully!");
}
catch (Exception ex)
{
// Console.WriteLine("Exception is:" + ex.ToString());
//MessageBox.Show("Exception is:" + ex.ToString());
}
}
protected override void OnStop()
{
EventLog.WriteEntry("OnStop----->.Service started"); // keep this line in start method
EventLog.WriteEntry("OnStop------>.Timer event fired"); // keep this in timer_tick event
// TODO: Add code here to perform any tear-down necessary to stop your service.
timer1.Enabled = false;
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
//timer1.Enabled = false;
//timer1.Stop();
timer1.Enabled = true;
timer1.Start();
EventLog.WriteEntry("timer1_Tick------>.Service started"); // keep this line in start method
EventLog.WriteEntry("timer1_Tick------->.Timer event fired"); // keep this in timer_tick event
SmtpClient client = new SmtpClient("smtp.gmail.com"); //pop.gmail.com smtp.gmail.com
client.EnableSsl = true;
MailAddress from = new MailAddress("sundar@armaninfotech.com", "[ Your full name here]");
MailAddress to = new MailAddress("shailesh@armaninfotech.com", "Your recepient name");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);
message.Body = "This is a test For Checking the Windows Service........ ";
message.Subject = "This is For Checking the Windows Service is Running in the Background..Test By sundar........ ";
NetworkCredential myCreds = new NetworkCredential("sundar@armaninfotech.com", "sundar123", "");
client.Credentials = myCreds;
try
{
client.Send(message);
//MessageBox.Show("Message Has Been Sent Successfully!");
}
catch (Exception ex)
{
// Console.WriteLine("Exception is:" + ex.ToString());
//MessageBox.Show("Exception is:" + ex.ToString());
}
}
}
}
![]() |
1 |
![]() |
Sundar123:
first time only mails are going after that my services is stoped Automatically & so this Event (timer1_Tick) is not Running ,what is the problem........because you write the mail code in timer start event. when the application runs, timer started, that means timer start event will fire. and your code is executing. but the timer tick event is not firing because of the reasons i think you didn't set interval and declare its timer tick event. so only once its sending mails. and service stopped, reason for this is i am not sure. but previously i heard, that service will stop if it has been idle. in your case its idle. so, its happening like that.
What my advice is better, to go to this URL and design the service as said in that URL. I too implement 6 services using that URL in 10 Minutes.
http://www.15seconds.com/issue/021007.htm
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
![]() |
0 |
![]() |
I tried your Code & put the send Mail Codes inside the Start() & Timer_ticler() also but which is not executing of Mailing parts ....... so give me the solution & Reply me.........
![]() |
0 |
![]() |
i changed your code and try this.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Text; using System.Net.Mail; using System.Web.Mail; //using System.Windows.Forms; using System.Net; namespace WindowsService { public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); string servicepollinterval = "10000"; double interval = 10000; try { interval = Convert.ToDouble(servicepollinterval); } catch (Exception) { } timer = new System.Timers.Timer(interval); timer.Elapsed += new System.Timers.ElapsedEventHandler(this.ServiceTimer_Tick); } protected override void OnStart(string[] args) { EventLog.WriteEntry(ServiceName + " Service started1"); EventLog.WriteEntry(ServiceName + " Service started2"); EventLog.WriteEntry(ServiceName + " Before Timer Initialized."); EventLog.WriteEntry(ServiceName + " Timer Initialized."); timer.Enabled = true; } protected override void OnStop() { EventLog.WriteEntry(ServiceName + " Service stopped"); timer.Enabled = false; } protected override void OnPause() { EventLog.WriteEntry(ServiceName + " Service paused"); timer.Enabled = false; } protected override void OnContinue() { EventLog.WriteEntry(ServiceName + " Service continued"); timer.Enabled = true; } private void ServiceTimer_Tick(object sender, System.Timers.ElapsedEventArgs e) { this.timer.Stop(); SmtpClient client = new SmtpClient("smtp.gmail.com"); //pop.gmail.com smtp.gmail.com client.EnableSsl = true; MailAddress from = new MailAddress("sundar@armaninfotech.com", "[ Your full name here]"); MailAddress to = new MailAddress("shailesh@armaninfotech.com", "Your recepient name"); System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to); message.Body = "This is a test For Checking the Windows Service........ "; message.Subject = "This is For Checking the Windows Service is Running in the Background..Test By sundar........ "; NetworkCredential myCreds = new NetworkCredential("sundar@armaninfotech.com", "sundar123", ""); client.Credentials = myCreds; try { client.Send(message); //MessageBox.Show("Message Has Been Sent Successfully!"); } catch (Exception ex) { // Console.WriteLine("Exception is:" + ex.ToString()); //MessageBox.Show("Exception is:" + ex.ToString()); } this.timer.Start(); } } }
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
![]() |
0 |
![]() |
thanks , its Really working fine ...........tell me what i did the mistake in this.................
![]() |
0 |
![]() |
you didn't declare timer tick event in the constructor so that event is not firing,
you didn't set timer interval in the constructor
in that timer tick event , you have to stop the timer , then execute your code and again you have to start the timer.
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
![]() |
0 |
![]() |