Example #1
0
        private static List <Tuple <string, Stream> > GetXmlFilesFromList()
        {
            List <Tuple <string, Stream> > inputFileStreams = new List <Tuple <string, Stream> >();  // XML files (item1=Name, item2=Data)

            spClient.ClientContext spCtx = LoginCsom();

            spClient.Web myWeb = spCtx.Web;
            spClient.FolderCollection   myFolders = myWeb.Folders;
            spClient.List               myList    = spCtx.Web.Lists.GetByTitle(SpListName);
            spClient.ListItemCollection allItems  = myList.GetItems(spClient.CamlQuery.CreateAllItemsQuery());
            spCtx.Load(myWeb);
            spCtx.Load(myFolders);
            spCtx.Load(allItems);
            spCtx.ExecuteQuery();

            foreach (spClient.ListItem oneItem in allItems)
            {
                spClient.AttachmentCollection allAttachments = oneItem.AttachmentFiles;
                spCtx.Load(allAttachments);
                spCtx.ExecuteQuery();

                foreach (spClient.Attachment oneAttachment in allAttachments)
                {
                    spClient.File myXmlFile = myWeb.GetFileByServerRelativeUrl(oneAttachment.ServerRelativeUrl);
                    spClient.ClientResult <Stream> myXmlData = myXmlFile.OpenBinaryStream();
                    spCtx.Load(myXmlFile);
                    spCtx.ExecuteQuery();

                    using (MemoryStream mStream = new MemoryStream())
                    {
                        if (myXmlData != null)
                        {
                            myXmlData.Value.CopyTo(mStream);
                            byte[]       myBinFile = mStream.ToArray();
                            MemoryStream xmlStream = new MemoryStream(myBinFile);
                            inputFileStreams.Add(Tuple.Create(myXmlFile.Name, (Stream)xmlStream));
                        }
                    }
                }
            }

            return(inputFileStreams);
        }
        public void AddAttachments(ListItem oListItem)
        {
            ClientContext ctx = (ClientContext)oListItem.Context;

            //Get list item attachments

            Microsoft.SharePoint.Client.AttachmentCollection attachments = oListItem.AttachmentFiles;
            ctx.Load(attachments);
            ctx.ExecuteQuery();

            //Get webclient for downloading attachment file from SharePoint site

            WebClient webclient = Shared.GetWebClient();

            //Loop through item attachments and add to email
            foreach (Microsoft.SharePoint.Client.Attachment attachment in attachments)
            {
                string fileAbsolutePath = Shared.GetRootUrl(ctx) + attachment.ServerRelativeUrl; //location of file on SharePoint site
                byte[] fileDataArray    = webclient.DownloadData(fileAbsolutePath);              //contents of file downloaded to byte array

                addAttachment(fileAbsolutePath, fileDataArray, attachment);
            }
        }
Example #3
0
        /// <summary>
        /// Sends the mail now.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="web">The web.</param>
        /// <param name="toUsers">To users.</param>
        /// <param name="ccUsers">The cc users.</param>
        /// <param name="bccUsers">The BCC users.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <param name="fromUser">From user.</param>
        /// <param name="attachments">The attachments.</param>
        /// <returns>
        /// true or false
        /// </returns>
        public bool SendMailNow(ClientContext context, Web web, List <string> toUsers, List <string> ccUsers, List <string> bccUsers, string subject, string body, string fromUser, Microsoft.SharePoint.Client.AttachmentCollection attachments)
        {
            bool isSent = false;

            try
            {
                //// BELDataAccessLayer helper = new BELDataAccessLayer();
                ExchangeService service          = new ExchangeService(ExchangeVersion.Exchange2013);
                string          emailUserName    = BELDataAccessLayer.Instance.GetConfigVariable("EmailUserName");
                string          emailPassword    = BELDataAccessLayer.Instance.GetConfigVariable("EmailPassword");
                string          emailExchangeURL = BELDataAccessLayer.Instance.GetConfigVariable("EmailExchangeURL");
                service.Credentials        = new WebCredentials(emailUserName, emailPassword);
                service.Url                = new Uri(emailExchangeURL);
                service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, fromUser);
                EmailMessage msg = new EmailMessage(service);
                ////msg.From = new EmailAddress(fromUser);
                ////msg.Sender = new EmailAddress(fromUser);
                if (toUsers != null)
                {
                    foreach (string to in toUsers)
                    {
                        if (!string.IsNullOrEmpty(to))
                        {
                            msg.ToRecipients.Add(new EmailAddress(to));
                        }
                    }
                }
                if (ccUsers != null)
                {
                    foreach (string cc in ccUsers)
                    {
                        if (!string.IsNullOrEmpty(cc))
                        {
                            msg.CcRecipients.Add(new EmailAddress(cc));
                        }
                    }
                }
                if (bccUsers != null)
                {
                    foreach (string bcc in bccUsers)
                    {
                        if (!string.IsNullOrEmpty(bcc))
                        {
                            msg.BccRecipients.Add(new EmailAddress(bcc));
                        }
                    }
                }
                msg.Subject = subject;
                msg.Body    = new MessageBody(BodyType.HTML, HttpUtility.HtmlDecode(body));

                if (attachments != null)
                {
                    foreach (Microsoft.SharePoint.Client.Attachment file in attachments)
                    {
                        byte[] bytes = BELDataAccessLayer.Instance.GetFileBytesByUrl(context, file.ServerRelativeUrl);
                        msg.Attachments.AddFileAttachment(file.FileName, bytes);
                    }
                }
                msg.SendAndSaveCopy();
                isSent = true;
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                isSent = false;
            }
            return(isSent);
        }