从sharepoint列表项中检索附件并附加到电子邮件 [英] Retrieve attachments from sharepoint list item and attach to email
问题描述
我需要提取sharepoint列表项中的所有附件,并将它们附加到我目前设置的电子邮件中。
以下是要抓取的代码附件 - 好吧我得到附件名称文件的共享点列表,但不知道如何获得实际文件,所以我可以附加到电子邮件。
以下是我的内容到目前为止,我们已经获取了列表项附件。
clientContext.Load(targetListItem,item => item [" Title"]);
clientContext.Load(附件);
clientContext.ExecuteQuery();
if(targetListItem.AttachmentFiles.Count> 0)
{
foreach(targetListItem.AttachmentFiles中的var附件)
{
path = attachment.ServerRelativeUrl; //获取文件的名称,在最后一次斜杠后搜索值的值
pos = path.LastIndexOf(" /")+ 1;
filename = path.Substring(pos,path.Length - pos);
lblSupportingDocuments.Text + ="< a href ='" + ConfigurationManager.AppSettings [" BaseURL"]。ToString()+ attachment.ServerRelativeUrl +"'target ='_ blank'>" + filename +"< / a>< br />" ;;
}
}
以下是我发送电子邮件和附加文件的代码,但不确定如何将上面的附件作为电子邮件附件的一部分放入。
SmtpClient服务器=新的SmtpClient(系统。 Configuration.ConfigurationManager.AppSettings [" emailserver的"]的ToString());
MailMessage email = new MailMessage();
email.From = new MailAddress(System.Configuration.ConfigurationManager.AppSettings [" emailFrom"]。ToString());
email.To.Add(mTo.ToString());
if(subjectMatterExpert.ToString()==" Y")
{
email.Bcc.Add(System.Configuration.ConfigurationManager.AppSettings [groupName] .ToString());
}
//添加文件系统的所有附件
if(file1!=""&& file1!= null)
{
Attachment mailAttachment = new Attachment(file1Path + file1); //创建附件
email.Attachments.Add(mailAttachment);
}
email.Subject = mSubject;
email.Body + ="< font face ='arial'size ='2'>" + mBody +"< / font>" ;;
email.IsBodyHtml = true;
尝试
{
server.Send(email);
}
catch(SmtpFailedRecipientException错误)
{
throw(error);
}
附件可以按流添加附件。
message.Attachments.Add(new Attachment(stream," filename.csv" ,"text / csv"));
因此您需要通过CSOM读取SharePoint文件,并转换为流。
要读取的示例代码文件为流。
var files = context.Web.GetFolderByServerRelativeUrl(uri).Files;
context.Load(files);
context.ExecuteQuery();
foreach(文件中的Microsoft.SharePoint.Client.File文件)
{
ClientResult< System.IO.Stream> data = file.OpenBinaryStream();
context.Load(file);
context.ExecuteQuery();
using(System.IO.MemoryStream mStream = new System.IO.MemoryStream())
{
if(data!= null)
{
data.Value .CopyTo(Mstream工具);
}
}
}阅读附件,你可以查看下面的帖子。
< a href ="https://social.msdn.microsoft.com/Forums/SECURITY/en-US/3881057d-6a43-4f07-8387-23d6a6dd9648/sharepoint-list-items-get-attachments-c-console-app?论坛= sharepointgeneral"> https://social.msdn.microsoft.com/Forums/SECURITY/en-US/3881057d-6a43-4f07-8387-23d6a6dd9648/sharepoint-list-items-get-attachments-c-console-app ?forum = sharepointgeneral
最好的问候,
Lee
I need to pull all the attachments that are in a sharepoint list item and attach them to an email that I currently have set up.
Below is the code to grab the attachments - well i am getting the attachment name file the sharepoint list, but not sure how I can get the actual file, so I can then attach to the email.
Below is what I have thus far to grab the list item attachments.
clientContext.Load(targetListItem, item => item["Title"]); clientContext.Load(attachments); clientContext.ExecuteQuery(); if (targetListItem.AttachmentFiles.Count > 0) { foreach (var attachment in targetListItem.AttachmentFiles) { path = attachment.ServerRelativeUrl; //get the name of the file, search string of value after last slash pos = path.LastIndexOf("/") + 1; filename = path.Substring(pos, path.Length - pos); lblSupportingDocuments.Text += "<a href='" + ConfigurationManager.AppSettings["BaseURL"].ToString() + attachment.ServerRelativeUrl + "' target='_blank'>" + filename + "</a><br/>"; } }
Below is the code I have for the sending of email and attaching files, just not sure how the attachment from above gets put in as part of the email attachment.
SmtpClient server = new SmtpClient(System.Configuration.ConfigurationManager.AppSettings["emailServer"].ToString()); MailMessage email = new MailMessage(); email.From = new MailAddress(System.Configuration.ConfigurationManager.AppSettings["emailFrom"].ToString()); email.To.Add(mTo.ToString()); if (subjectMatterExpert.ToString() == "Y") { email.Bcc.Add(System.Configuration.ConfigurationManager.AppSettings[groupName].ToString()); } //add any attachments from the filesystem if (file1 != "" && file1 != null) { Attachment mailAttachment = new Attachment(file1Path + file1); //create the attachment email.Attachments.Add(mailAttachment); } email.Subject = mSubject; email.Body += "<font face='arial' size='2'>" + mBody + "</font>"; email.IsBodyHtml = true; try { server.Send(email); } catch (SmtpFailedRecipientException error) { throw (error); }
Hi,
Attachments could add an attachment by stream.
message.Attachments.Add(new Attachment(stream, "filename.csv", "text/csv"));
so you need read SharePoint file by CSOM, and convert to stream.
Sample code to read file as stream.
var files = context.Web.GetFolderByServerRelativeUrl(uri).Files; context.Load(files); context.ExecuteQuery(); foreach (Microsoft.SharePoint.Client.File file in files) { ClientResult<System.IO.Stream> data = file.OpenBinaryStream(); context.Load(file); context.ExecuteQuery(); using (System.IO.MemoryStream mStream = new System.IO.MemoryStream()) { if (data != null) { data.Value.CopyTo(mStream); } } }Read attachements, you could check below thread.
Best Regards,
Lee
这篇关于从sharepoint列表项中检索附件并附加到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!