-Message of Gmail in readable text

I have looked everywhere and continue the dead end.

I am trying to use the Google APIs to load gmail into a C # plugin and then put it in the generated mail objects class that the parent program can understand.

I got the ability to download messages, and I can get everything except the actual body of the email, which is deeply immersed in the MessagePart payload structure.

What I need is a way to convert parts of the payload to Richtext or HTML, if possible, or plain text, if not.

At the moment, I do not need any attachments of the images to go through, just the text in the body.

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 to gmail.body would be greatly appreciated.

+4
2

, , ...

MIME, , base64

, ...

public static String GetMimeString(MessagePart Parts)
    {
        String Body = "";

        if (Parts.Parts != null)
        {
            foreach (MessagePart part in Parts.Parts)
            {
                Body = String.Format("{0}\n{1}", Body, GetMimeString(part));
            }
        }
        else if (Parts.Body.Data != null && Parts.Body.AttachmentId == null && Parts.MimeType == "text/plain")
        {
            String codedBody = Parts.Body.Data.Replace("-", "+");
            codedBody = codedBody.Replace("_", "/");
            byte[] data = Convert.FromBase64String(codedBody);
            Body = Encoding.UTF8.GetString(data);
        }

        return Body;
+7

@Stephen Hammond Objective C ( ):

NSString *body = [self getMimeString:message.payload];

- (NSString*)getMimeString:(GTLRGmail_MessagePart*)parts {
    NSString *body = @"";

    if (parts.parts) {
        for (GTLRGmail_MessagePart *part in parts.parts) {
            body = [NSString stringWithFormat:@"%@\n%@", body, [self getMimeString:part]];
        }
    }
    else if (parts.body.data && !parts.body.attachmentId && [parts.mimeType.lowercaseString isEqualToString:@"text/plain"]) {
        NSString *base64String = [parts.body.data stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
        base64String = [base64String stringByReplacingOccurrencesOfString:@"_" withString:@"/"];

        NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
        body = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];

    }

    return body;
}
0

All Articles