c# - Quartz Job isn't Executing using .net -
c# - Quartz Job isn't Executing using .net -
i'm using quartz in windows service application. code i've used work in console application when inserted job execute , wont send email out.
public partial class service1 : servicebase { ischedulerfactory sf = new stdschedulerfactory(); ischeduler schd; public service1() { initializecomponent(); if(!system.diagnostics.eventlog.sourceexists("mysource")) { system.diagnostics.eventlog.createeventsource("mysource", "mynewlog"); } eventlog1.source = "mysource"; eventlog1.log = "mynewlog"; canpauseandcontinue = true; } protected override void onstart(string[] args) { eventlog1.writeentry("in onstart"); schd = sf.getscheduler(); schd.start(); ijobdetail job = jobbuilder.create<hellojob>() .withidentity("checkthis", "groupthis") .build(); itrigger trigger = triggerbuilder.create() .withidentity("triggerthis", "groupthis") .withsimpleschedule(x => x .withintervalinseconds(5) .repeatforever()) .build(); schd.schedulejob(job, trigger); } protected override void onstop() { var jobkey = new jobkey("checkthis","groupthis"); eventlog1.writeentry("in onstop"); schd.pausejob(jobkey); eventlog1.writeentry("job stopped"); } protected override void onpause() { eventlog1.writeentry("in onpause"); schd.pauseall(); } protected override void oncontinue() { base.onpause(); eventlog1.writeentry("in oncontinue"); schd.resumeall(); } public class hellojob : ijob { public void execute(ijobexecutioncontext context) { string body = "windows service working."; // message send out var fromaddress = new mailaddress("**************************@gmail.com", "**********"); // address sending email var toaddress = new mailaddress("***************@vtext.com", "************"); // address sending email const string frompassword = "*************"; // password address kwere sending const string subject = ""; // subject of email sending (note: in texts subjects show parenthesis) string connectionstring = "*******************************"; using (sqlconnection connection = new sqlconnection(connectionstring)) using (sqlcommand command = new sqlcommand("select * addressinformation bring together orders o on o.userid = a.userid bring together products p on p.productid = o.productid [email address] not null", connection)) { connection.open(); using (sqldatareader reader = command.executereader()) { while (reader.read()) { body += "\ncustomer name:" + reader["customername"].tostring() + " product: " + reader["productname"].tostring(); } } } var smtp = new smtpclient // create instance of smtp used send email (simple mail service transfer protocol (smtp) net standard electronic mail service (e-mail) transmission.) { host = "smtp.gmail.com", // gets or sets name or ip address of host used smtp transactions port = 587, // gets or sets port used smtp transactions enablessl = true, //specify whether smtpclient uses secure sockets layer (ssl) encrypt connection. deliverymethod = smtpdeliverymethod.network, //specifies how outgoing email messages handled.(email sent through network smtp server.) usedefaultcredentials = false, //gets or sets boolean value controls whether defaultcredentials sent requests. (defaultcredentials represents scheme credentials current security context in application running. ) credentials = new networkcredential(fromaddress.address, frompassword) //provides credentials password-based authentication schemes. }; using (var message = new mailmessage(fromaddress, fromaddress) //creates mailmessage instance , addresses { subject = subject, // subject of message body = body // body of message }) { smtp.send(message); //sends mesage using our instance of smtpclient } } } } i curious knew why service application wasn't sending emails out yet console application will. help appreciated!
i giving service application local scheme permission when needed user permissions connect database.i needed have constructor job otherwise wont execute in windows service applications!
c# .net quartz-scheduler quartz.net
Comments
Post a Comment