我已经到处寻找并继续陷入死胡同。
我正在尝试使用Google API将gmail下载到C#插件中,然后将其放入父程序可以理解的已创建的邮件对象类中。
我已经到了能够下载消息的地步,我可以获得除了实际的电子邮件正文之外的所有内容,这些正在深埋在有效负载的MessagePart结构中。
我所追求的是一种将Payload的部分转换为Richtext或HTML(如果可能)或纯文本的方法,如果没有。
目前我不需要任何图像附件,只需要身体中的文字。
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);
}
}
如何获取我需要添加到gmail.body中的数据的任何帮助将不胜感激。
答案 0 :(得分:7)
直到最后,还有更多的搜索和追逐以及更多的死胡同......
由于我有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;
答案 1 :(得分:0)
这是目标C中的@Stephen Hammond解决方案(测试和工作):
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;
}