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 {
/// <summary>
/// Default Consructor
/// </summary>
public NotificationTimerJob(): base() {}
/// <summary>
/// Parameterized Constructor
/// </summary>
/// <param name="jobName">Name of Job to display in central admin</param>
/// <param name="service">SharePoint Service </param>
/// <param name="server">Name of the server</param>
/// <param name="targetType">job type is for content db or job</param>
public NotificationTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType): base(jobName, service, server, targetType) {}
/// <summary>
/// Parameterized Constructor
/// </summary>
/// <param name="jobName"></param>
/// <param name="webApplication"></param>
public NotificationTimerJob(string jobName, SPWebApplication webApplication): base(jobName, webApplication, null, SPJobLockType.ContentDatabase) {
this.Title = "Email Notification Job";
}
/// <summary>
/// Execute method called when the timer job starts executing. It runs on "DB News Announcements" list which checks for all
/// approved annoucements and sends out an email to all users for all approved news.
/// </summary>
/// <param name="contentDbId"></param>
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 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();
}
});
}
/// <summary>
/// Send an email to indented users
/// </summary>
/// <param name="smtpAddress">SMTP address to sending email</param>
/// <param name="subject">subject of email</param>
/// <param name="body">body of email</param>
/// <param name="isBodyHtml">whether body is html or text</param>
/// <param name="from">from address</param>
/// <param name="to">to users(multiple email address can be seperated by comma(,) )</param>
/// <param name="cc">cc users</param>
/// <param name="bcc">bcc users</param>
/// <returns>Status whether email successfully sent or not</returns>
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;
}
}
}This code for Create Custom timer Job for Sending an E mail
