How to create an automated, custom email report in a C# timer job
David Maillie
Data Scientist / Senior Lead Analyst at Ahold Delhaize USA (formerly Peapod Digital Labs and Retail Business Services)
In the previous article we covered how to use a C# timer job to connect with SharePoint lists and SQL database tables. Now we are going to cover how to send out an automated, custom email report – just sending out a simple email with C# is too easy. This process can be added to the try/catch portion of any timer job to send an email out to recipients.
First we have to open the email client. In this case we are using SMTP, so the first three statements that are required are:
This is where you will enter in the port and server or host name. If these are incorrect the email will not get delivered.
Next is the email header (mail to and from). You can send this email message to as many people as you need to.
For each additional recipient add a new mail.to.add line as shown above. That is pretty self-explanatory. Next is the subject line.
Again, self-explanatory.
Next is the body. For a regular alert email you can just add the wording required and any parameters, fields, calculated values, etc… In this case, I am actually turning the email body into a report that uses html to create tables for displaying the data. To do this the isBodyHtml tag needs to be set to true:
The html report header will be created using tr and td tags to spate the columns:
Next is the report body which can have many to zero rows. You will use a loop of some sort to run through all the items in the list (in this case I am using a for loop). n is the counter variable and is set to zero. K is the total number of items brought back from a CAML query on a SharePoint list but this could be a total from a SQL database table, etc… In this case, the logic is that for each item in the list where Phase4[n] (an array value) equals “No” the report will add another line.
Then to close the report html table and send the email you will need to enter the ending table html tag and the client.send(mail) statement.
Be sure and test this in your development or test environment first and set all of the emails to yourself. Once you have tested it and it is built to look the way you want, then you can change the list, port, mail server/host settings to reflect your production domain and then build it into an executable file and schedule it to run as desired. This example report was set to email recipients on Friday mornings.
Below is an actual example of an email that was sent out with this code:
I would also like to take a moment to thank God as he is the one that makes everything possible!
Your eye is a lamp that provides light for your body. When your eye is good, your whole body is filled with light. Matthew 6:22
David Maillie is a technology aficionado and sales superstar available for motivational speaking, training and education to groups, businesses and charities.
Other great posts and content from David Maillie that you will definitely want to see:
How to fix a WebFocus report Excel versioning error
How to do a silent install remotely on numerous workstations with PSEXEC
How to use C# timer jobs to access SharePoint data and write it into a SQL database
How to fix SAP Crystal Reports not running - common issue
How to access SharePoint data in SAP Crystal Reports!
How to copy SharePoint data quickly without any code!
How to create, find and fix alerts in SharePoint
What is the difference between a SharePoint list and library?
Power Platform Architect and Developer at Microsoft. I specialize in optimizing the utilization of Microsoft’s cloud productivity services for my clients
8 年David great posts, keep them coming!