in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

This Blog

Syndication

All About SharePoint - S.S. Ahmed - MVP Microsoft SharePoint

All About SharePoint, as the name suggests, is all about SharePoint. It has articles, tutorials, source code, FAQs, and tips about SharePoint, InfoPath, C#, Microsoft Office, SQL Server, XML, etc.

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

 

Published Sep 03 2006, 05:16 PM by ssa
Filed under: , ,

Comments

No Comments

Leave a Comment

(required )  
(optional )
(required )  
Add

About ssa

MOSS MVP - Over 8 years experience. 4 years SharePoint experience!

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts