locked
CRM 2013 reading Annotation to a MemoryStream (without saving it to the disk) RRS feed

  • Question

  • Hi

    I need to programmatically send an email and attach certain annotations to it. I managed to retrieve the correct annotation entities, but I have a problem with attaching them to the MailMessage. The official way of retrieving the file from Annotation uses a FileStream, which saves the file to disk. I'd rather not do this and convert to a MemoryStream instead, but I'm not sure how to do this.

    I'd appretiate any help on the subject.

    My code so far:

    private void CreateMail(IOrganizationService pService, string pTo, string pFrom)
            {
                string _subject = //my subject
                string _body = //my body
    
                MailMessage _mm = new MailMessage(pFrom, pTo, _subject, _body)
                {
                    IsBodyHtml = false
                };
    
                var _annotations = CRMData.GetAnnotationsByEntity(pService, InvoiceId);
    
                foreach (Entity _e in _annotations.Entities)
                {
                    string _filename = string.Empty;
                    if (_e.Contains("filename"))
                        _filename = Convert.ToString(_e["filename"]);
                    else
                        continue;
    
                    using (FileStream _fs = new FileStream(_filename, FileMode.Create))
                        _mm.Attachments.Add(new Attachment(_fs, _filename));
                }
    
                CommonMethods.SendMail(_mm);
            }
    
    public static EntityCollection GetAnnotationsByEntity(IOrganizationService pService, Guid pEntityId)
            {
                QueryExpression _query = new QueryExpression("annotation");
                _query.ColumnSet = new ColumnSet("filename", "documentbody");
    
                _query.Criteria.AddCondition(new ConditionExpression("objectid", ConditionOperator.Equal, pEntityId));
    
                return pService.RetrieveMultiple(_query);
            }

    Regards
    Grzegorz Zych



    • Edited by Grzegorz Zych Wednesday, September 23, 2015 2:11 PM typos
    Wednesday, September 23, 2015 2:10 PM

Answers

  • Sadly, this does not solve the underlying problem, because the FileStream constructor still creates the file on disk.

    I managed to solve the problem in the following way:

    private void CreateMail(IOrganizationService pService, string pTo, string pFrom)
            {
                string _subject = "my subject";
                string _body = "my body";
    
                MailMessage _mm = new MailMessage(pFrom, pTo, _subject, _body)
                {
                    IsBodyHtml = false
                };
    
                var _annotations = CRMData.GetAnnotationsByEntity(pService, InvoiceId);
    
                Dictionary<string, MemoryStream> _msList = new Dictionary<string, MemoryStream>();
                foreach (Entity _e in _annotations.Entities)
                {
                    string _filename = string.Empty;
                    if (_e.Contains("filename"))
                        _filename = Convert.ToString(_e["filename"]);
                    else
                        continue;
    
                    string _documentBody = string.Empty;
                    if (_e.Contains("documentbody"))
                        _documentBody = Convert.ToString(_e["documentbody"]);
                    else
                        continue;
    
                    byte[] imageBytes = Convert.FromBase64String(_documentBody);
                    _msList.Add(_filename, new MemoryStream(imageBytes, 0, imageBytes.Length));
                }
    
                foreach(var _ms in _msList)
                    _mm.Attachments.Add(new Attachment(_ms.Value, _ms.Key));
    
                CommonMethods.SendMail(_mm);
    
                foreach (var _ms in _msList)
                    _ms.Value.Dispose();
            }

    • Marked as answer by Grzegorz Zych Thursday, September 24, 2015 8:38 AM
    Thursday, September 24, 2015 8:37 AM

All replies

  • I think the file has to be Base 64. You could try something like this to convert it:

    using (FileStream _fs = new FileStream(_filename, FileMode.Create))
            {
                byte[] buffer = new byte[_fs.Length];
                _fs.Read(buffer, 0, (int)_fs.Length);
                _mm.Attachments.Add(new Attachment(Convert.ToBase64String(buffer), _filename);
            }

    Wednesday, September 23, 2015 3:35 PM
  • Sadly, this does not solve the underlying problem, because the FileStream constructor still creates the file on disk.

    I managed to solve the problem in the following way:

    private void CreateMail(IOrganizationService pService, string pTo, string pFrom)
            {
                string _subject = "my subject";
                string _body = "my body";
    
                MailMessage _mm = new MailMessage(pFrom, pTo, _subject, _body)
                {
                    IsBodyHtml = false
                };
    
                var _annotations = CRMData.GetAnnotationsByEntity(pService, InvoiceId);
    
                Dictionary<string, MemoryStream> _msList = new Dictionary<string, MemoryStream>();
                foreach (Entity _e in _annotations.Entities)
                {
                    string _filename = string.Empty;
                    if (_e.Contains("filename"))
                        _filename = Convert.ToString(_e["filename"]);
                    else
                        continue;
    
                    string _documentBody = string.Empty;
                    if (_e.Contains("documentbody"))
                        _documentBody = Convert.ToString(_e["documentbody"]);
                    else
                        continue;
    
                    byte[] imageBytes = Convert.FromBase64String(_documentBody);
                    _msList.Add(_filename, new MemoryStream(imageBytes, 0, imageBytes.Length));
                }
    
                foreach(var _ms in _msList)
                    _mm.Attachments.Add(new Attachment(_ms.Value, _ms.Key));
    
                CommonMethods.SendMail(_mm);
    
                foreach (var _ms in _msList)
                    _ms.Value.Dispose();
            }

    • Marked as answer by Grzegorz Zych Thursday, September 24, 2015 8:38 AM
    Thursday, September 24, 2015 8:37 AM