none
MailItem.Body is always null RRS feed

  • Question

  • Whenever i get a appointment response from the server i'm saving it to the mail body and finally doing mailitem.save().
    But when i try to access the same in order to display in the calendar by trying to access mailItem.Body, it always returns null. It is happening for 50% of the user who is using the plugin which will started when they open the outlook. I mean people with new laptop or desktop are not facing this issue.  I was referring this stackoverflow link, where they were mentioning about some security issue(link below). Could you elaborate it more?

    https://stackoverflow.com/questions/26067849/error-reading-outlook-calendar-appointment-with-c-sharp-program


    Tuesday, May 15, 2018 2:50 AM

All replies

  • Hello sharathnasa,

    >>Whenever i get a appointment response from the server i'm saving it to the mail body and finally doing mailitem.save().

    Why do you save the appointment response in to mail body? How do you do this?

    >>But when i try to access the same in order to display in the calendar by trying to access mailItem.Body, it always returns null.

    Some code that could illustrate us what you have done will be helpful for us to reproduce your issue.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 15, 2018 6:52 AM
  • We have cloud integration with outlook using the plugin we have built. So whenever we create an appointment in the cloud, that will sync to outlook. So when we get a response from cloud, we're storing it in the offline store and then after successfully saving we're getting the data from store and displaying it in the outlook calendar. 

    Snippet of saving to store:

    
    SaveType saveType = SaveType.Outlook;
                        FolderManager folderManager = FolderManager.GetInstance();
                        if (null != storeItem)
                        {
                            //XmlDocument xmlDocument = storeItem.XmlDocument;
                            XDocument xDoc = storeItem.XDocument;
                            //if (null != xmlDocument)
                            if (null != xDoc)
                            {
                                MAPIFolder folderStore = GetFolderStore(storeLocationType);
                              
                                try
                                {
    
                                    Items items = folderStore.Items;
                                }
                                catch (System.Exception ex)
                                {
                                  
                                    folderManager.CreateFolderStructure();
                                    folderStore = GetFolderStore(storeLocationType);
                                   
                                }
                                
                                 if (null != folderStore)
                                {
                                    MailItem mailItem = null;
                                    
                                    if (isUpdate)
                                    {
                                        mailItem = GetItemByEntryId(folderStore, storeItem.Id);
                                        if (null == mailItem)
                                        {
                                            if (null != logger) logger.Log(LogSeverity.DEBUG, null, METHOD_NAME, null, String.Format("{0}: Couldn't find MailItem in store for update.", METHOD_NAME));
                                        }
                                        if (null != logger) logger.Log(LogSeverity.DEBUG, null, METHOD_NAME, null, String.Format("{0}: Mail item retreived from store is : {1}", METHOD_NAME,this.getStoreItemName(storeItem)));
    
                                    }
                                    else
                                    {
                                        mailItem = folderStore.Items.Add(OlItemType.olMailItem) as MailItem;
                                       
                                    }
    
                                    if (null != mailItem)
                                    {
                                        PropertyAccessor propAccess = PropAccessor.getPropertyAccessor(mailItem);
                                        String prop_X_SAP_BYD_GW_UNIQUE_ID = PropAccessor.getPropValue(propAccess, Constants.X_SAP_BYD_GW_UNIQUE_ID);
                                        mailItem.BodyFormat = OlBodyFormat.olFormatPlain;
                                      
                                        
                                        using (XmlReader reader = xDoc.CreateReader(ReaderOptions.None))
                                        {
                                            reader.MoveToContent();
                                            //It is important to use the XmlReader to prevent out of memory issues
                                            mailItem.Body = reader.ReadOuterXml();
                                            if (null != logger) logger.Log(LogSeverity.DEBUG, null, METHOD_NAME, null, ("Mail item body : " + mailItem.Body));
                                        }
    
                                        MetadataDetails metadataDetails = storeItem.MetadataDetails;
                                        if (null != metadataDetails)
                                        {
                                           
                                            try
                                            {
                                                PropAccessor.setPropValue(propAccess, Constants.X_SAP_BYD_GW_UNIQUE_ID, metadataDetails.UniqueId);
    
                                              
                                            }
                                            catch (System.Exception ex)
                                            {
                                                if (null != logger) logger.Log(LogSeverity.ERROR, null, METHOD_NAME, ex, null);
                                            }
    
                                        }
                                        else
                                        {
                                            if (null != logger) logger.Log(LogSeverity.DEBUG, null, METHOD_NAME, null, ("Metadatadetails is nil"));
                                        }
    
                                        mailItem.Subject = mailItem.EntryID;
                                        mailItem.UnRead = false;
                                        mailItem.Save();
                                        if (null != logger) logger.Log(LogSeverity.DEBUG, null, METHOD_NAME, null, ("Saved mail item : " + this.getMetadataTitle(metadataDetails)));
                                        try
                                        {
                                            if (null != logger) logger.Log(LogSeverity.DEBUG, null, METHOD_NAME, null, ("Moving mail item to store :" + this.getMetadataTitle(metadataDetails)));
                                            if(null != folderStore.FullFolderPath && null != logger) logger.Log(LogSeverity.DEBUG, null, METHOD_NAME, null, ("Folder path is :  " + this.getFullFolderPath(folderStore)));
                                            MailItem mailItm = mailItem.Move(folderStore);
                                            if (null != logger) logger.Log(LogSeverity.DEBUG, null, METHOD_NAME, null, (String.Format("Mail item : {0} moved successfully to store : {1}", this.getMetadataTitle(metadataDetails), this.getFullFolderPath(folderStore))));
    
                                        }
                                        catch (System.Exception ex)
                                        {
                                            if (null != logger) logger.Log(LogSeverity.ERROR, null, METHOD_NAME, ex, "Couldn't move mail item");
                                        }
                                    }
                         }   }    }

    Code to read from the store:

     if (string.IsNullOrEmpty(this.storeLocationPath))
                        {
                            folder = folderStoreManager.GetFolderStore(storeLocationType);
                        }
                        else
                        {
                            folder = folderStoreManager.GetFolderStore(storeLocationPath);
                        }
                        if (null != folder)
                        {
                            if (folder.Items.Count >= index)
                            {
                                tempMailItem = (MailItem)folder.Items[index];
                                if (null != tempMailItem)
                                {
                                    
                                    XDocument xDoc = XDocument.Load(new StringReader(tempMailItem.Body));
    
                                    MetadataDetails metadataDetails = GetMetadataDetails(tempMailItem);
    
                                    if (null != xDoc)
                                    {
                                        returnValue = new StoreItem(xDoc, metadataDetails);
                                        returnValue.Id = tempMailItem.EntryID;
                                        list.Add(tempMailItem.EntryID);
                                    }
    
                                    if (null != tempMailItem)
                                    {
                                        Marshal.ReleaseComObject(tempMailItem);
                                        tempMailItem = null;
                                    }
                                }
                            }
                        }


    tempMailItem.Body is giving null

    Regards,

    Sharath

    Tuesday, May 15, 2018 7:19 AM
  • Hello Sharath,

    I would suggest you output mailitem.body before saving to confirm if the mailitem.body has any content.

    Are you sure the tempMailItem is the mailitem you saved? Except body property, have you tried to output some other properties? Such as Subjects, SenderMailAddress..etc. Will them return correct result?

    Besides, what do you mean "appointment response"? I thought it was an AppointmentItem or a MeetingItem, but I could only see mail item in your code. What is the "storeItem"? What's the type of the item? Have you tried to use a normal mail for testing?

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 15, 2018 8:50 AM
  • Sharath,

    Where and when do you set the Body property?

    I don't see any place in the code where it is set up.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Tuesday, May 15, 2018 9:49 AM
  • Hi Eugene,

    I think Sharath used this part to set body property.

                                        using (XmlReader reader = xDoc.CreateReader(ReaderOptions.None))
                                        {
                                            reader.MoveToContent();
                                            //It is important to use the XmlReader to prevent out of memory issues
                                            mailItem.Body = reader.ReadOuterXml();
                                            if (null != logger) logger.Log(LogSeverity.DEBUG, null, METHOD_NAME, null, ("Mail item body : " + mailItem.Body));
                                        }

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 15, 2018 9:53 AM
  • Thank you, Terry.

    Most probably the reader.ReadOuterXml() call returns null. I'd recommend checking it there.

    string outerXml = reader.ReadOuterXml(); if(!string.IsNullOrWhitespace(outerXml)) mailItem.Body = outerXml; else mailItem.Body = "Hello world!";


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Tuesday, May 15, 2018 10:01 AM