How to Send an EMail From WebPart in MOSS 2007

Hi Devs,
The following is the piece of code to send Email from webpart.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint;
using System.Data;
using System.Collections;
namespace SendMail
{
public class SendMail : Microsoft.SharePoint.WebPartPages.WebPart
{
private TextBox txtTo;
private Button btnSendMail;
protected override void CreateChildControls()
{
txtTo = new TextBox();
this.Controls.Add(txtTo);
btnSendMail = new Button();
btnSendMail.Text = “Send Email”;
btnSendMail.Click += new EventHandler(SendMail_Click);
this.Controls.Add(btnSendMail);
}
void SendMail_Click(object sender, EventArgs e)
{
SPSite _site = new SPSite(HttpContext.Current.Request.Url.ToString());
SPWeb _web = _site.OpenWeb();
_web.AllowUnsafeUpdates = true;
SPUtility.SendEmail(_web, false, false, txtTo.Text, “Test Email”, “Text Body Message”);
_web.Dispose();
_site.Dispose();
}
}
}

Create the WebPart and register it as SafeControl

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>