Required Webpart To Send Email
Required Webpart To Send Email
SharePoint
By Brian Pursley | 8 Dec 2008
An article that introduces SharePoint web part development by creating a simple contact form
web part.
Top of Form
/w EPDw UKMTAy
Requirements
The web part created in this article will use Windows SharePoint Services (WSS) 3.0. It will also
work with MOSS 2007, which is a more advanced version of SharePoint built on the same
infrastructure as WSS 3.0. To create the web part, we will be using Visual Studio 2008 installed
on Windows Server 2003, along with Windows SharePoint Services 3.0 Tools: Visual Studio
2008 Extensions, Version 1.2, which is a free download from the Microsoft Download Center.
Creating a Web Part Project
The first step is to create a new web part project using Visual Studio 2008. To do this, open
Visual Studio 2008, and choose New – Project from the file menu. Select Visual C# - SharePoint
from the Project Type list, and select the Web Part template from the list on the right. Enter
ContactFormWebPart as the name and the solution name, and choose a directory where you
want the solution files to be saved. When you click OK, an empty web part project will be
created and opened in Visual Studio.
The empty project has one web part, called WebPart1, but this isn’t what we want to call our
web part, so the first step is to delete WebPart1 from the project by right clicking on the
WebPart1 folder in the Solution Explorer in Visual Studio and choosing Delete.
Next, let's add a new web part to the project, called ContactForm. Right click on the
ContactFormWebPart project in the Solution Explorer, and choose Add – New Item from the
context menu. Select SharePoint from the categories list, and Web Part from the templates list.
Enter ContactForm for the name, and click Add.
This gives you a new source file called ContactForm.cs that has an empty class inheriting from
WebPart, with some TODO comments. In the next section, we are going to replace the
CreateChildControls() function and add some additional code to this class.
Adding Code to Create the Controls
Now, we are ready to begin writing code that will display the web part. Open ContactForm.cs,
and you will see a function called CreateChildControls(). This function is where we will add
labels, textboxes, and a button to allow the user to interact with our web part.
But first, let's create some class-level variables for the controls that we will create. Declaring
them at the class level as opposed to within the CreateChildControls() function will allow us
to reference these controls from the button event handler later on.
Collapse
TextBox txtContactName;
TextBox txtEmailAddress;
TextBox txtPhone;
TextBox txtMessage;
Button btnSendMessage;
Label lblMessageSent;
Now, let's add the code to CreateChildControls to build the display. In order to keep it simple,
an HTML table will be used to align the controls in a consistent manner. Each table row will
have two cells: one for the field label, and the other for the text boxes. Controls are created one
at a time and added to a table cell, which is then added to a table row.
Collapse
protected override void CreateChildControls()
{
base.CreateChildControls();
Table t;
TableRow tr;
TableCell tc;
t.Controls.Add(tr);
t.Controls.Add(tr);
t.Controls.Add(tr);
t.Controls.Add(tr);
// Message label
tr = new TableRow();
tc = new TableCell();
tc.Style["padding-top"] = "7px";
tc.VerticalAlign = VerticalAlign.Top;
Label lblMessage = new Label();
lblMessage.Text = "Message:";
tc.Controls.Add(lblMessage);
tr.Controls.Add(tc);
// Message textbox
tc = new TableCell();
tc.VerticalAlign = VerticalAlign.Top;
txtMessage = new TextBox();
txtMessage.ID = "txtMessage";
txtMessage.Height = Unit.Pixel(100);
txtMessage.Width = Unit.Pixel(400);
txtMessage.TextMode = TextBoxMode.MultiLine;
txtMessage.Wrap = true;
tc.Controls.Add(txtMessage);
tr.Controls.Add(tc);
t.Controls.Add(tr);
t.Controls.Add(tr);
t.Controls.Add(tr);
this.Controls.Add(t);
}
Finally, we need to add an event handler to send the message as an email when the Send
Message button is clicked by the user. In the code above, the event handler was already wired up
with the line btnSendMessage.Click += new EventHandler btnSendMessage_Click);, so
now we just need to create the btnSendMessage_Click function. This function simply builds an
email message using text that was entered into the text boxes, and sends the email using
SharePoint's SPUtility.SendEmail() function.
You will need to change the "to" and "from" fields in the message header for your specific
situation. The "to" field specifies where the email will be sent, and the "from" field is the email
address that the email should appear to be from. The "from" field isn't too important, but some
mail servers may require this to be a valid email address.
Collapse
protected void btnSendMessage_Click(object sender, EventArgs e)
{
// Build the email subject string
System.Text.StringBuilder subject = new System.Text.StringBuilder();
subject.Append("Contact Form Message from ");
subject.Append(txtContactName.Text);
Collapse
C:\>stsadm -o addsolution -filename ContactFormWebPart.wsp