A Practical Guide to SharePoint 2013

A Practical Guide to SharePoint 2013
A Practical Guide to SharePoint 2013 - Book by Saifullah Shafiq

Sunday, September 3, 2006

Sending an email to all members of the site using .NET code

Category: Development
Level: Beginner 
You can send an email to all members of your SharePoint site using the following code:

SPSite mySite;
SPWeb currentWeb;
mySite = new SPSite(Url); //Url contains site URL
currentWeb = mySite.OpenWeb();
string sMemberName = "";
string strSubject = "";
string strBody = "";
for (int i=0;i<currentWeb.Users.Count;i++)
{
 sMemberName = currentWeb.UsersIdea.LoginName.ToString();
 strSubject = "Testing";
 strBody = "This is a test.";
 SendMail(currentWeb,currentWeb.Users[sMemberName].ToString() , strSubject, strBody);
}
//SendMail Function
public void SendMail(SPWeb currentWeb, string _user,string strSubject, string strBody)
{
 
 MailMessage msg;
 try
 {
  SmtpMail.SmtpServer = "smtp.yourserver.com";
  msg = new MailMessage();
  msg.BodyFormat = MailFormat.Html;
  msg.From =  currentWeb.Author.Email;
  msg.Subject = strSubject;
  msg.Body = strBody;
  msg.To = currentWeb.Users[_user].Email;
  SmtpMail.Send(msg);
    
 }
 catch (Exception ex)
 {
  //Exception Handling Code ...
 }
}

--SSA

No comments:

Post a Comment