将Gmail API邮件转换为OpenPop Mime邮件 [英] Converting a Gmail API Message into an OpenPop Mime message

查看:60
本文介绍了将Gmail API邮件转换为OpenPop Mime邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将Gmail API邮件转换为OpenPop Mime邮件?

Is it possible to convert a Gmail API Message into an OpenPop Mime Message?

当前,我有:

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();

不幸的是,它返回的全部为空,因为原始请求返回为null.是否有范围要求或其他原因导致gmail不返回原始消息?

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?

推荐答案

要获取Raw字符串,您需要在GetRequest中指定Raw格式.

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;

此时,rawString是base64url编码的字符串.您必须先将其转换为普通字符串,然后再编码为字节(请参见 http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-08#appendix-C ):

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);
}

然后您可以使用Base64UrlDecode的结果来创建OpenPop MIME消息.

You can then use the result of Base64UrlDecode to create the OpenPop MIME message.

这篇关于将Gmail API邮件转换为OpenPop Mime邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆