Open your Visual Studio 2102.

Then select "File" -> "New" -> "Project..." then select Empty SharePoint Project.

add new project

Then click Office/SharePoint.

Then click SharePoint Solutions.

Then click SharePoint 2013 Empty Project.

Then set your server name where you want to deploy your solution and choose Deploy on Farm solution.

Deploy on farm solution

Then click Finish.

Then right-click on Solution Explorer (or press Ctrl+ ;).

Then add a new item.

add new item

Select code from the Installed Menu under Visual C# Items and click on Class.

Add class

Please use the following code.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.SharePoint.Administration;
  6. using Microsoft.SharePoint;
  7. using System.Net.Mail;
  8. namespace CustomTimerJob {
  9. public class Timer: Microsoft.SharePoint.Administration.SPJobDefinition {
  10. public Timer(): base() {}
  11. public Timer(string jobName, SPService service, SPServer server, SPJobLockType targetType): base(jobName, service, server, targetType) {}
  12. public Timer(string jobName, SPWebApplication webApplication): base(jobName, webApplication, null, SPJobLockType.ContentDatabase) {
  13. this.Title = "Email Notification Job";
  14. }
  15. public override void Execute(Guid contentDbId) {
  16. string from = string.Empty;
  17. string smtpAddress = string.Empty;
  18. string to = "[email protected]";
  19. string subject = "Email Notification";
  20. string body = "<h1> Email Sending from Testing My Timer Job Application </h1>";
  21. SPSecurity.RunWithElevatedPrivileges(delegate() {
  22. // get a reference to the current site collection's content database
  23. SPWebApplication webApplication = this.Parent as SPWebApplication;
  24. SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];
  25. // get a reference to the "Tasks" list in the RootWeb of the first site collection in the content database
  26. SPWeb rootWeb = contentDb.Sites[0].RootWeb;
  27. // Get the DB News Announcements List
  28. SPList listjob = rootWeb.Lists.TryGetList("Tasks");
  29. // Get sender address from web application settings
  30. from = rootWeb.Site.WebApplication.OutboundMailSenderAddress;
  31. // Get SMTP address from web application settings
  32. smtpAddress = rootWeb.Site.WebApplication.OutboundMailServiceInstance.Server.Address;
  33. // Send an email if the news is approved
  34. bool emailSent = SendMail(smtpAddress, subject, body, true, from, to, null, null);
  35. if (listjob != null && emailSent) {
  36. SPListItem newListItem = listjob.Items.Add();
  37. newListItem["Title"] = string.Concat("Email Notification Sent at : ", DateTime.Now.ToString());
  38. newListItem.Update();
  39. }
  40. });
  41. }
  42. public bool SendMail(string smtpAddress, string subject, string body, bool isBodyHtml, string from, string to, string cc, string bcc) {
  43. bool mailSent = false;
  44. SmtpClient smtpClient = null;
  45. try {
  46. // Assign SMTP address
  47. smtpClient = new SmtpClient();
  48. smtpClient.Host = smtpAddress;
  49. //Create an email message
  50. MailMessage mailMessage = new MailMessage(from, to, subject, body);
  51. if (!String.IsNullOrEmpty(cc)) {
  52. MailAddress CCAddress = new MailAddress(cc);
  53. mailMessage.CC.Add(CCAddress);
  54. }
  55. if (!String.IsNullOrEmpty(bcc)) {
  56. MailAddress BCCAddress = new MailAddress(bcc);
  57. mailMessage.Bcc.Add(BCCAddress);
  58. }
  59. mailMessage.IsBodyHtml = isBodyHtml;
  60. // Send the email
  61. smtpClient.Send(mailMessage);
  62. mailSent = true;
  63. } catch (Exception) {
  64. mailSent = false;
  65. }
  66. return mailSent;
  67. }
  68. }
  69. }
Then add a New Feature and change the Scope and rename it .

add a New Feature

Then create another class and copy this code.

We need to set the interval of our timer job application.
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Security.Permissions;
  4. using Microsoft.SharePoint;
  5. using Microsoft.SharePoint.Security;
  6. using System.Linq;
  7. namespace CustomTimerJob.Features.NotificationTimerJobFeature {
  8. public class EventReceiver: SPFeatureReceiver {
  9. // Name of the Timer Job, but not the Title which is displayed in central admin
  10. private
  11. const string List_JOB_NAME = "NotificationTimerJob";
  12. // Uncomment the method below to handle the event raised after a feature has been activated.
  13. public override void FeatureActivated(SPFeatureReceiverProperties properties) {
  14. SPSecurity.RunWithElevatedPrivileges(delegate() {
  15. SPSite site = properties.Feature.Parent as SPSite;
  16. // make sure the job isn't already registered
  17. site.WebApplication.JobDefinitions.Where(t => t.Name.Equals(List_JOB_NAME)).ToList().ForEach(j => j.Delete());
  18. // install the job
  19. Timer listLoggerJob = new Timer(List_JOB_NAME, site.WebApplication);
  20. SPMinuteSchedule schedule = new SPMinuteSchedule();
  21. schedule.BeginSecond = 0;
  22. schedule.EndSecond = 59;
  23. schedule.Interval = 15;
  24. listLoggerJob.Schedule = schedule;
  25. listLoggerJob.Update();
  26. });
  27. }
  28. // Uncomment the method below to handle the event raised before a feature is deactivated.
  29. public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
  30. SPSecurity.RunWithElevatedPrivileges(delegate() {
  31. SPSite site = properties.Feature.Parent as SPSite;
  32. // delete the job
  33. site.WebApplication.JobDefinitions.Where(t => t.Name.Equals(List_JOB_NAME)).ToList().ForEach(j => j.Delete());
  34. });
  35. }
  36. // Uncomment the method below to handle the event raised after a feature has been installed.
  37. //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
  38. //{
  39. //}
  40. // Uncomment the method below to handle the event raised before a feature is uninstalled.
  41. //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
  42. //{
  43. //}
  44. // Uncomment the method below to handle the event raised when a feature is upgrading.
  45. //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
  46. //{
  47. //}
  48. }
  49. }
Then deploy the project.

deploy

After successful deployment go to your SharePoint central administrator.

Click on monitoring.

Click on monitoring
Then click Review job definition.

monitoring

Then go to your created timer job application.

It’s working successfully.