Dotnetplace.com

.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

Advertise

Loading...


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;

                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. 

Protected by Copyscape Online Plagiarism Tool

Comments (2) -

Hasstedt
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
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?

mainostoimisto
mainostoimisto United States
5/26/2017 12:17:26 AM #

As well as, I’m only usually surprised concerning the remarkable factors served simply by an individual.

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5