写一个收邮件的程序,标题要Base64解码。我看网上的写了一个解GB2312的函数,可以用,但碰到UTF-8编码的就不行。
然后我又找到一个说是能解UTF-8的:http://bbs.csdn.net/topics/300045820,用他的字符串测试了一下,的确可以。可一旦用我收到的字符串:'=?UTF-8?B?54yq?='来测试,怎么弄都是乱码:'??'。我很迷惑:到底是我的字符串有问题还是函数有问题?我用的是Delphi7tips:那个邮件是从手机客户端发出的

解决方案 »

  1.   

    不,头文件已经截到了,但idMessage.Subject收到的是上面那个Base64字符串,现在要将它解码
      

  2.   

    贴一下代码,大家学习一下:function TForm1.Base64Decode(strInput:string):string;
    var
      strDecode:string;
      posStart:Integer;
      posEnd:Integer;
      tmp:string;
    begin
      while pos('=?utf-8?b?',lowercase(strInput))>0 do
      begin
        try
          posStart:=pos('=?utf-8?b?',lowercase(strInput));
          posEnd:=pos('?=',lowercase(strInput));
          strDecode:=strDecode+copy(strInput,1,posStart-1);
          tmp:=copy(strInput,posStart+10,posEnd-posStart-11);//原来写成了posStart+11,因为代码是从解GB2312那个过程拷贝过来的,没注意改,改成posStart+10就行了
          strDecode:=strDecode+DecodeString(tmp);
          strInput:=copy(strInput,posEnd+2,length(strInput)-posEnd-1);
        finally
          Application.ProcessMessages;
        end;
      end;
      strDecode := strDecode + strInput;
      result := strDecode;
    end;