I try to make a windows service in C# to send daily automatically emails, based on data read from a specified SQL server database.
I mananged to make the service run, to send email automatically but when I try to insert a sqlconnection service just doesn't work anymore.
Any ideas?
It works if I remove SqlConnection and data reader.
Here is my code:
System.Timers.Timer createOrderTimer;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
createOrderTimer = new System.Timers.Timer();
createOrderTimer.Elapsed += new System.Timers.ElapsedEventHandler(GetMail);
createOrderTimer.Interval = 2500;
createOrderTimer.Enabled = true;
createOrderTimer.AutoReset = true;
createOrderTimer.Start();
}
protected override void OnStop()
{
}
public void GetMail(object sender, System.Timers.ElapsedEventArgs args)
{
SqlConnection connect = new SqlConnection("Server=10.222.160.17,5356;"
+ "Database=tracking_tool;"
+ "Trusted_Connection=True;"
+ "user=admin;"
+ "password=root");
string line = "";
connect.Open();
SqlCommand GetData = new SqlCommand("select * from validation", connect);
SqlDataReader ReadData = null;
ReadData = GetData.ExecuteReader();
while (ReadData.Read())
{
line = ReadData["Line"].ToString();
}
connect.Close();
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("[email protected]");
mailMessage.From = new MailAddress("[email protected]");
mailMessage.Subject = "example";
mailMessage.Body = "example";
SmtpClient smtpClient = new SmtpClient("10.214.81.97");
smtpClient.Send(mailMessage);
}
Data Source=yourserver; Initial Catalog=yourdb; User Id=youruser; Password=yourpword;and definately removetrusted connectionlinevariable? you just keep replacing its value in the loop. what's the endgame here?