SharePoint 2010 Timer Job For Sending Email Notification

Every one are very familiar with SharePoint 2010 Timer Job. We just take a quick look over Timer Job. SharePoint handles some repetitive background process at some specific time is called ” Timer Job”.
Now the question is How the Timer Job will Help you ? OK, suppose you are a Project Manager and you want to remind your colleague to update your daily status. You just write a timer job that will send the email to your colleague for updating the daily status sheet.
Lets start with walk through ….. :)
  • Crate a SharePoint Custom list and Name it “CustomEmailNotifications” and Store the Email addresses in Title Column.
  • Open Visual Studio 2010 and create a new Empty SharePoint Project and name it “NewNotificationTimerJob“.
  • Add a new Class and call it “CustomEmailNotificationJob”.
  • Copy following code.
public class CustomEmailNotificationJob:SPJobDefinition
{
public const string JOB_NAME = “Custom Daily Status Notifications Job”;
public CustomEmailNotificationJob()
: base()
{
}
public CustomEmailNotificationJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
: base(jobName, service, server, targetType)
{
}
public CustomEmailNotificationJob(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
{
this.Title = JOB_NAME;
}
public override void Execute(Guid contentDbId)
{
// Change your site
using (SPSite oSPsite = new SPSite(“http://abcd/”))
{
using (SPWeb oSPWeb = oSPsite.OpenWeb())
{
oSPWeb.AllowUnsafeUpdates = true;
// Fetch the List
SPList list = oSPWeb.Lists["CustomEmailNotifications"];
SPListItemCollection collListItem = list.Items;
foreach (SPListItem oListItem in collListItem)
{
EmailUser(oListItem.Title);
}
}
}
}
private bool EmailUser(string emailAddress)
{
MailMessage mail = new MailMessage();
//change the Email address
mail.From = new MailAddress(“someone@mail.com”);
mail.To.Add(emailAddress);
mail.Subject = “Daily Status”;
mail.Body = “Please Update your Daily Status on Portal : “;
//Change to your SMTP server
SmtpClient smtp = new SmtpClient(“abc.com”);
smtp.Send(mail);
return true;
}
}
  • Next we need to add a Feature, so right-click the Features folder and add Feature
  • After adding the Feature, right-click it and Add Event Receiver
  • Copy the following code to the event receiver:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
//remove the job if it already exists
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == CustomEmailNotificationJob.JOB_NAME)
job.Delete();
}
// create the job
CustomEmailNotificationJob emailJob = new CustomEmailNotificationJob(CustomEmailNotificationJob.JOB_NAME, site.WebApplication);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 5;
emailJob.Schedule = schedule;
emailJob.Update();
}
// Uncomment the method below to handle the event raised before a feature is deactivated.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == CustomEmailNotificationJob.JOB_NAME)
job.Delete();
}
}
  • Now the last step is to set the scope of the feature to Site
  • Build the Project and Deploy.
  • Go to Central Admin — > Monitoring — >Timer Jobs — >Review job Definition .
Timerjob1
  • In the Review Job Definitions you can see your timer job.
  • Hope every thing is write you will receive notification.
Debug Your Timer Job
For debugging your timer job you can follow this link  “ http://msdn.microsoft.com/en-us/library/ff798310.aspx “
Enjoy  ….. :) :) :) !

Comments