Example #1
0
        public EWSIncomingMessage(EmailMessage message)
        {
            _message = message;

            message.Load(new PropertySet(
                    ItemSchema.Subject,
                    ItemSchema.Body,
                    EmailMessageSchema.ConversationIndex,
                    EmailMessageSchema.Sender,
                    EmailMessageSchema.From,
                    EmailMessageSchema.ToRecipients,
                    EmailMessageSchema.CcRecipients,
                    ItemSchema.MimeContent,
                    ItemSchema.DateTimeReceived,
                    ItemSchema.DateTimeSent,
                    EmailMessageSchema.ConversationTopic,
                    ItemSchema.Attachments,
                    ItemSchema.HasAttachments,
                    MeetingRequestSchema.Location,
                    MeetingRequestSchema.Start,
                    MeetingRequestSchema.End
                ));

            Attachments = BuildAttachmentList(message);
        }
Example #2
0
        public MailRequest(EmailMessage msg)
        {
            this.From = msg.From.Address;
            this.To = msg.ToRecipients.First().Address;
            this.Subject = msg.Subject;
            this.Body = msg.Body;
            this.OriginalMessage = msg;

            msg.Load(new PropertySet(ItemSchema.MimeContent));
            this.FileFormat = msg.MimeContent.Content;

        }
Example #3
0
        public MailMsg(EmailMessage msg, string user)
        {
            this._User = user;
            this._id = msg.Id.ToString();
            timestamp = msg.DateTimeSent;
            this._Subject = msg.Subject;
            this._Body = msg.Body;

            if (msg.HasAttachments)
            {
                //we will add all attachements to a list
                List<Attachement> oList = new List<Attachement>();
                //load all attachements
                msg.Load(new Microsoft.Exchange.WebServices.Data.PropertySet(Microsoft.Exchange.WebServices.Data.EmailMessageSchema.Attachments));
                foreach (Microsoft.Exchange.WebServices.Data.Attachment att in msg.Attachments)
                {
                    if (att is Microsoft.Exchange.WebServices.Data.FileAttachment)
                    {
                        //load the attachement
                        Microsoft.Exchange.WebServices.Data.FileAttachment fileAttachment = att as Microsoft.Exchange.WebServices.Data.FileAttachment;
                        //load into memory stream, seems the only stream supported
                        System.IO.MemoryStream ms = new System.IO.MemoryStream(att.Size);
                        fileAttachment.Load(ms);//blocks some time
                        ms.Position = 0;
                        oList.Add(new Attachement(ms, fileAttachment.Name));
                        ms.Close();
                    }
                }
                /*
                foreach (Attachment a in msg.Attachments)
                {
                    if (a is Microsoft.Exchange.WebServices.Data.FileAttachment)
                    {
                        FileAttachment fa = a as FileAttachment;
                        System.IO.MemoryStream ms=new System.IO.MemoryStream(fa.Size);
                        fa.Load(ms);
                        oList.Add(new Attachement(ms, fa.Name));
                    }
                }
                */
                this._Attachements = oList.ToArray();
                this.attList.AddRange(oList);
            }
        }
Example #4
0
        /// <summary>
        /// Forms the memory stream of the mail with attachments.
        /// </summary>
        /// <param name="collectionOfAttachments">Collection of attachments as dictionary</param>
        /// <returns>Memory stream of the created mail object</returns>
        internal MemoryStream GetMailAsStream(Dictionary <string, Stream> collectionOfAttachments, string[] documentUrls, bool attachmentFlag)
        {
            MemoryStream result      = null;
            string       documentUrl = string.Empty;

            try
            {
                // need to be able to update/configure or get current version of server
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
                //// can use on premise exchange server credentials with service.UseDefaultCredentials = true, or explicitly specify the admin account (set default to false)
                service.Credentials = new WebCredentials(generalSettings.AdminUserName, generalSettings.AdminPassword);
                service.Url         = new Uri(generalSettings.ExchangeServiceURL);
                Microsoft.Exchange.WebServices.Data.EmailMessage email = new Microsoft.Exchange.WebServices.Data.EmailMessage(service);
                email.Subject = documentSettings.MailCartMailSubject;

                if (attachmentFlag)
                {
                    email.Body = new MessageBody(documentSettings.MailCartMailBody);
                    foreach (KeyValuePair <string, Stream> mailAttachment in collectionOfAttachments)
                    {
                        if (null != mailAttachment.Value)
                        {
                            // Remove the date time string before adding the file as an attachment
                            email.Attachments.AddFileAttachment(mailAttachment.Key.Split('$')[0], mailAttachment.Value);
                        }
                    }
                }
                else
                {
                    int index = 0;
                    foreach (string currentURL in documentUrls)
                    {
                        if (null != currentURL && 0 < currentURL.Length)
                        {
                            string[] currentAssets = currentURL.Split('$');
                            string   documentURL   = generalSettings.SiteURL + currentAssets[1];
                            string   documentName  = currentAssets[2];

                            documentUrl = string.Concat(documentUrl, string.Format(CultureInfo.InvariantCulture, "'{0} ) {1} : <a href='{2}'>{2} </a><br/>", ++index, documentName, documentURL));
                        }
                    }
                    documentUrl = string.Format(CultureInfo.InvariantCulture, "<div style='font-family:Calibri;font-size:12pt'>{0}</div>", documentUrl);
                    email.Body  = new MessageBody(documentUrl);
                }
                //// This header allows us to open the .eml in compose mode in outlook
                email.SetExtendedProperty(new ExtendedPropertyDefinition(DefaultExtendedPropertySet.InternetHeaders, "X-Unsent", MapiPropertyType.String), "1");
                email.Save(WellKnownFolderName.Drafts); // must save draft in order to get MimeContent
                email.Load(new PropertySet(EmailMessageSchema.MimeContent));
                MimeContent mimcon = email.MimeContent;
                //// Do not make the StylCop fixes for MemoryStream here
                MemoryStream fileContents = new MemoryStream();
                fileContents.Write(mimcon.Content, 0, mimcon.Content.Length);
                fileContents.Position = 0;
                result = fileContents;
            }
            catch (Exception exception)
            {
                //Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName);
                MemoryStream fileContents = new MemoryStream();
                result = fileContents;
            }
            return(result);
        }
        /// <summary>
        /// Forms the memory stream of the mail with attachments.
        /// </summary>
        /// <param name="collectionOfAttachments">Collection of attachments as dictionary</param>
        /// <returns>Memory stream of the created mail object</returns>
        internal MemoryStream GetMailAsStream(Dictionary<string, Stream> collectionOfAttachments, string[] documentUrls, bool attachmentFlag)
        {
            MemoryStream result = null;
            string documentUrl = string.Empty;
            try
            {
                // need to be able to update/configure or get current version of server
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
                //// can use on premise exchange server credentials with service.UseDefaultCredentials = true, or explicitly specify the admin account (set default to false)
                service.Credentials = new WebCredentials(generalSettings.AdminUserName, generalSettings.AdminPassword);
                service.Url = new Uri(generalSettings.ExchangeServiceURL);
                Microsoft.Exchange.WebServices.Data.EmailMessage email = new Microsoft.Exchange.WebServices.Data.EmailMessage(service);
                email.Subject = documentSettings.MailCartMailSubject;

                if (attachmentFlag)
                {
                    email.Body = new MessageBody(documentSettings.MailCartMailBody);
                    foreach (KeyValuePair<string, Stream> mailAttachment in collectionOfAttachments)
                    {
                        if (null != mailAttachment.Value)
                        {
                            // Remove the date time string before adding the file as an attachment
                            email.Attachments.AddFileAttachment(mailAttachment.Key.Split('$')[0], mailAttachment.Value);
                        }
                    }
                }
                else
                {
                    int index = 0;
                    foreach (string currentURL in documentUrls)
                    {
                        if (null != currentURL && 0 < currentURL.Length)
                        {
                            string[] currentAssets = currentURL.Split('$');
                            string documentURL = generalSettings.SiteURL + currentAssets[1];
                            string documentName = currentAssets[2];

                            documentUrl = string.Concat(documentUrl, string.Format(CultureInfo.InvariantCulture, "{0} ) {1} : <a href='{2}'>{2} </a><br/>" , ++index, documentName, documentURL));
                        }
                    }
                    documentUrl = string.Format(CultureInfo.InvariantCulture, "<div style='font-family:Calibri;font-size:12pt'>{0}</div>", documentUrl);
                    email.Body = new MessageBody(documentUrl);
                }
                //// This header allows us to open the .eml in compose mode in outlook
                email.SetExtendedProperty(new ExtendedPropertyDefinition(DefaultExtendedPropertySet.InternetHeaders, "X-Unsent", MapiPropertyType.String), "1");
                email.Save(WellKnownFolderName.Drafts); // must save draft in order to get MimeContent
                email.Load(new PropertySet(EmailMessageSchema.MimeContent));
                MimeContent mimcon = email.MimeContent;
                //// Do not make the StylCop fixes for MemoryStream here
                MemoryStream fileContents = new MemoryStream();
                fileContents.Write(mimcon.Content, 0, mimcon.Content.Length);
                fileContents.Position = 0;
                result = fileContents;
            }
            catch (Exception exception)
            {
                //Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName);
                MemoryStream fileContents = new MemoryStream();
                result = fileContents;
            }
            return result;
        }