|
This simple application is designed to be placed into the scheduler of a server to run daily, and to then read from an XML file to determine
when to send which reports to whom. The chief goal of this application once in place is that no direct server access be required for updates or
maintenance. Reports can be added, removed, or changed, and the recipients and schedule modified as well, all with FTP or FrontPage access only.
The application consists of two parts -- the EXE file that must be placed into the Task Scheduler on the server (it doesn't even have to be the
web server where the reports reside, but it should have a persistent internet connection and it must have an SMTP service running on it or else
it needs modified to use an external SMTP server), and the XML configuration file that can reside anywhere as long as the EXE can reference it
via URI. The application can also be run from the command line without an XML configuration file.
EmailReport.cs:
using System; using System.Data; using System.IO; using System.Net; using System.Web; using System.Web.Mail; using System.Xml;
namespace ASPAlliance { /// <summary> /// Summary description for Class1. /// </summary> class EmailReport { // Edit this to point to the report configuration file. const string config_url = @"http://localhost/Reports.xml"; static DataView dv; static DataSet ds;
static void Main(string[] args) { // Get Parameters try { dv = ReadParams(args); } catch { Console.WriteLine("usage: EmailReport <to email> <from email> <subject> <url> <frequency in integer days>"); Console.WriteLine("or"); Console.WriteLine("EmailReport"); Console.WriteLine(" which reads from online XML config file."); return; }
// Loop through all rows for(int i=0;i<dv.Count;i++) { string dayofweek; int dayofmonth; if((dv[i]["weekday"]!=null)&&(dv[i]["weekday"].ToString().Length > 0)) { dayofweek = dv[i]["weekday"].ToString().ToLower(); } else { dayofweek = ""; } if((dv[i]["monthday"]!=null)&&(dv[i]["monthday"].ToString().Length > 0)) { dayofmonth = Int32.Parse(dv[i]["monthday"].ToString()); } else { dayofmonth = -1; } // Determine if Frequency requires a send if( (System.DateTime.Now.Day % Int32.Parse(dv[i]["frequency"].ToString()) == 0) || (System.DateTime.Now.DayOfWeek.ToString().ToLower() == dayofweek) || (System.DateTime.Now.Day == dayofmonth) ) { try { sendEmail(dv[i]["to_email"].ToString(), dv[i]["from_email"].ToString(), dv[i]["subject"].ToString(), readHtmlPage(dv[i]["url"].ToString()), System.Web.Mail.MailFormat.Html); } catch(Exception ex) { Console.WriteLine(ex.ToString()); } } } } static DataView ReadParams(string[] args) { if (args.Length < 5) { // Read from XML File ds = new DataSet(); ds.ReadXml(config_url); return ds.Tables[0].DefaultView;
} else { DataTable dt = new DataTable(); DataRow dr;
dt.Columns.Add(new DataColumn("url", typeof(string))); dt.Columns.Add(new DataColumn("to_email", typeof(string))); dt.Columns.Add(new DataColumn("from_email", typeof(string))); dt.Columns.Add(new DataColumn("subject", typeof(string))); dt.Columns.Add(new DataColumn("frequency", typeof(Int32)));
dr = dt.NewRow();
dr["to_email"] = args[0]; dr["from_email"] = args[1]; dr["subject"] = args[2]; dr["url"] = args[3]; dr["frequency"] = Int32.Parse(args[4]); dt.Rows.Add(dr); return new DataView(dt); } }
private static String readHtmlPage(string url) { String result; WebResponse objResponse; WebRequest objRequest = System.Net.HttpWebRequest.Create(url); objResponse = objRequest.GetResponse(); using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()) ) { result = sr.ReadToEnd(); // Close and clean up the StreamReader sr.Close(); } return result; }
private static void sendEmail(string To, string From, string Subject, string Body, System.Web.Mail.MailFormat Format) { MailMessage Mailer = new MailMessage(); Mailer.From = From; Mailer.To = To; Mailer.Subject = Subject; Mailer.Body = Body; Mailer.BodyFormat = Format; SmtpMail.Send(Mailer); } } }
| | csharpindex.com/colorCode |
Reports.xml:
<?xml version="1.0" encoding="utf-8" ?> <reports> <report name="Daily Expiring Ads"> <to_email>you@there.com</to_email> <from_email>me@here.com</from_email> <subject>Daily Expiring Ads Report</subject> <url>http://localhost/Report.aspx</url> <!-- frequency should be a number from 1 to 30. Report will be sent out if day of month is divisible by frequency. --> <frequency>1</frequency> <!-- If used, represents day of week (monday, tuesday, wednesday, etc.) on which report should be sent out each week --> <weekday></weekday> <!-- If used, represents day of month (1 thru 31) on which report should be sent out each month --> <monthday></monthday> </report> <report name="Weekly Expiring Ads"> <to_email>you@there.com</to_email> <from_email>me@here.com</from_email> <subject>Daily Expiring Ads Report</subject> <url>http://localhost/Report.aspx</url> <!-- frequency should be a number from 1 to 30. Report will be sent out if day of month is divisible by frequency. --> <frequency>30</frequency> <!-- If used, represents day of week (monday, tuesday, wednesday, etc.) on which report should be sent out each week --> <weekday>Tuesday</weekday> <!-- If used, represents day of month (1 thru 31) on which report should be sent out each month --> <monthday></monthday> </report> <report name="Monthly Expiring Ads"> <to_email>you@there.com;you2@there.com</to_email> <from_email>me@here.com</from_email> <subject>Daily Expiring Ads Report</subject> <url>http://localhost/Report.aspx</url> <!-- frequency should be a number from 1 to 30. Report will be sent out if day of month is divisible by frequency. --> <frequency>28</frequency> <!-- If used, represents day of week (monday, tuesday, wednesday, etc.) on which report should be sent out each week --> <weekday>Wednesday</weekday> <!-- If used, represents day of month (1 thru 31) on which report should be sent out each month --> <monthday>23</monthday> </report> </reports>
| | csharpindex.com/colorCode |
Hope this helps. Right now there are three options for scheduling, and they are OR'd together. So if you want something to be mailed EVERY DAY you
can just set the frequency field to 1. If you want it to go out every X days, set frequency to X. If you want it to go out every Monday, set
weekday to "Monday". And if you just want it to go out on the 15th of the month, set monthday to 15.
|