如何将附件从sharepoint列表发送到电子邮件 [英] how to send attachment from a sharepoint list to an email
问题描述
您好
我有一个包含附件字段的列表。用户将附上pdf,我希望它附加时,直接将此附件发送到电子邮件。我有一个事件接收器代码,它将从一个站点获取一个现成的附件,并将其发送到电子邮件。它应该
从sharepoint列表中获取附件并将其发送到电子邮件。我该怎么办?请告诉我。
I have a list that contain attachment field. User will attach pdf, and I want it when he attach, directly this attachment sent to an email. I have an event receiver code that will take a ready attachment from a site and will send it to the email. It should take the attachment from sharepoint list and send it to an email. How should I do it? please tell me.
base 。ItemUpdated(属性);
base.ItemUpdated(properties);
SPListItem listItem
= properties.ListItem;
SPListItemlistItem = properties.ListItem;
字符串 OS
= properties.AfterProperties [ " OrderStatus" ]。ToString ();
StringOS = properties.AfterProperties["OrderStatus"].ToString();
字符串 电子邮件
= listItem [ " EmailAddress" ]。ToString ();
StringEmail = listItem["EmailAddress"].ToString();
//从
获取Sharepoint SMTP信息SPAdministrationWebApplication
string smtpServer
= SPAdministrationWebApplication 。Local.OutboundMailServiceInstance.Server .Address;
stringsmtpServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;
string smtpFrom
= SPAdministrationWebApplication 。Local.OutboundMailSenderAddress;
stringsmtpFrom = SPAdministrationWebApplication.Local.OutboundMailSenderAddress;
//创建邮件并从信息中提供
//Create the mail message and supply it with from and to info
// ****它会改变****
MailMessage mailMessage
= new MailMessage (smtpFrom,Email);
MailMessagemailMessage = newMailMessage(smtpFrom,Email);
if (OS
== " completed" )
if(OS == "completed")
{
//设置消息的主题和正文
mailMessage.Subject =
"" ;
mailMessage.Subject = " ";
mailMessage.IsBodyHtml =
true ;
mailMessage.IsBodyHtml = true;
mailMessage.Body =
"您的订单已准备好,快来接受" ;
mailMessage.Body = "Your order is ready, come and take it";
mailMessage.To.Add( < span style ="color:#0000ff; font-size:small"> 新 MailAddress (电子邮件));
mailMessage.To.Add(newMailAddress(Email));
mailMessage.From =
new MailAddress (smtpFrom);
mailMessage.From = newMailAddress(smtpFrom);
if (listItem.Attachments.Count
!= 0)
if(listItem.Attachments.Count != 0)
{
}
//使用WebClient下载文件内容
//Download the content of the file with a WebClient
WebClient webClient
= new WebClient ();
WebClientwebClient = newWebClient();
//为WebClient提供我们用户的网络凭证
//Supply the WebClient with the network credentials of our user
webClient.Credentials =
CredentialCache 。DefaultNetworkCredentials;
webClient.Credentials = CredentialCache.DefaultNetworkCredentials;
< span style ="color:#008000; font-size:small"> //下载文件的字节数组
//Download the byte array of the file
byte []
data = webClient.DownloadData( " http://store1.up-00.com/2017-08/150357134445511.png" < span style ="font-size:small"> );
byte[] data = webClient.DownloadData("http://store1.up-00.com/2017-08/150357134445511.png");
//将字节数组转储到内存流中,因为
//Dump the byte array in a memory stream because
//我们可以将它写入我们的附件
MemoryStream memoryStreamOfFile
= new MemoryStream (数据);
MemoryStreammemoryStreamOfFile = newMemoryStream(data);
//添加附件
mailMessage.Attachments.Add( new System.Net.Mail。 附件 (memoryStreamOfFile,
" LPO" ,
" Image / png" ) );
mailMessage.Attachments.Add(newSystem.Net.Mail.Attachment(memoryStreamOfFile, "LPO", "Image/png"));
//创建SMTP客户端对象并发送
消息
SmtpClient smtpClient
= new SmtpClient (smtpServer);
SmtpClientsmtpClient = newSmtpClient(smtpServer);
smtpClient.Send(mailMessage );
smtpClient.Send(mailMessage);
推荐答案
阅读下面的SharePoint列表附件字段值:
Read the SharePoint list attachment field value like below:
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;
}
}
});
然后下载文件流数据并添加到bsp; mailMessage.Attachments:
Then download the the file stream data and add to mailMessage.Attachments:
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);
}
谢谢
最好的问候
这篇关于如何将附件从sharepoint列表发送到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!