Send Email with multiple attachments in SharePoint 2010


I have a custom list named "Testing". I have an item which has multiple attachments. I need to send all those attachments in an email.


item.png 

Steps Involved:

  1. Open Visual Studio 2010 by going Start | All Programs | Microsoft Visual Studio 2010 | Right click on Microsoft Visual Studio 2010 and click on Run as administrator.
  2. Go to File tab, click on New and then click on Project.
  3. In the New Project dialog box, expand the Visual C# node, and then select the Windows node.
  4. In the Templates pane, select Console Application.
  5.  Enter the Name as SendEmail and then click OK.
  6.  In the solution explorer, right click on the solution and then click on Properties.
  7. Select the Application tab; check whether “.Net Framework 3.5” is selected for Target Framework.
  8. Select the Build tab; check whether “Any CPU” is selected for Platform Target.
  9. Add the following references

a)      Microsoft.SharePoint    

  1. Add the following namespaces

a)      using Microsoft.SharePoint; 

b)      using System.Net.Mail;

  1. Replace Program.cs with the following code

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net.Mail;

using System.IO;

using Microsoft.SharePoint;

 

namespace SendEmail

{

    class Program

    {

        private static MailMessage mailInformation(SPListItem listItem, SPWeb spWeb,string from, string to, string subject, string body)

        {

            MailMessage mail = new MailMessage();

            mail.From = new MailAddress(from);

            mail.To.Add(new MailAddress(to));

            mail.Subject = subject;

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

        }

 

        static void Main(string[] args)

        {

            string eMailFrom = "[email protected]";

            string eMailTo = "[email protected]";

            string eMailSubject = "Mail with multiple attachments";

            string eMailBody = "Mail with multiple attachments from SharePoint list item";

            string hostAddress = string.Empty;

            using (SPSite site = new SPSite("https://serverName"))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    SPList list = web.Lists.TryGetList("Testing");

                    if (list != null)

                    {

                        SPListItem item = list.GetItemById(61);

                        MailMessage mailMessage=mailInformation(item, web, eMailFrom, eMailTo, eMailSubject, eMailBody);

                        hostAddress = site.WebApplication.OutboundMailServiceInstance.Server.Address;

                        sendEmail(mailMessage, hostAddress);

                    }

                }

            }

        }

    }

}


 

Output:

mail.jpg
 


Ebook Download
View all
Learn
View all