1,用一下代码解码:unit MyMailBase64;interfaceuses
  Windows, SysUtils, StrUtils, Classes;
function GetMailTitle(const Value: string): string;
function GetMailSender(const value: string): string;
function MailDeCode(const src: string): string;
function Base64Decode(mCode: string): string;
const
  cBase64: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';implementationprocedure StrToStrList(sign: string; str: string; var StrList: TStringList);
var
  temp: string;
begin
  StrList.Text := '';
  while Pos(sign, str) > 0 do
  begin
    temp := Copy(str, 1, Pos(sign, str) - 1);
    Delete(str, 1, Pos(sign, str) + Length(sign) - 1);
    StrList.Add(temp);
  end;
  if Length(str) > 0 then
    StrList.Add(str);
end;function MailDeCode(const src: string): string;
var
  i, Pos1, pos2, pos3: Integer;
  post: string;
  srclist: Tstringlist;
  function Decoder(const s: string): string;
  var
    s1, s2, s3: Integer;
    t, v: string;
    Encoding: Char;
    hex, step: Integer;
    a1: array[1..4] of Byte;
    b1: array[1..4] of Byte;
    j: Integer;
    byte_ptr, real_bytes: Integer;
  begin
    s1 := Pos('=?', s);
    s2 := 1;
    hex := 0;
    if s1 > 0 then
      for s2 := Length(s) - 1 downto 1 do
        if Copy(s, s2, 2) = '?=' then Break;
    if (s1 = 0) or (s2 = 1) then
    begin
      Result := s;
      Exit;
    end;
    t := Copy(s, s1 + 2, s2 - s1 - 2);
    s3 := Pos('?', t);
    Delete(t, 1, s3);
    if t = '' then
    begin
      Result := s;
      Exit;
    end;
    Encoding := t[1];
    Delete(t, 1, 2);
    v := '';
    step := 0;
    case Encoding of
      'Q':
        while t <> '' do
        begin
          case step of
            0: begin
                case t[1] of
                  '_': v := v + ' ';
                  '=': step := 1;
                else
                  v := v + t[1];
                end;
              end;
            1: begin
                if t[1] <= '9' then hex := (Ord(t[1]) - ord('0')) * 16
                else hex := (Ord(t[1]) - 55) * 16;
                step := 2;
              end;
            2: begin
                if t[1] <= '9' then hex := hex + (Ord(t[1]) - Ord('0'))
                else hex := hex + Ord(t[1]) - 55;
                step := 0;
              end;
          end;
          Delete(t, 1, 1);
        end;
      'B': begin
          byte_ptr := 0;
          for j := 1 to Length(t) do
          begin
            Inc(byte_ptr);
            case t[j] of
              'A'..'Z': a1[byte_ptr] := Ord(t[j]) - 65;
              'a'..'z': a1[byte_ptr] := Ord(t[j]) - 71;
              '0'..'9': a1[byte_ptr] := Ord(t[j]) + 4;
              '+': a1[byte_ptr] := 62;
              '/': a1[byte_ptr] := 63;
              '=': a1[byte_ptr] := 64;
            end;
            if byte_ptr = 4 then
            begin
              byte_ptr := 0;
              real_bytes := 3;
              if a1[1] = 64 then real_bytes := 0;
              if a1[3] = 64 then begin
                a1[3] := 0;
                a1[4] := 0;
                real_bytes := 1;
              end;
              if a1[4] = 64 then begin
                a1[4] := 0;
                real_bytes := 2;
              end;
              b1[1] := a1[1] * 4 + (a1[2] div 16);
              b1[2] := (a1[2] mod 16) * 16 + (a1[3] div 4);
              b1[3] := (a1[3] mod 4) * 64 + a1[4];
              if real_bytes > 0 then
                v := v + chr(b1[1]);
              if real_bytes > 1 then
                v := v + chr(b1[2]);
              if real_bytes > 2 then
                v := v + chr(b1[3]);
            end;
          end;
        end;
    end;
    Result := Copy(s, 1, s1 - 1) + v + Copy(s, s2, Length(s));
    if Pos('***SPAM***', Result) > 0 then
      Result := Copy(Result, Pos('-', Result) + 1, Length(Result));
      //Result := Copy(Result, Pos('-', Result) + 1, Length(Result) - pos('-', Result));
  end;
begin
  Pos1 := Pos('=?', src);
  pos2 := 1;
  if Pos1 > 0 then
    for pos2 := Length(src) - 1 downto 1 do
      if Copy(src, pos2, 2) = '?=' then Break;
  if (Pos1 = 0) or (pos2 = 1) then begin
    Result := src;
    Exit;
  end;
  post := Copy(src, Pos1 + 2, pos2 - 2 - pos1);
  pos3 := Pos('?', post);
  Delete(post, 1, pos3);
  if post = '' then
  begin
    Result := src;
    Exit;
  end;
  srclist := TStringList.Create;
  try
    srclist.Clear;
    StrToStrList('   ', src, srclist);
    Result := '';
    for i := 0 to srclist.Count - 1 do
    begin
      post := srclist.Strings[i];
      Result := Result + Decoder(post);
    end;
  finally
    srclist.Free;
  end;
  //去掉最后的'?='
  if Pos('?=', Result) > 0 then
    Delete(Result, Pos('?=', Result), 2);
end;function QuotedPrintableEncode(mSource: string): string;
var
  I, J: Integer;
begin
  Result := '';
  J := 0;
  for I := 1 to Length(mSource) do begin
    if mSource[I] in [#32..#127, #13, #10] - ['='] then begin
      Result := Result + mSource[I];
      Inc(J);
    end else begin
      Result := Result + '=' + IntToHex(Ord(mSource[I]), 2);
      Inc(J, 3);
    end;
    if mSource[I] in [#13, #10] then J := 0;
    if J >= 70 then begin
      Result := Result + #13#10;
      J := 0;
    end;
  end;
end; {   QuotedPrintableEncode   }

解决方案 »

  1.   


    function QuotedPrintableEncode(mSource: string): string;
    var
      I, J: Integer;
    begin
      Result := '';
      J := 0;
      for I := 1 to Length(mSource) do begin
        if mSource[I] in [#32..#127, #13, #10] - ['='] then begin
          Result := Result + mSource[I];
          Inc(J);
        end else begin
          Result := Result + '=' + IntToHex(Ord(mSource[I]), 2);
          Inc(J, 3);
        end;
        if mSource[I] in [#13, #10] then J := 0;
        if J >= 70 then begin
          Result := Result + #13#10;
          J := 0;
        end;
      end;
    end; {   QuotedPrintableEncode   }function QuotedPrintableDecode(mCode: string): string;
    var
      I, J, L: Integer;
    begin
      Result := '';
      J := 0;
      mCode := AdjustLineBreaks(mCode);
      L := Length(mCode);
      I := 1;
      while I <= L do begin
        if mCode[I] = '=' then begin
          Result := Result + Chr(StrToIntDef('$' + Copy(mCode, I + 1, 2), 0));
          Inc(J, 3);
          Inc(I, 3);
        end else if mCode[I] in [#13, #10] then begin
          if J < 70 then Result := Result + mCode[I];
          if mCode[I] = #10 then J := 0;
          Inc(I);
        end else begin
          Result := Result + mCode[I];
          Inc(J);
          Inc(I);
        end;
      end;
    end; {   QuotedPrintableDecode   }function Base64Encode(mSource: string; mAddLine: Boolean = True): string;
    var
      I, J: Integer;
      S: string;
    begin
      Result := '';
      J := 0;
      for I := 0 to Length(mSource) div 3 - 1 do begin
        S := Copy(mSource, I * 3 + 1, 3);
        Result := Result + cBase64[Ord(S[1]) shr 2 + 1];
        Result := Result + cBase64[((Ord(S[1]) and $03) shl 4) + (Ord(S[2]) shr 4) + 1];
        Result := Result + cBase64[((Ord(S[2]) and $0F) shl 2) + (Ord(S[3]) shr 6) + 1];
        Result := Result + cBase64[Ord(S[3]) and $3F + 1];
        if mAddLine then begin
          Inc(J, 4);
          if J >= 76 then begin
            Result := Result + #13#10;
            J := 0;
          end;
        end;
      end;
      I := Length(mSource) div 3;
      S := Copy(mSource, I * 3 + 1, 3);
      case Length(S) of
        1: begin
            Result := Result + cBase64[Ord(S[1]) shr 2 + 1];
            Result := Result + cBase64[(Ord(S[1]) and $03) shl 4 + 1];
            Result := Result + cBase64[65];
            Result := Result + cBase64[65];
          end;
        2: begin
            Result := Result + cBase64[Ord(S[1]) shr 2 + 1];
            Result := Result + cBase64[((Ord(S[1]) and $03) shl 4) + (Ord(S[2]) shr 4) + 1];
            Result := Result + cBase64[(Ord(S[2]) and $0F) shl 2 + 1];
            Result := Result + cBase64[65];
          end;
      end;
    end; {   Base64Encode   }function Base64Decode(mCode: string): string;
    var
      I, L: Integer;
      S: string;
    begin
      Result := '';
      L := Length(mCode);
      I := 1;
      while I <= L do begin
        if Pos(mCode[I], cBase64) > 0 then begin
          S := Copy(mCode, I, 4);
          if (Length(S) = 4) then begin
            Result := Result + Chr((Pos(S[1], cBase64) - 1) shl 2 +
              (Pos(S[2], cBase64) - 1) shr 4);
            if S[3] <> cBase64[65] then begin
              Result := Result + Chr(((Pos(S[2], cBase64) - 1) and $0F) shl 4 +
                (Pos(S[3], cBase64) - 1) shr 2);
              if S[4] <> cBase64[65] then
                Result := Result + Chr(((Pos(S[3], cBase64) - 1) and $03) shl 6 +
                  (Pos(S[4], cBase64) - 1));
            end;
          end;
          Inc(I, 4);
        end else Inc(I);
      end;
    end; {   Base64Decode   }function GetMailTitle(const Value: string): string;
    var
      iPos: integer;
    begin
      Result := Value;
      if Copy(Value, 1, 2) <> '=?' then
      begin
        Result := Value;
        exit;
      end;
          //'?B?'前面的都要去掉
      iPos := Pos('?B?', Value);
      if iPos = 0 then
      begin
        iPos := Pos('?Q?', Value);
        if iPos = 0 then
        begin
          Result := Value;
          exit;
        end;
        Inc(iPos, 3);
          //最后的'?='也要去掉
        Result := Copy(Value, iPos, Length(Value) - iPos - 1);
        Result := QuotedPrintableDecode(Result);
      end
      else
      begin
        Inc(iPos, 3);
          //最后的'?='也要去掉
        Result := Copy(Value, iPos, Length(Value) - iPos - 1);
        Result := Base64Decode(Result);
      end;
    end;
      //由于发件人是中文+'<[email protected]>',组成的,所以多加了这个函数!function GetMailSender(const value: string): string;
    var
      iPos: integer;
      preStr: string;
      bkStr: string;
    begin
      Result := value;
      if Copy(Value, 1, 2) <> '=?' then
      begin
        Result := Value;
        exit;
      end;
      iPos := Pos('?=   <', Value);
      if iPos = 0 then
      begin
        Result := Value;
        exit;
      end
      else
      begin
        preStr := Copy(Value, 1, iPos + 1);
        bkStr := Copy(Value, iPos + 2, length(Value) - iPos + 2);
        Result := GetMailTitle(preStr) + bkStr;
      end;
    end;
    end.
    2,3服务器开通ftp,用idftp上传下载。
    4顶