接收邮件为何主题中中文为乱码
2007-10-22 11:55
211 查看
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 GetTitle(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;
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 GetTitle(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;
相关文章推荐
- Indy接收邮件中文无编码乱码问题
- openerp 7.0邮件接收中文附件乱码问题解决办法
- 使用Formail发送html格式的中文主题邮件,解决乱码问题等
- Linux环境下邮件主题、发件人中文乱码解决
- python 接收邮件(下载附件,解决中文乱码)自测OK
- JavaMail发送中文邮件中主题乱码的解决方法
- 转 JavaMail发送中文邮件中主题乱码的解决方法
- 在Linux下使用C++发送中文邮件,而用outlook接收是乱码
- centos5下sendmail发送邮件时主题中文乱码的解决办法
- JavaMail发送中文邮件中主题乱码的解决方法
- 调整 Thunderbird 邮件主题和正文的字体、字号,修正中文显示乱码
- 邮件主题、别名中文乱码问题
- JavaMail发送中文邮件中主题乱码的解决方法
- Servlet接收包含中文的url出现乱码
- Jenkins 通过邮件模板发送 html 片段,中文出现乱码
- 用ORACLE 存储过程发送HTML邮件,支持文本和HTML两种格式,增加防中文乱码
- 解决jquery ajax接收后台返回值出现中文乱码问题
- php接收GET方法传入的中文参数乱码深究
- URL参数带中文,后台接收乱码解决方案
- 为什么页面接收到的中文是乱码?