We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

This resource has not currently been approved, and is not currently linked to from our directory of resources. It is being displayed here for preview by the author and moderators only.
Rated
Read 1,282 times

Related Categories

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

S.S. Ahmed

Page1

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.Users[i].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 ...
 }
}

Do not forget to add Microsoft.System.Web.DLL in references. Add following code at the top of your page:

using Microsoft.System.Web;

You can also use third party email components. <a href="http://www.aspemail.com">aspemail.com</a> is an example. It is an excellent emailing component that you can use in your .NET applications including SharePoint's.

 

S.S. Ahmed is a senior IT Professional and works for a web and software development firm. Ahmed is a Microsoft Office SharePoint Server MVP. Ahmed specializes in creating database driven dynamic web sites. He has been working with SharePoint for the last 3-4 years. He develops customized SharePoint solutions. Ahmed likes to hop into other tools as well. Ahmed has used Project Server, InfoPath and BizTalk. Ahmed enjoys travelling and has been to many parts of the world. Web: www.walisystems.com Blog: www.sharepointblogs.com/ssa

Comments