Extra newline carriage returns from Graph SDK Get Mail API for Email Body

Raghuvanshi, Amit 1 Reputation point
2023-12-05T11:13:21.64+00:00

We are using Graph API to get mails from a mail box, mail body is fetched in text format.

Currently facing 2 Issues

  1. For a specific mail We are getting additional new line carriage returns, because of which when we construct the text file it has additional new line character.
  2. Text file is being constructed in C++ using ofstream object, which is opened in text mode i.e. ios::out and when \r\n is fed to the file its adding additional new line character to the file.Mail With Issue.png
Developer technologies | C++
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Akash Jadav 85 Reputation points Microsoft External Staff
    2023-12-15T08:56:09.4533333+00:00

    Hi there!

    The extra newline character is likely a result of the way the text data is formatted or retrieved from MS Graph API.

    We can use following C++ code by processing the mail body string response as mentioned below:

    std::string mailBody = "This is a sample mail body.\n\n\n\nSample New lines.\n\n\n\n";
    std:: regex regexNewlines("(\\r\\n|\\n){2,}");
    mailBody = std::regex_replace(mailBody, regexNewlines, "\n");
    
    std::cout << mailBody << std::endl;
    ```Below is a JavaScript code snippet (web context) for processing the mail body string:
    
    
    ```javascript
    let mailBody = "This is a sample mail body.\n\n\n\nSample New lines.\n\n\n\n";
    
    const regexNewlines = /(\r\n|\n){2,}/g;
    mailBody = mailBody.replace(regexNewlines, "\n");
    
    console.log(mailBody);
    

    We suggest processing the response received in MS Graph API.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.