indy 邮件解码的问题!!utf8无法解决

Dereky 2009-09-11 05:21:31
按照论坛上找到的几个方法,对其他的邮件几倍都可以顺利的解码了
可是发现有一个淘宝的注册信,怎么都不行

这个邮件的标题源是这样的:
=?utf-8?B?5Lqy54ix55qEIHRpYW54aW5nY2Zw77yM?= =?utf-8?B?5a6M5oiQ5pyA5ZCO5LiA5q2l77yM5oKo?= =?utf-8?B?55qE5rOo5YaM5bCx5oiQ5Yqf5LqG77yB?=

最怪异的是,
var
tmp:String;

tmp := IdMessage1.Subject;

如果 直接:Memo1.Lines.Add(tmp);
没问题,但是乱码

如果 ShowMessage(tmp);
都会报错,好像说内存地址什么的错误

我本来想用 ExtractStrings([' '],[],Pchar(tmp),AStrings);
切开这个字符串,来分别解码的,也是不行。。。。

求教该怎么做啊??

初学 Dlephi,经验不足,望各位大大赐教!!!
...全文
230 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
hj1128 2011-10-06
  • 打赏
  • 举报
回复
小周强人。解决我相同的问题。
willzzq 2011-09-27
  • 打赏
  • 举报
回复
以上需要 uses StrUtils,IdCoderMIME,
EncdDecd,HTTPApp
willzzq 2011-09-27
  • 打赏
  • 举报
回复
解决办法有,绝对OK。支持=?gb2312?b? 、 =?gbk?q? 、 =?gbk?b? 、 =?utf-8?q? 、 =?utf-8?b? 五种情况。D7调试通过。
调用方法:str := Base64Decode(utf8str);
======================================================
function Base64Decode(strInput:string):string;
begin
Result := Base64Decode1(strInput);
while (Pos('=?gb', LowerCase(Result))>0) or (Pos('=?utf', LowerCase(Result))>0) do
Result := Base64Decode1(Result);
end;

function Base64Decode1(strInput:string):string;
var
posGB2312B,posGBKQ,posGBKB,posUTF8Q,posUTF8B:Integer;
strDecode,strChar,stmp,s2 : string;
posStart,CharLen : Integer;
posEnd : Integer;
IdDecoderMIME1: TIdDecoderMIME;
begin
posGB2312B := pos('=?gb2312?b?',lowercase(strInput)); if posGB2312B=0 then posGB2312B := 99999999;
posGBKQ := pos('=?gbk?q?',lowercase(strInput)); if posGBKQ=0 then posGBKQ := 99999999;
if posGB2312B < posGBKQ then begin
strChar := '=?gb2312?b?';
CharLen := 11;
end
else begin
strChar := '=?gbk?q?';
CharLen := 8;
end;
posGBKB := pos('=?gbk?b?',lowercase(strInput)); if posGBKB=0 then posGBKB := 99999999;
if (posGBKB< posGB2312B) and ( posGBKB < posGBKQ) then begin
strChar := '=?gbk?b?';
CharLen := 8;
end;
posUTF8Q := pos('=?utf-8?q?',lowercase(strInput)); if posUTF8Q=0 then posUTF8Q := 99999999;
if (posUTF8Q< posGB2312B) and ( posUTF8Q < posGBKQ) and (posUTF8Q <posGBKB) then begin
strChar := '=?utf-8?q?';
CharLen := 10;
end;
posUTF8B := pos('=?utf-8?b?',lowercase(strInput)); if posUTF8B=0 then posUTF8B := 99999999;
if (posUTF8B< posGB2312B) and ( posUTF8B < posGBKQ) and (posUTF8B <posGBKB) and (posUTF8B<posUTF8Q) then begin
strChar := '=?utf-8?b?';
CharLen := 10;
end;

IdDecoderMIME1 := TIdDecoderMIME.Create(nil);

try
while pos(strChar,lowercase(strInput))>0 do begin
try
posStart:=pos(strChar,lowercase(strInput));
posEnd:=posEx('?=',lowercase(strInput),posStart+CharLen);
if ('=?utf-8?b?'= strChar) then
strDecode:=strDecode+copy(strInput,1,posStart-1)+ UTF8Decode(DecodeString (copy(strInput,posStart+CharLen,posEnd-posStart-CharLen)))
else if ( '=?gbk?b?'= strChar) then
strDecode:=strDecode+copy(strInput,1,posStart-1)+ DecodeString (copy(strInput,posStart+CharLen,posEnd-posStart-CharLen))
else if ( '=?utf-8?q?'= strChar) or ( '=?gbk?q?'= strChar) then begin
stmp := copy(strInput,posStart+CharLen,posEnd-posStart-CharLen);
stmp := AnsiReplaceStr(stmp,'=','%');
stmp := AnsiReplaceStr(stmp,'_','+');
stmp := HttpDecode( stmp);
if ('=?utf-8?q?'= strChar) then
stmp := UTF8Decode(stmp);
s2 := copy(strInput,1,posStart-1);
if Trim(s2)='' then s2 :='';
strDecode:=strDecode+ s2 + stmp ;
end
else
strDecode:=strDecode+copy(strInput,1,posStart-1)+ IdDecoderMIME1.DecodeString (copy(strInput,posStart+CharLen,posEnd-posStart-CharLen));
strInput:=copy(strInput,posEnd+2,length(strInput)-posEnd-1);
finally
// Application.ProcessMessages;
end;

end;
finally
IdDecoderMIME1.Free;
end;

strDecode := strDecode + strInput;
result := strDecode;

end;
Dereky 2009-09-14
  • 打赏
  • 举报
回复
狂晕~~
就没有兄弟能帮看一下的吗
Delphi 真的这么冷清啊。。。
Dereky 2009-09-13
  • 打赏
  • 举报
回复

晕啊,没看懂楼上的意思。
是说先要转码?然后再次解码??
Dereky 2009-09-13
  • 打赏
  • 举报
回复
mjp1234airen4385 2009-09-12
  • 打赏
  • 举报
回复
//设置应答内容的类型
IdHttp.Request.ContentType := 'application/x-www-form-urlencoded';
//获取论坛数据,由于论坛是UTF8编码的,故此处需转换
StrData := UTF8ToAnsi(IdHttp.Get(PChar(BBSUrl)));
Dereky 2009-09-12
  • 打赏
  • 举报
回复
yun~
Dereky 2009-09-11
  • 打赏
  • 举报
回复
顶起~
naocha2 2009-09-11
  • 打赏
  • 举报
回复
mark
Dereky 2009-09-11
  • 打赏
  • 举报
回复
啊 应该是 string 吧
macchen1224 2009-09-11
  • 打赏
  • 举报
回复
你好,請問你確定 「IdMessage1.Subject」回傳的值是string嗎?還是pchar,謝謝。

16,749

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧