Пример #1
0
        /// <summary>
        /// Generates the email.
        /// </summary>
        /// <param name="collectionOfAttachments">The collection of attachments.</param>
        /// <returns>Stream Content</returns>
        internal static Stream GenerateEmail(Dictionary <string, Stream> collectionOfAttachments, string[] documentUrls, bool attachmentFlag)
        {
            Stream result = null;

            try
            {
                MemoryStream mailFile = GetMailAsStream(collectionOfAttachments, documentUrls, attachmentFlag);
                mailFile.Position = 0;
                WebOperationContext.Current.OutgoingResponse.Headers.Clear();
                WebOperationContext.Current.OutgoingResponse.ContentType   = MailHelperFunctions.ReturnExtension(string.Empty);
                WebOperationContext.Current.OutgoingResponse.ContentLength = mailFile.Length;
                WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Description: File Transfer");
                WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Disposition", "attachment; filename=" + ServiceConstantStrings.TempEmailName + DateTime.Now + ConstantStrings.EmailFileExtension);
                WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Transfer-Encoding: binary");
                WebOperationContext.Current.OutgoingResponse.Headers.Add("Expires: 0");
                WebOperationContext.Current.OutgoingResponse.Headers.Add("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                WebOperationContext.Current.OutgoingResponse.Headers.Add("Pragma: public");
                result = mailFile;
            }
            catch (Exception exception)
            {
                Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName);
                Stream mailFile = null;
                result = mailFile;
            }
            return(result);
        }
Пример #2
0
        public Stream SaveEmail(RequestObject requestObject, MailAttachmentDetails mailAttachmentDetails)
        {
            Stream result = null;

            try
            {
                if (null != requestObject && null != mailAttachmentDetails && ValidationHelperFunctions.CheckRequestValidatorToken())
                {
                    ///// filename, stream
                    Dictionary <string, Stream> collectionOfAttachments = new Dictionary <string, Stream>();
                    ///// full URL, relative URL
                    string[]      allAttachmentUrl = mailAttachmentDetails.FullUrl.Split(';');
                    ClientContext clientContext;
                    bool          attachmentFlag = mailAttachmentDetails.IsAttachmentCall;
                    if (attachmentFlag)
                    {
                        foreach (string attachmentUrl in allAttachmentUrl)
                        {
                            if (!string.IsNullOrWhiteSpace(attachmentUrl))
                            {
                                // Do not use 'using' to generate client context here since it will dispose the object before other functions get executed
                                clientContext = ServiceUtility.GetClientContext(requestObject.SPAppToken, new Uri(attachmentUrl.Split(Convert.ToChar(ConstantStrings.DOLLAR, CultureInfo.InvariantCulture))[0]), requestObject.RefreshToken);
                                Microsoft.SharePoint.Client.File file       = clientContext.Web.GetFileByServerRelativeUrl(attachmentUrl.Split(Convert.ToChar(ConstantStrings.DOLLAR, CultureInfo.InvariantCulture))[1]);
                                ClientResult <System.IO.Stream>  fileStream = file.OpenBinaryStream();
                                ///// Load the Stream data for the file
                                clientContext.Load(file);
                                clientContext.ExecuteQuery();

                                ///// In order to allow for multiple files with the same name, we provide a GUID tag to ensure unique keys in the dictionary
                                string uniqueKeyWithDate = attachmentUrl.Split(Convert.ToChar(ConstantStrings.DOLLAR, CultureInfo.InvariantCulture))[1].Substring(attachmentUrl.Split(Convert.ToChar(ConstantStrings.DOLLAR, CultureInfo.InvariantCulture))[1].LastIndexOf(Convert.ToChar(ConstantStrings.BackwardSlash, CultureInfo.InvariantCulture)) + 1) + ConstantStrings.DOLLAR + Guid.NewGuid();
                                collectionOfAttachments.Add(uniqueKeyWithDate, fileStream.Value);
                            }
                        }
                    }

                    result = MailHelperFunctions.GenerateEmail(collectionOfAttachments, allAttachmentUrl, attachmentFlag);
                }
                else
                {
                    result = BriefcaseHelperFunction.ReturnErrorFile(TextConstants.MessageNoInputs);
                }
            }
            catch (Exception exception)
            {
                Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName);
                result = BriefcaseHelperFunction.ReturnErrorFile(string.Concat(ConstantStrings.ServiceResponse, exception.HResult, exception.Message));
            }
            return(result);
        }