Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Successfully deployed the "Sharepoint Timer job".but i am not getting E-Mail Notification.

$
0
0

i referred this website to create the sharepoint custom timer job,its getting deployed.but i am not getting any email notification.

http://www.c-sharpcorner.com/UploadFile/1ce8ad/create-custom-timer-job-in-sharepoint-2013-for-send-email/

My class 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 NotificationCustomTimerJob
{
    public class NotificationTimerJob : Microsoft.SharePoint.Administration.SPJobDefinition
    {
           public NotificationTimerJob()
            : base()
        {
        }


        public NotificationTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
            : base(jobName, service, server, targetType)
        {
        }

        public NotificationTimerJob(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 = "yangyeswar20@gmail.com";
            string subject = "Email Notification";
            string body = "<h1> Email Sending from Email Notification Job </h1>";

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                // get a reference to the current site collection's content database 
                SPWebApplication webApplication = this.Parent as SPWebApplication;
                SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];

                // get a reference to the "Tasks" list in the RootWeb of the first site collection in the content database 
                SPWeb rootWeb = contentDb.Sites[0].RootWeb;

                // Get the DB News Announcements List 
                SPList listjob = rootWeb.Lists.TryGetList("Tasks");

                // Get sender address from web application settings 
                from = rootWeb.Site.WebApplication.OutboundMailSenderAddress;

                // Get SMTP address from web application settings 
                smtpAddress = rootWeb.Site.WebApplication.OutboundMailServiceInstance.Server.Address;

                // Send an email if the news is approved 
                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
            {
                // Assign SMTP address 
                smtpClient = new SmtpClient();  
                smtpClient.Host = smtpAddress;

                //Create an email message 
                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;

                // Send the email 
                smtpClient.Send(mailMessage);
                mailSent = true;
            }
            catch (Exception)
            {
                mailSent = false;
            }

            return mailSent;
        }


    }
}

My Event handler code

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint; // 
using Microsoft.SharePoint.Security;
using System.Linq;

namespace NotificationCustomTimerJob.Features.NotificationTimerJobFeature
{
    /// <summary> 
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade. 
    /// </summary> 
    /// <remarks> 
    /// The GUID attached to this class may be used during packaging and should not be modified. 
    /// </remarks> 

    [Guid("1ed364be-db4d-4620-998e-c2b0c0a438e1")]
    //[Guid("67388410-1290-41d2-8645-5a1203b27bc4")]
    public class NotificationTimerJobFeatureEventReceiver : SPFeatureReceiver
    {
        // Name of the Timer Job, but not the Title which is displayed in central admin 
        private const string List_JOB_NAME = "NotificationTimerJob";

        // Uncomment the method below to handle the event raised after a feature has been activated. 

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPSite site = properties.Feature.Parent as SPSite;

                // make sure the job isn't already registered 
                site.WebApplication.JobDefinitions.Where(t => t.Name.Equals(List_JOB_NAME)).ToList().ForEach(j => j.Delete());

                // install the job 
                NotificationTimerJob listLoggerJob = new NotificationTimerJob(List_JOB_NAME, site.WebApplication);
                SPMinuteSchedule schedule = new SPMinuteSchedule();
                schedule.BeginSecond = 0;
                schedule.EndSecond = 59;
                schedule.Interval = 1;
                listLoggerJob.Schedule = schedule;
                listLoggerJob.Update();
            });
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated. 

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPSite site = properties.Feature.Parent as SPSite;

                // delete the job 
                site.WebApplication.JobDefinitions.Where(t => t.Name.Equals(List_JOB_NAME)).ToList().ForEach(j => j.Delete());
            });
        }


        // Uncomment the method below to handle the event raised after a feature has been installed. 

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties) 
        //{ 
        //} 


        // Uncomment the method below to handle the event raised before a feature is uninstalled. 

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties) 
        //{ 
        //} 

        // Uncomment the method below to handle the event raised when a feature is upgrading. 

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters) 
        //{ 
        //} 
    }
}


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>