Namespaces to add:
using System.Text;
using Microsoft.SharePoint.Administration;
using System.Data;
using System.IO;
using Microsoft.SharePoint;
using System.Net.Mail;
using System.Web.Configuration;
using System.Net;
using System.Collections.Specialized;
using System.Collections;
using Microsoft.SharePoint.Utilities;
namespace Sample
{
class RemainderMail : SPJobDefinition
{
string siteURL = "ur sit url";
public RemainderMail()
: base()
{
}
public RemainderMail(string jobName, SPService service, SPServer server, SPJobLockType targetType)
: base(jobName, service, server, targetType)
{
}
public RemainderMail(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
{
this.Title = "Sending Remainder Mail TimerJob";
}
public override void Execute(Guid contentDbId)
{
SendingRemainderMails();
}
public void SendingRemainderMails()
{
using (SPSite oSite = new SPSite(siteURL))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
try
{
oWeb.AllowUnsafeUpdates = true;
SPList oNotelist = oWeb.Lists["Noticelist"];
SPListItemCollection Notelisttemcoll = oRegNotelist.Items;
foreach (SPListItem item in regNotelisttemcoll)
{
string strMail = null;
if (Convert.ToString(item["NoticeStatus"]) == "Pending")
{
string noticeid = Convert.ToString(item["Title"]);
SPGroup grp = oWeb.SiteGroups["Staff List"];
foreach (SPUser user in grp.Users)
{
strMail = user.Email;
sendEmail(strMail, noticeid);
}
}
}
}
catch (Exception ex)
{
ExceptionLog(ex);
}
}
}
}
protected void sendEmail(string strMail, string noticeid)
{
try
{
SmtpClient client = new SmtpClient();
client.Host = "server name";
NetworkCredential userCrendential = new NetworkCredential("username", "pwd");
client.Credentials = userCrendential;
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
string strBody = "";
mail.From = new MailAddress("abc@abc.com", "Title");
mail.Bcc.Add("abc@abc.com");
mail.To.Add(strMail);
mail.Subject = "--subject--";
mail.Body = "Dear Sir/Madam," + "\r\n\n" + strBody + "\r\n" + siteURL + "SitePages/notice.aspx?NoticeID=" + noticeid + "\r\n\n\n\n" + "Thanks," + "\r\n" + "Team.";
client.Send(mail);
saveEmail("Sent", strMail,noticeid);
}
catch (Exception ex)
{
using (SPSite oSite = new SPSite(siteURL))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
saveEmail("Failed", strMail, noticeid);
ExceptionLog(ex);
}
}
}
}
public void saveEmail(string emailStatus, string strMail, string noticeid)
{
using (SPSite oSite = new SPSite(siteURL))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
try
{
oWeb.AllowUnsafeUpdates = true;
SPUser oUser = oWeb.CurrentUser;
SPList lstEmail = oWeb.Lists["EMailLog"];
SPListItem itemadd = lstEmail.Items.Add();
SPFieldCollection fldColl = lstEmail.Fields;
StringDictionary headers = new StringDictionary();
string strBody = "";
headers.Add("to", strMail);
headers.Add("from", "ur email ");
headers.Add("subject", "title");
headers.Add("content-type", "text/html");
string bodyText = "Dear Sir/Madam," + "\r\n\n" + strBody + "\r\n" + siteURL + "SitePages/Notice.aspx?NoticeID=" + noticeid + "\r\n\n\n\n" + "Thanks," + "\r\n" + Team.";
itemadd["Body"] = bodyText;
foreach (DictionaryEntry entry in headers)
{
switch (entry.Key.ToString())
{
case "to":
itemadd["MailTo"] = entry.Value.ToString();
break;
case "subject":
itemadd["Subject"] = entry.Value.ToString();
break;
case "content-type":
itemadd["MessageType"] = entry.Value.ToString();
break;
case "from":
itemadd["Title"] = entry.Value.ToString();
break;
}
}
itemadd["DateTime"] = DateTime.Now.ToShortDateString();
itemadd["Status"] = emailStatus;
itemadd.Update();
oWeb.AllowUnsafeUpdates = false;
}
catch (Exception ex)
{
ExceptionLog(ex);
}
}
}
}
using System.Text;
using Microsoft.SharePoint.Administration;
using System.Data;
using System.IO;
using Microsoft.SharePoint;
using System.Net.Mail;
using System.Web.Configuration;
using System.Net;
using System.Collections.Specialized;
using System.Collections;
using Microsoft.SharePoint.Utilities;
namespace Sample
{
class RemainderMail : SPJobDefinition
{
string siteURL = "ur sit url";
public RemainderMail()
: base()
{
}
public RemainderMail(string jobName, SPService service, SPServer server, SPJobLockType targetType)
: base(jobName, service, server, targetType)
{
}
public RemainderMail(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
{
this.Title = "Sending Remainder Mail TimerJob";
}
public override void Execute(Guid contentDbId)
{
SendingRemainderMails();
}
public void SendingRemainderMails()
{
using (SPSite oSite = new SPSite(siteURL))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
try
{
oWeb.AllowUnsafeUpdates = true;
SPList oNotelist = oWeb.Lists["Noticelist"];
SPListItemCollection Notelisttemcoll = oRegNotelist.Items;
foreach (SPListItem item in regNotelisttemcoll)
{
string strMail = null;
if (Convert.ToString(item["NoticeStatus"]) == "Pending")
{
string noticeid = Convert.ToString(item["Title"]);
SPGroup grp = oWeb.SiteGroups["Staff List"];
foreach (SPUser user in grp.Users)
{
strMail = user.Email;
sendEmail(strMail, noticeid);
}
}
}
}
catch (Exception ex)
{
ExceptionLog(ex);
}
}
}
}
protected void sendEmail(string strMail, string noticeid)
{
try
{
SmtpClient client = new SmtpClient();
client.Host = "server name";
NetworkCredential userCrendential = new NetworkCredential("username", "pwd");
client.Credentials = userCrendential;
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
string strBody = "";
mail.From = new MailAddress("abc@abc.com", "Title");
mail.Bcc.Add("abc@abc.com");
mail.To.Add(strMail);
mail.Subject = "--subject--";
mail.Body = "Dear Sir/Madam," + "\r\n\n" + strBody + "\r\n" + siteURL + "SitePages/notice.aspx?NoticeID=" + noticeid + "\r\n\n\n\n" + "Thanks," + "\r\n" + "Team.";
client.Send(mail);
saveEmail("Sent", strMail,noticeid);
}
catch (Exception ex)
{
using (SPSite oSite = new SPSite(siteURL))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
saveEmail("Failed", strMail, noticeid);
ExceptionLog(ex);
}
}
}
}
public void saveEmail(string emailStatus, string strMail, string noticeid)
{
using (SPSite oSite = new SPSite(siteURL))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
try
{
oWeb.AllowUnsafeUpdates = true;
SPUser oUser = oWeb.CurrentUser;
SPList lstEmail = oWeb.Lists["EMailLog"];
SPListItem itemadd = lstEmail.Items.Add();
SPFieldCollection fldColl = lstEmail.Fields;
StringDictionary headers = new StringDictionary();
string strBody = "";
headers.Add("to", strMail);
headers.Add("from", "ur email ");
headers.Add("subject", "title");
headers.Add("content-type", "text/html");
string bodyText = "Dear Sir/Madam," + "\r\n\n" + strBody + "\r\n" + siteURL + "SitePages/Notice.aspx?NoticeID=" + noticeid + "\r\n\n\n\n" + "Thanks," + "\r\n" + Team.";
itemadd["Body"] = bodyText;
foreach (DictionaryEntry entry in headers)
{
switch (entry.Key.ToString())
{
case "to":
itemadd["MailTo"] = entry.Value.ToString();
break;
case "subject":
itemadd["Subject"] = entry.Value.ToString();
break;
case "content-type":
itemadd["MessageType"] = entry.Value.ToString();
break;
case "from":
itemadd["Title"] = entry.Value.ToString();
break;
}
}
itemadd["DateTime"] = DateTime.Now.ToShortDateString();
itemadd["Status"] = emailStatus;
itemadd.Update();
oWeb.AllowUnsafeUpdates = false;
}
catch (Exception ex)
{
ExceptionLog(ex);
}
}
}
}