You would think taking attachments from a Sharepoint list and adding them to a mail message would be easy. Well, not that easy. They make you jump through hoops and stream the attachment with the file name. Would have been nice to just take an attachment off a list and then add it to an email using the same attachment object. Oh well. Here’s the code I use to take an attachment off a list item, look at the file name, then attach it to an email using System.Mail.
MailMessage mail = new MailMessage();
mail.CC.Add(wfConfig.EmailCC);
mail.From = new MailAddress(wfConfig.EmailFrom);
mail.Body =”body”:
mail.IsBodyHtml = true;
if (workflowProperties.Item.Attachments.Count > 0)
{
foreach (string fileName in workflowProperties.Item.Attachments)
{
SPFile file = workflowProperties.Item.ParentList.ParentWeb.GetFile(
workflowProperties.Item.Attachments.UrlPrefix + fileName);
if (file.Name.ToUpper().Contains(DocNameToCheck))
{
Attachment attachment = new Attachment(file.OpenBinaryStream(), fileName, string.Empty);
mail.Attachments.Add(attachment);
smtp.Send(mail);
}//if
}//foreach
foreach (string fileName inworkflowProperties.Item.Attachments)
Wow – that looks like exactly what I’m looking for, but I’m not totally sure how you implemented it.
Is this some sort of code in you injected somewhere in the back end of sharepoint?
Is it a client object model in a customer work flow?
Is it 2007/2010?
Sorry for all the questions, I recognize bits and pieces of what you’re doing but I’m not sure where to thread them in.
Thanks!
Hi,
This is server side code that is compiled using Visual Studio. It uses the SharePoint object model to access the attachments. In my case, I created a Seuential workflow project in Visual Studio 2008 and accessed the attachments. In 2010 you can use the Client OM to do something similar. Good luck.
Thank you.good post
Reblogged this on KamilAmirul's – Ideas and Solutions and commented:
My current task on handling attachments in SharePoint. Seems hard enough for a noob like me