Friday, 16 October 2015

Send mail in Sharepoint.

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace MultipleTasksSolution
{
    public class EmailUtility
    {
        public const String Gmail_SMTP_HostAddress = "smtp.gmail.com";
        public const int Gmail_SMTP_Port = 587;
        public const string Gmail_UserName = "*******@gmail.com"; // provide gmail user id
        public const string Gmail_ToAddress = "*******gmail.com";
        public const string Gmail_FromAddress = "*******@gmail.com";
        public const string Gmail_UserPassword = "********"; // provide your gmail password

       //Send mail by Gmail
        public static bool SendEmailUsingNet(string subject,string body)
        {
            MailMessage message = new MailMessage(Gmail_FromAddress, Gmail_ToAddress);
            message.Subject = subject;
            message.Body = body;
            message.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient();
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.EnableSsl = true;
            smtp.Host = Gmail_SMTP_HostAddress;
            smtp.Port = Gmail_SMTP_Port;

            NetworkCredential nc = new NetworkCredential(Gmail_UserName, Gmail_UserPassword);
            smtp.Credentials = nc;

            try
            {
                smtp.Send(message);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }


        //Send mail Using Sharepoint
        public static bool ProcessRequest(string receiver, string sender, string subject, string body,string cc,string siteurl)
        {
            bool IsMailSent = false;
            if (string.IsNullOrEmpty(siteurl))
                siteurl = SPContext.Current.Web.Url;

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite currentSite = new SPSite(siteurl))
                    {
                        using (SPWeb currentWeb = currentSite.RootWeb)
                        {

                            StringDictionary headers = new StringDictionary();
                            headers.Add("to", receiver);
                            headers.Add("from", sender);
                            headers.Add("subject", subject);
                            headers.Add("content-type", "text/html");
                            headers.Add("cc", cc);
                            string bodyText = body;
                            SmtpClient smtp = new SmtpClient();
                            smtp.Host = "************";
                            IsMailSent = SPUtility.SendEmail(currentWeb, headers, bodyText);

                        }
                    }
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return IsMailSent;
        }

    }
}

Refer Link

No comments:

Post a Comment