.NET Articles, Web Devploment, ASP.NET, HTML, Sharepoint 2010, Aamir Hasan
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here



Posted by Fahad Ahmad   on Friday, May 25, 2012 Total Views:  

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;



                    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)




                            _fileList  = lstSPFile;






                string smtpServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;

                string smtpFrom = SPAdministrationWebApplication.Local.OutboundMailSenderAddress;


                MailMessage mailMessage = new MailMessage(smtpFrom, "");

                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);




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. 

Protected by Copyscape Online Plagiarism Tool

Comments (3) -

Hasstedt United States
3/3/2017 4:12:58 PM #

Hey there! This can be my own very first comment right here and so i merely desired to supply a fast shout out along with say My spouse and i really take pleasure in reading the articles or blog posts. Could you propose another blogs/websites/forums that take care of the same subjects? Appreciate it!

Raahe United States
5/20/2017 3:09:13 AM #

My own husband or wife and also When i absolutely really like your blog and locate the majority of ones post’s being exactly what I’m in search of. can you deliver guests authors to publish information for you personally?

Hatto United States
1/9/2019 2:19:37 PM #

When i ended up being interested if people regarded as modifying the actual structure of your web site? The wonderfully published; I love what exactly youve have got to say. Nevertheless you might a little more in the way of content material therefore persons could possibly meet up with this much better. You’ve obtained for the better involving textual content with regard to solely possessing one or maybe 2 photos. You might space it available greater?

Kohnert United States
3/7/2021 5:40:11 AM #

Hey! I know this can be kind of away from topic however I’d realized I’d ask. Could you want to consider exchanging inbound links or maybe customer writing any writing or maybe vice-versa? My own weblog covers many of the exact same subjects as your own along with I believe we could drastically benefit from the other. If you happen to become interested really feel liberated to shoot us a good e-mail. We enjoy listening to coming from a person! Excellent weblog in addition!

Add comment

  Country flag

  • Comment
  • Preview

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5