Search This Blog

Friday, January 25, 2013

How to send the attachment(s) from a SharePoint List through email

Recently one of my client requested that they want an alert email every time an item is created or modified. Moreover if there is any attachment in that list, they want that attachment to be the part of email alert. The first part of the request is pretty simple. A simple alert can do the job but sending email is where it gets tricky.
To tackle this, I knew I had to create a workflow but the simple “SPUtility.SendEmail” doesn’t allow you to send emails with attachment so this is how I tackled it.

            List<SPFile> _fileList = null;
            SPWeb myweb = SPContext.Current.Web;
            SPSite mysite = SPContext.Current.Site;
            SPList mylist = SPContext.Current.List;
            SPListItem myitem = SPContext.Current.ListItem;
            SPAttachmentCollection myattach = myitem.Attachments;
            if (myattach.Count != 0)
            {
                myweb.AllowUnsafeUpdates = false;
 
                List<SPFile> lstSPFile = null;
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    using (SPSite oSite = new SPSite(myitem.ParentList.ParentWeb.Site.ID))
                    {
                        using (SPWeb oWeb = oSite.OpenWeb(myitem.ParentList.ParentWeb.ID))
                        {
                          SPFolder folder = myitem.ParentList.RootFolder.SubFolders["Attachments"].SubFolders[myitem.ID.ToString()];
                            lstSPFile = new List<SPFile>();
                            foreach (SPFile file in folder.Files)
                            {
                                lstSPFile.Add(file);
                            }
                            _fileList  = lstSPFile;

                        }
                    }
                });
               
                string smtpServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;
                string smtpFrom = SPAdministrationWebApplication.Local.OutboundMailSenderAddress;
  
                MailMessage mailMessage = new MailMessage(smtpFrom, "email@email.com");
                mailMessage.Body = "body body body body body body body body body ";
                mailMessage.Subject = "subject subject subject";

                foreach(var file in _fileList){
                    WebClient webClient = new WebClient();

                    //Supply the WebClient with the network credentials of our user
                    webClient.Credentials = CredentialCache.DefaultNetworkCredentials;
                    string mypath = "http://server/" + file;
                    //Download the byte array of the file
                    byte[] data = webClient.DownloadData(mypath);

                    MemoryStream memoryStreamOfFile = new MemoryStream(data);
                    mailMessage.Attachments.Add(new System.Net.Mail.Attachment(memoryStreamOfFile, file.Name.ToString()));
                }

                //Create the SMTP client object and send the message
                SmtpClient smtpClient = new SmtpClient(smtpServer);
                smtpClient.Send(mailMessage);
            }

Well I don’t know if I have to explain the code line by line as it self-explanatory. With the help of this, no matter how many attachments are there in the List Item, it will send out the email. Please do leave a comment if you need more explanation. 

3 comments:

  1. This is very good and informational posting, It’s really helpful. Thanks for sharing it with us. Keep it on…

    Sharepoint Developer

    ReplyDelete
  2. This is very good and informational posting, It’s really helpful. Thanks for sharing it with us. Keep it on…

    Sharepoint Developer

    ReplyDelete