• Security incident: ISF was recently accessed by intruders. Please change your password, and change it anywhere else you used it. Read more

How to Parse Gmails MessageParts in C#

PhantomWolf

Penultimate Amazing
Joined
Mar 6, 2007
Messages
21,203
I know we have a lot of programmer on the board so I am hoping that someone has played with Google's APIs before and might have a suggestion.

I have looked all over the place and keep running into dead ends.

I am trying to use the Google APIs to download gmail into a C# plugin and then place it into a created Mail Object Class that the parent program can understand.

I have gotten to the point of being able to download the Messages and I can get everything except the actual E-Mail body, which is buried deeply in the payload's MessagePart structure.

What I am after is a way to convert the Payload's Parts into Richtext or HTML if possible, or pure text if not.

At the moment I don't need to have any image attachments come through, just the text in the body.

This is the code I'm using...

Code:
GmailService gs = new GmailService(new Google.Apis.Services.BaseClientService.Initializer() {   ApplicationName = Constant.clientId, HttpClientInitializer = credential });

UsersResource.MessagesResource messagesResource = gs.Users.Messages;
IList<Message> messages = messagesResource.List(userGmail.Value).Execute().Messages;
foreach (Message message in messages)
{
   String messageID = message.Id;
   ReceivedGmail check = ObjectSpace.RetrieveObjectByKeyProperty<ReceivedGmail>(messageID);

   if (check == null)
   {
       messagesResource.Get(userGmail.Value, messageID).Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Raw;

       Message mail = messagesResource.Get(userGmail.Value, messageID).Execute();
       MessagePart mailbody = mail.Payload;

       ReceivedGmail gmail = new ReceivedGmail() { User = User.CurrentUser, Unread = true, mailID = messageID };

       //Get all the header information from the message (Date, Emails, etc.) and add to gmail object.

       //gmail.body = ?????;

       ObjectSpace.StoreObject<ReceivedGmail>(gmail);
    }
}

Any help on how to get the data I need to add into gmail.body would be greatly appreciated.
 
Guess no one knows, but I did manage to solve it.

Just had to find the Part with the MimeType of "text/plain" using recursion and then after replacing "+" for "-" and "/" for any "_" in the Body.Data string, decode it with standard Base64.

Easy...
 

ISF - Join now!

Every member here is approved by hand. No bots, no spam, just people who care about evidence and honest debate.

Membership is free!

Create your free account

Back
Top Bottom