问题
Is it possible to convert a Gmail API Message into an OpenPop Mime Message?
Currently, I have:
List<Message> messagesList = new List<Message>();
List<string> rawMessagesList = new List<string>();
foreach(Message m in messages)
{
Message m2 = service.Users.Messages.Get("me", m.Id).Execute();
string m3 = service.Users.Messages.Get("me", m.Id).Execute().Raw;
messagesList.Add(m2);
rawMessagesList.Add(m3);
}
string rMessage = rawMessagesList[0];
byte[] byteMessage = Encoding.ASCII.GetBytes(rMessage);
OpenPop.Mime.Message openPopMessage = new OpenPop.Mime.Message(byteMessage);
string newStringMessage = FindPlainTextInMessage(openPopMessage);
Console.Read();
Unfortunately, all it returns is nothing, because the raw request returns as null. Is there a scope requirement, or some other reason why gmail is not returning the raw message?
回答1:
To obtain the Raw string, you need to specify the Raw format in your GetRequest.
var emailRequest = svc.Users.Messages.Get("userId", "id");
emailRequest.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Raw;
var rawString = emailRequest.Execute().Raw;
At this point, rawString is a base64url encoded string. You have to convert it to a normal string before encoding to bytes (see http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-08#appendix-C):
private static byte[] Base64UrlDecode(string arg)
{
// Convert from base64url string to base64 string
string s = arg;
s = s.Replace('-', '+').Replace('_', '/');
switch(s.Length % 4)
{
case 0:
break; // No pad chars in this case
case 2:
s += "==";
break; // Two pad chars
case 3:
s += "=";
break; // One pad char
default:
throw new Exception("Illegal base64url string!");
}
return Convert.FromBase64String(s);
}
You can then use the result of Base64UrlDecode to create the OpenPop MIME message.
来源:https://stackoverflow.com/questions/30268191/converting-a-gmail-api-message-into-an-openpop-mime-message