Friday, January 29, 2016

Send Email with multiple attachments in SharePoint List

There is no direct implementation for this functionality and can't send attachments in SharePoint Designer Workflow so I have implemented the Custom Event Receiver for SharePoint List and added this code under "ItemAdded"

1) Create SharePoint Custom List (example: EmailNotificationLog)
1) Create Farm level project and select Event Receiver Project in VS 2010/2012 and name it (I used the name to "SendEmailwithAttachment")

2) Open "Elements.xml" file and change Receivers tag with below
 <Receivers ListUrl="Lists/EmailNotificationLog" Scope ="Web">

3) Add Microsoft.SharePoint reference to project
4) Open .cs file and add the below code under ItemAdded

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Net.Mail;
namespace SendEmailwithAttachment.SendEmailwithAttachmentEventReceiver
{
public class SendEmailwithAttachmentEventReceiver : SPItemEventReceiver
    {
        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            string hostAddress = string.Empty;
                using(SPSite site=new SPSite(properties.WebUrl))
                {
                   using(SPWeb web = site.OpenWeb())
                   {
                       string eMailFrom = properties.AfterProperties["From"].ToString();
                       string eMailTo = properties.AfterProperties["To"].ToString();
                       string eMailCc = properties.AfterProperties["Cc"].ToString();
                       string eMailBcc = properties.AfterProperties["Bcc"].ToString();
                       string eMailSubject = properties.AfterProperties["Subject"].ToString();
                       string eMailBody = properties.AfterProperties["Body"].ToString();
                       SPList oList = web.Lists["EmailNotificationLog"];
                       SPListItem item = oList.GetItemById(properties.ListItemId);
                       MailMessage mailMessage = mailInformation(item, web, eMailFrom, eMailTo,eMailCc,eMailBcc, eMailSubject, eMailBody);
                       hostAddress = site.WebApplication.OutboundMailServiceInstance.Server.Address;
                       sendEmail(mailMessage, hostAddress);
                   }
                }
         
        }
        private static MailMessage mailInformation(SPListItem listItem, SPWeb spWeb, string from, string to,string Cc,string Bcc, string subject, string body)
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(from);
             string[] ToMuliId = to.Split(';');
             foreach (string ToEMailId in ToMuliId)
             {
                 mail.To.Add(new MailAddress(ToEMailId));
             }
         
            if (Cc.ToString() != "")
            {
                 string[] CCId = Cc.Split(';');

                foreach (string CCEmail in CCId)
                {
                    mail.CC.Add(new MailAddress(CCEmail)); //Adding Multiple CC email Id
                }
             
               // mail.CC.Add(new MailAddress(Cc));
            }
            if (Bcc.ToString() != "")
            {
                string[] bccid = Bcc.Split(';');

                foreach (string bccEmailId in bccid)
                {
                    mail.Bcc.Add(new MailAddress(bccEmailId)); //Adding Multiple BCC email Id
                 
                }
                //mail.Bcc.Add(new MailAddress(Bcc));
            }
            mail.Subject = subject;
            mail.IsBodyHtml = true;
            mail.Body = body;
            for (int attachment = 0; attachment < listItem.Attachments.Count; attachment++)
            {
                string fileURL = listItem.Attachments.UrlPrefix + listItem.Attachments[attachment];
                SPFile file = spWeb.GetFile(fileURL);
                mail.Attachments.Add(new Attachment(file.OpenBinaryStream(), file.Name));
            }
            return mail;
        }
        private static void sendEmail(MailMessage eMail, string host)
        {
            SmtpClient smtp = new SmtpClient(host);
            smtp.UseDefaultCredentials = true;
         
            smtp.Send(eMail);
        }

    }
}



5) Save it and deploy the .wsp file and activate the feature in site level