Showing posts with label web parts. Show all posts
Showing posts with label web parts. Show all posts

Monday, May 20, 2013

Sending an Email using Sharepoint Web Part


For sending a mail using SPUtility I referred to this article. This code is just a demo.

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Collections;
using Microsoft.SharePoint.Utilities;

namespace CustomPeoplePickerOne.PeoplePicker_swp_1
{
    [ToolboxItemAttribute(false)]
    public class PeoplePicker_swp_1 : WebPart
    {
        PeopleEditor pe = null;
        SPWeb web = null;
        protected override void CreateChildControls()
        {
            web = SPContext.Current.Web;

            pe = new PeopleEditor();
            pe.ValidatorEnabled = true;
            this.Controls.Add(pe);

            Button viewPeople = new Button();
            viewPeople.Text = "View";
            viewPeople.Click += new EventHandler(viewPeople_Click);
            this.Controls.Add(viewPeople);

            Button sendMailButton = new Button();
            sendMailButton.Text = "Send Mail";
            sendMailButton.Click += new EventHandler(sendMailButton_Click);
            this.Controls.Add(sendMailButton);
               
        }

        void sendMailButton_Click(object sender, EventArgs e)
        {

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                if (SendMail("kinathr@navantis.com"))
                {
                    this.Controls.Add(new LiteralControl("<br/>"));
                    this.Controls.Add(new LiteralControl("<b>Email Successfully sent to Kinath</b>"));
                }
                else
                {
                    this.Controls.Add(new LiteralControl("<br/>"));
                    this.Controls.Add(new LiteralControl("<b>Sending Failed to Kinath</b>"));
                }
             
            });


        }

        void viewPeople_Click(object sender, EventArgs e)
        {
            viewPeopleOption4();
        }

        void viewPeopleOption()
        {
            foreach (var loginName in pe.CommaSeparatedAccounts.Split(';', ','))
            {
                SPUser user = SPContext.Current.Web.EnsureUser(loginName);
                TextBox tb = new TextBox();
                tb.Text = string.Format("Name : {0} Email : {1}", user.Name, user.Email);
                this.Controls.Add(tb);

                //SPUtility.SendEmail(
            }
        }

        void viewPeopleOption2()
        {
            //Create the data table and add data to it
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn(" Name ", typeof(String)));
            dt.Columns.Add(new DataColumn(" Email ", typeof(String)));

            //get data from the people editor
            for (int i = 0; i < pe.ResolvedEntities.Count; i++)
            {
                string accName = "";
                string email = "";
                PickerEntity pickerEntity = (PickerEntity)pe.ResolvedEntities[i];
                Hashtable hsEntityData = pickerEntity.EntityData;
                if (hsEntityData.ContainsKey("Account Name"))
                {
                    accName = Convert.ToString(hsEntityData["Account Name"]);
                }
                else
                {
                    accName = "Not available";
                }
                if (hsEntityData.ContainsKey("Email"))
                {
                    email = Convert.ToString(hsEntityData["Email"]);
                }
                else
                {
                    email = "Not available";
                }


                TextBox tb = new TextBox();
                tb.Text = string.Format("Name : {0} Email : {1}", accName, email);
                this.Controls.Add(tb);

            }
        }

        void viewPeopleOption3()
        {
            var accountName = pe.Accounts[0];
            SPUser user = web.EnsureUser((string)accountName);

            Label userLable = new Label();
            userLable.Text = string.Format("User : {0} Email : {1}",user.Name,user.Email);
            this.Controls.Add(userLable);

        }

        void viewPeopleOption4()
        {
            if (pe.IsValid)
            {
                for (int i = 0; i < pe.ResolvedEntities.Count; i++)
                {
                    PickerEntity picker = (PickerEntity)pe.ResolvedEntities[i];
                    Hashtable hstEntityData = picker.EntityData;
                    string[] name = picker.DisplayText.Split(' ');
                    string Email = Convert.ToString(hstEntityData["Email"]);
                    //string FName = name[0].ToString();
                    //string SName = (name[1].ToString() != null) ? name[1].ToString() : "";
                    Label userLable = new Label();
                    userLable.Text = Email;
                    this.Controls.Add(userLable);

                }
            }
        }

        bool SendMail(string userMail)
        {
            bool success = false;
            /*
             * public static bool SendEmail(
                       SPWeb web,
                       bool fAppendHtmlTag,
                       bool fHtmlEncode,
                       string to,
                       string subject,
                       string htmlBody,
                       bool appendFooter
                        )
             * */

            SPWeb web = SPContext.Current.Web;
            bool fAppendHtmlTag = false;
            bool fHtmlEncode = false;
            string toEmail = userMail;
            string subject = "Test Email";
            string htmlBody = "Test Email Body";
            bool appendFooter = false;

            //first check whehter the SMTP settings are established
            if (SPUtility.IsEmailServerSet(web))
            {
                success = SPUtility.SendEmail(web, fAppendHtmlTag, fHtmlEncode, toEmail, subject, htmlBody, appendFooter);
            }
            else
            {
                this.Controls.Add(new LiteralControl("<br/>"));
                this.Controls.Add(new LiteralControl("<b>SMTP settings not set</b>"));
            }
            return success;
        }
    }
}