delphi 读邮件乱码

作者&投稿:班斩 (若有异议请与网页底部的电邮联系)
delphi读取txt时候的中文乱码,该怎么处理~

通常来说,txt 是纯文本格式,也是最容易读取的,如果读取是乱码,可能的原因是由于 txt 的字符集格式设置不相符。

delphi 7 读取的 txt 是用 ansi 格式读取的,而 delphi 2007 及以上版本使用 utf-8 来读取。

建议:

1、检查 txt 文件的编码格式,将之修改与 delphi 使用字符集一致。
2、可以尝试使用 Utf8toAnsi、AnsitoUtf8 等函数进行编码转换。

你是指网页上还是Delphi上显示乱码,网页上的话也要设置相应页面为UTF8,


如果是DELPHI上要显示uft8的话,取出的内容用函数转换下
function unicode2gb( unicodestr:string):string;
var
SourceLength:integer;
DoneLength:integer;
AscNo:integer;
Byte1,Byte2,Byte3:integer;
GbStr:string;
begin
result:='';
if Trim(unicodestr)='' then exit;

SourceLength:=Length(UnicodeStr);
DoneLength:=1;
repeat
AscNo:=ord(UnicodeStr[DoneLength]);
case (AscNo and $E0) of
$E0:begin
Byte1:=(AscNo and $0f) shl 12;
Inc(DoneLength);
if DoneLength>SourceLength then break;
AscNo:=ord(UnicodeStr[DoneLength]);
Byte2:=(AscNo and $3f) shl 6;
Inc(DoneLength);
if DoneLength>SourceLength then break;
AscNo:=ord(UnicodeStr[DoneLength]);
Byte3:=AscNo and $3f;
end;
$C0:begin
Byte1:=(AscNo and $1f) shl 6;
Inc(DoneLength);
if DoneLength>SourceLength then break;
AscNo:=ord(UnicodeStr[DoneLength]);
Byte2:=(AscNo and $3f);
Byte3:=0;
end;
0..$bf:begin
Byte1:=AscNo;
Byte2:=0;
Byte3:=0;
end;
end; //case;
GbStr:=GBStr+widechar(Byte1+Byte2+Byte3);
Inc(DoneLength);
if DoneLength>SourceLength then break;
until DoneLength>SourceLength;
result:=GbStr;
end;


不麻烦呀,函数你复制我上面写的这个就行了。你输出时调用下就行了。比如你Delphi上定义两变量,s1,s2;取的内容先放到一个变量s1上,显示时输出S2,用我的函数调用下就行了,
var
s1,s2 : string;
begin
s2 := unicode2gb(s1);
end;

邮件解码函数
function CheckTxt(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..3] of byte;
j: integer;
byte_ptr,real_bytes: integer;
tempedit:TEdit;
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-2-s1);
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;
v:=v+Chr(hex);
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+2,999);

tempedit:=TEdit.Create(nil);
try
tempedit.Width:=0;
tempedit.Height:=0;
tempedit.Visible:=True;
tempedit.Text:=Result;
Result:=tempedit.Text;
finally
FreeAndNil(tempedit);
end;
if Pos('***SPAM***',Result)>0 then
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;
end;

你选择编码模式为,简体中文,, 如果不行,再试试繁体中文,就行了,,没有比这更好的办法了

还是编码的问题,你看看这个组件有没有设置编码格式的属性.

先对读取的内容进行编码转换,如ansistring
然后再用GB3213显示

因为中文在软件中一般只支持UTF和ANSI码


开封县18569339838: delphi idPop3如何处理电子邮件标题的乱码问题??
屠亨京都: 是用了Base64编码,论坛中搜出个Base解码的例子,把=?GB2312之后的字符解一下就行了

开封县18569339838: Delphi里面的fastreport乱码了怎么办 -
屠亨京都: 换行乱码 主要改fr_class.pas中的wrapline过程,其中有一段: else if s[last] = thenOutLine(Copy(s, beg, last - beg))else OutLine(Copy(s, beg, last - beg 1)).//造成乱码的根本原因 改为 else if s[last] = then OutLine(Copy(s, beg, last - beg))else if ...

开封县18569339838: Delphi 编写的软件,文字乱码 -
屠亨京都: 这个是中西文字符,在中文系统里的硬伤啊 解决办法是在Delphi2007以后的系统里把程序再编译一下就行了. 因为2007以前的字符集是Ansi,2007以后的才是完全兼容中文系统的.好象在Delphi7里,把String改成WideString,可以解决这些问题.

开封县18569339838: DELPHI2010显示中文乱码 -
屠亨京都: delphi2007 之后已经启用了unicode 编码,俗称长编码;你以前在DELPHI 7 中写的程序,用它来打开是有可能出现中文乱码;你需要转换工程,ANSI 转UNICODE 码工都懂的,我就不多说了;而且DELPHI 2007 写的程序中输出的中文,在显示时也可能在一些操作系统(非简体中文)出现乱码,因此建议整个工程编码过程中就要注意采用统一的UTF8

开封县18569339838: delphi xe2 显示中文乱码,该怎么解决 -
屠亨京都: delphi xe2 显示中文乱码 gb2312了.AResponseInfo.CharSet:='gb2312'; ARequestInfo.CharSet:='gb2312'; 然后接受的信息 AnsiString(ARequestInfo.Params[i])

开封县18569339838: delphi sendmessage乱码
屠亨京都: 把两个byte组合成word型 假如 byte(sentence[i]) = 1 ,byte(sentence[i-1])也等于1 第一个参数为低位字节,第二个为高位字节, A or B shl 16,二进制为0000000100000001 函数的结果是257

开封县18569339838: Delphi传入参数出现乱码,Delphi接收到传入的xml文件字符串,出现乱码,求解. -
屠亨京都: 编码问题 用utf8toansi转换一下就好了

开封县18569339838: delphi读取txt时候的中文乱码,该怎么处理 -
屠亨京都: 能提供些你的文件和代码,好根据你的文件修改代码,,空口不好理解和针对性处理

开封县18569339838: delphi用mscomm做串口通讯 接收到数据是乱码 怎么办啊? -
屠亨京都: 出现乱码可能 1 上位机下位机参数设置不一致 波特率 校验== 2 mscomm控件属性 设置 具体看下设置一下控件属性7)InputMode属性 void SetInputMode(long nNewValue); long GetInputMode(); 访属性用于设置或者返回传输数据的类型.其取值和基本含义如下表所示. 设定值 值 描述 ComInputModeText(缺省) 0 通过Input属性以文本方式取回数据 ComInputModeBinary 1 通过Input属性以二进制方式取回数据

开封县18569339838: 关于DELPHI 用SOCKET 发消息给JAVA,JAVA显示乱码的问题已用UTF - 8但是还是解决不了.......... -
屠亨京都: delphi使用utf8encode转换一下再发

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网