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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.SharePoint.Administration;
- using Microsoft.SharePoint;
- using System.Net.Mail;
-
- namespace CustomTimerJob {
- public class Timer: Microsoft.SharePoint.Administration.SPJobDefinition {
- public Timer(): base() {}
- public Timer(string jobName, SPService service, SPServer server, SPJobLockType targetType): base(jobName, service, server, targetType) {}
- public Timer(string jobName, SPWebApplication webApplication): base(jobName, webApplication, null, SPJobLockType.ContentDatabase) {
- this.Title = "Email Notification Job";
- }
- public override void Execute(Guid contentDbId) {
- string from = string.Empty;
- string smtpAddress = string.Empty;
- string to = "[email protected]";
- string subject = "Email Notification";
- string body = "<h1> Email Sending from Testing My Timer Job Application </h1>";
- SPSecurity.RunWithElevatedPrivileges(delegate() {
-
- SPWebApplication webApplication = this.Parent as SPWebApplication;
- SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];
-
-
- SPWeb rootWeb = contentDb.Sites[0].RootWeb;
-
-
- SPList listjob = rootWeb.Lists.TryGetList("Tasks");
-
-
- from = rootWeb.Site.WebApplication.OutboundMailSenderAddress;
-
-
- smtpAddress = rootWeb.Site.WebApplication.OutboundMailServiceInstance.Server.Address;
-
-
- bool emailSent = SendMail(smtpAddress, subject, body, true, from, to, null, null);
-
- if (listjob != null && emailSent) {
- SPListItem newListItem = listjob.Items.Add();
- newListItem["Title"] = string.Concat("Email Notification Sent at : ", DateTime.Now.ToString());
- newListItem.Update();
- }
- });
-
- }
- public bool SendMail(string smtpAddress, string subject, string body, bool isBodyHtml, string from, string to, string cc, string bcc) {
- bool mailSent = false;
- SmtpClient smtpClient = null;
-
- try {
-
- smtpClient = new SmtpClient();
- smtpClient.Host = smtpAddress;
-
-
- MailMessage mailMessage = new MailMessage(from, to, subject, body);
- if (!String.IsNullOrEmpty(cc)) {
- MailAddress CCAddress = new MailAddress(cc);
- mailMessage.CC.Add(CCAddress);
- }
- if (!String.IsNullOrEmpty(bcc)) {
- MailAddress BCCAddress = new MailAddress(bcc);
- mailMessage.Bcc.Add(BCCAddress);
- }
- mailMessage.IsBodyHtml = isBodyHtml;
-
-
- smtpClient.Send(mailMessage);
- mailSent = true;
- } catch (Exception) {
- mailSent = false;
- }
-
- return mailSent;
- }
-
-
- }
- }
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.
- using System;
- using System.Runtime.InteropServices;
- using System.Security.Permissions;
- using Microsoft.SharePoint;
- using Microsoft.SharePoint.Security;
- using System.Linq;
-
- namespace CustomTimerJob.Features.NotificationTimerJobFeature {
-
- public class EventReceiver: SPFeatureReceiver {
-
- private
- const string List_JOB_NAME = "NotificationTimerJob";
-
-
-
- public override void FeatureActivated(SPFeatureReceiverProperties properties) {
- SPSecurity.RunWithElevatedPrivileges(delegate() {
- SPSite site = properties.Feature.Parent as SPSite;
-
-
- site.WebApplication.JobDefinitions.Where(t => t.Name.Equals(List_JOB_NAME)).ToList().ForEach(j => j.Delete());
-
-
- Timer listLoggerJob = new Timer(List_JOB_NAME, site.WebApplication);
- SPMinuteSchedule schedule = new SPMinuteSchedule();
- schedule.BeginSecond = 0;
- schedule.EndSecond = 59;
- schedule.Interval = 15;
- listLoggerJob.Schedule = schedule;
- listLoggerJob.Update();
- });
- }
-
-
-
-
- public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
- SPSecurity.RunWithElevatedPrivileges(delegate() {
- SPSite site = properties.Feature.Parent as SPSite;
-
-
- site.WebApplication.JobDefinitions.Where(t => t.Name.Equals(List_JOB_NAME)).ToList().ForEach(j => j.Delete());
- });
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- }
Then deploy the project.
After successful deployment go to your SharePoint central administrator.
Click on monitoring.
Then click
Review job definition.![monitoring]()
Then go to your
created timer job application.
It’s working successfully.