读取文本/html消息部分时遇到问题 [英] Having trouble reading the text/html message part
问题描述
我正在尝试使用.Net提取电子邮件的主题和正文.除了text/html MessagePart之外,一切似乎还可以.我不确定编码等-有人能正常工作吗?尝试转换时,这对我来说是错误的.
I'm trying to pull out the subject and body of an email with .Net. It seems to go OK except for the text/html MessagePart. I'm not sure of the encoding etc - has anybody got this working OK? It errors for me when trying to convert.
这是text/html正文数据的原始字符串
Here is the raw string for the text/html Body Data
"PGRpdiBkaXI9Imx0ciI-dGV4dCBpbiBoZXJlPGJyPjwvZGl2Pg0K"
"PGRpdiBkaXI9Imx0ciI-dGV4dCBpbiBoZXJlPGJyPjwvZGl2Pg0K"
这会引发错误.
该输入不是有效的Base-64字符串,因为它包含非base 64字符,两个以上的填充字符或填充字符中的非法字符."
"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters."
这是代码:
UsersResource.MessagesResource.GetRequest gr = gs.Users.Messages.Get(userEmail, TextBox1.Text);
gr.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Full;
Message m = gr.Execute();
foreach (MessagePart p in m.Payload.Parts)
{
if (p.MimeType == "text/html")
{
try
{
byte[] data = Convert.FromBase64String(p.Body.Data);
string decodedString = Encoding.UTF8.GetString(data);
Response.Write(decodedString);
}
catch (Exception ex) { }
}
}
我解码出错了?
感谢您的帮助.
推荐答案
主体数据似乎是base64url编码的,而不是base64编码的. 区别在于,在编码的64个字符的字母表中使用了-和_而不是+和/. 一种解决方案是在调用FromBase64String之前分别用+和/替换所有-和_字符.
The body data appears to be base64url-encoded, not base64-encoded. The difference is the use of - and _, instead of + and /, in the encoding’s alphabet of 64 characters. One solution is to replace all - and _ characters with + and / respectively, before calling FromBase64String.
请参见 http://tools.ietf.org/html/rfc4648#section-5
这篇关于读取文本/html消息部分时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!