用indy处理html格式邮件时遇到的问题,谁能帮我解决.(300分)

  • 主题发起人 主题发起人 xksyhjb
  • 开始时间 开始时间
X

xksyhjb

Unregistered / Unconfirmed
GUEST, unregistred user!
用indy的Tidmessage,idpop3,Tidsmtp

**********以下为发送部分***************

.以html格式写正文TStrings.如:
Memo1.Lines.Add('<html>'
Memo1.Lines.Add('<head>');
Memo1.Lines.Add('<title>Untitled Document</title>');
Memo1.Lines.Add('<meta http-equiv="Content-Type" content="text/html; charset=gb2312">');
Memo1.Lines.Add('</head>'
...
idmessage1.contenttype:='text/html'
IdMessage1.Clear;
IdMessage1.Body.Assign(Memo1.Lines).


IdMessage1.Recipients.EMailAddresses :=接收地址;
IdMessage1.From.Address:=发送地址;
//
IdSMTP1.Host:=smtp.163.net;
IdSMTP1.Port:=25;
IdSMTP1.UserId:=你的邮箱用户名(如yyy@163.net,写yyy即可);
IdSMTP1.Password:=你的邮箱口令;
IdSMTP1.Authenticationtype:=atLogin;(是否需要验证,现在一般的都要)
IdSMTP1.Connect;
try
IdSMTP1.Send(IdMessage1);
finally
IdSMTP1.Disconnect;
end;

************以下为接收部分***************

idPOP3.Retrieve(Fmsgindex, Msg);
...
...

for MsgPart := 0 to pred(Msg.MessageParts.Count) do
begin
if (Msg.MessageParts.Items[MsgPart] is TIdAttachment) then
begin
.....
.....
end
else
begin
if Msg.MessageParts.Items[MsgPart] is TIdText then
begin
.....
......
end;
end;

*****************************问题部分**************************
问题1.
在发送时,我如果我不加下面这句
idmessage1.contenttype:='text/html';
则foxmail,outlook等不能正确识别邮件内容,显示的是html源码
加了idmessage1.contenttype:='text/html'以后,如果邮件不带附件,foxmail,outlook还
可以识别,一旦加了附件后,foxmail,outlook显示的又是html源码.

问题2.
接上,如果我加发送时加了idmessage1.contenttype:='text/html';
我自已用idpop3接收时
for MsgPart := 0 to pred(Msg.MessageParts.Count) do
begin
if (Msg.MessageParts.Items[MsgPart] is TIdAttachment) then
begin
.....
.....
end
else
begin
if Msg.MessageParts.Items[MsgPart] is TIdText then
begin
.....
......
end;
end;

如果我加发送时不加idmessage1.contenttype:='text/html';
我自已用idpop3接收时

以上代码可以正常执行,在IDTEXT部分可以提取到html文本


上述问题非常着急,分不够还可再加.
 
自己提前
 
强烈关注
 
我知道这问题有点难度,好多高水平的dfw都不知哪去了.
 
澄清一下问题:
电子邮件分两部分,Header和Body,用邮件客户端(foxmail,outlook等)阅读邮件时
程序会自动根据Header部分的Content-Type字段来判断Body部分是什么东东,按对应的
方式来显示邮件,跟用什么东西发的邮件没关系。
问题1:如果不指明Content-Type是text/html,邮件客户端会用默认的text/plain
即纯文本方式显示邮件,所有你看到的是HTML源码;
问题2:有附件时,Content-Type应该是multipart/...,其中...可能是mixed,alternative,
related等,具体看看RFC。凡是multipart,还要指明分割各个部分的boundary,Body部分用
Mime编码,也看看RFC吧
 
to sofox

那么indy在发送html邮件时是把邮件正文按文本还是按附件发送呢?

在设contonttype='text/html'后,为什么我在接收时body部分为空?


 
“邮件正文”是什么?HTML部分?
实际上邮件正文(即Body)只有一个,但是可以由多个部分安装Mime编码方式包装在一起,
没一个部分都可以指明Content-Type。
例如,下面的邮件包括两个部分,一个是一段html,另一个部分是附件test.txt

Content-Type: multipart/alternative;
boundary="==123456==";

--==123456==
Content-Type: text/html;

<html>
<body>This is test html part</body>
</html>

--==123456==
Content-Type: application/octet-stream;
name="test.txt"
Content-Disposition: attachment;
filename="test.txt"

This is content of test.txt

--==123456==--

还是找RFC看看吧,如果你E文不错
 
to sofox

RFC我粗略浏览了一下,如果用socket编程实现起来太烦,我现在只想用indy来做,

老大,能不能给个例程(收、发html邮件+附件)。

先谢了.

解决问题另有高分相送,来昆明玩我做东.
:)
:)

 

发邮件单元

SeSkinLabel1.Caption := selfsendmsg.accont;
SeSkinLabel2.Caption := selfsendmsg.Host;
Smtp.Host := selfsendmsg.Host;
smtp.Port := selfsendmsg.Port;
smtp.AuthenticationType := selfsendmsg.Auth;
smtp.Username := selfsendmsg.UserName;
smtp.Password := selfsendmsg.UserPwd;

msg.ContentType := 'multipart/alternative'; //default style

msg.Clear;
msg.Body.Assign(selfsendmsg.Body);
msg.Subject := selfsendmsg.Subject;
msg.From.Text := selfsendmsg.FromAdd;
msg.Recipients.EMailAddresses := selfsendmsg.Toadd;
msg.Priority := TIdMessagePriority(selfsendmsg.Priority);
msg.CCList.EMailAddresses := selfsendmsg.Cc;
msg.BccList.EMailAddresses := selfsendmsg.Bcc;
msg.ReceiptRecipient.Text := selfsendmsg.Receipt;
msg.IsEncoded := true;
if selfsendmsg.Attach.Count > 0 then
for I := 0 to selfsendmsg.Attach.Count - 1 do // Iterate
TIdAttachment.Create(msg.MessageParts, selfsendmsg.Attach);
SMTP.Connect;
try
SMTP.Send(msg);
finally
SMTP.Disconnect;
end;
end; // with
end;


收邮件单元
if POP.Connected then
begin
POP.Disconnect;
end;
POP.Host := revertbox.pop3;
POP.Port := revertbox.pop3port;
POP.Username := revertbox.userid;
POP.Password := revertbox.userpwd;
POP.Connect;
FMsgCount := POP.CheckMessages;
FMailBoxSize := POP.RetrieveMailBoxSize div 1024;
try
if FMsgCount > 0 then
begin
for I := 1 to FMsgCount do // Iterate
begin
Fmsgindex := i;
Synchronize(fillmailheader);
end;
synchronize(resettreeview);
end
else
close;
finally
pop.Disconnect;
end;
end; // with
end;
procedure TrevertThread.fillmailheader;
var
mailcotion: string;
I: Integer;
MsgPart, attachcount: integer;
attachpath, attachfile, mailfile, sqlstr, mailsize: string;
itm: TListItem;
Fs: TFilestream;
ole, o2: OleVariant;
msginfo: array[1..10] of string;
begin
// with TfrmMDIChild(m.MDIChildren[m.getchildindex('邮件')]) do
with Frevertform, Foaform do
begin
SeSkinLabel1.Caption := revertbox.accont;
SeSkinLabel2.Caption := revertbox.pop3;
addmail.Filtered := true;
attach.Filtered := true;
addmail.Open;
attach.Open;
lv_header.Items.Clear;
{for MailIndex := 1 to Fmsgcount do
begin
} SeSkinLabel4.Caption := '第 ' + inttostr(Fmsgindex) + ' 封/共 ' + IntToStr(Fmsgcount) + ' 封';
attachcount := 0;
//
Msg.Clear;
msg.ContentType := 'text/html';
pop.RetrieveHeader(Fmsgindex, Msg);
seskinlabel5.Caption := pub.fun_CheckTxt(Msg.Subject) + '...';
with recevedt do
begin
close; sql.Clear;
sql.Add('select mailid from mailcontain where mailid=''' + msg.MsgId + '''');
prepared := true;
open;
if recordcount > 0 then
exit;
end; // with
mailsize := formatfloat('0.00', POP.RetrieveMsgSize(Fmsgindex) / 1024) + 'K';
Application.ProcessMessages;
POP.Retrieve(Fmsgindex, Msg);
itm := lv_header.Items.Insert(0);
itm.ImageIndex := 0;
itm.Caption := '';
itm.SubItems.Add(Msg.From.Text);
itm.SubItems.Add(pub.fun_CheckTxt(Msg.Subject));
itm.SubItems.Add(DatetimeToStr(Msg.Date));
itm.SubItems.Add(mailsize);
itm.SubItems.Add(msg.MsgId);
SeSkinPanel1.Visible := False;
//测试用
msginfo[5] := pub.fun_CheckTxt(Msg.Subject);
msginfo[1] := msg.MsgId;
msginfo[2] := inttostr(Msg.MessageParts.Count);
// showmessage(inttostr(Msg.MessageParts.Count));
for MsgPart := 0 to pred(Msg.MessageParts.Count) do
begin
if (Msg.MessageParts.Items[MsgPart] is TIdAttachment) then
begin
attachfile := pub.fun_CheckTxt(TIdAttachment(Msg.MessageParts.Items[MsgPart]).Filename);
attachpath := attachdir + '/' + copy(msg.MsgId, 2, length(msg.MsgId) - 2) + '/';
createdir(attachpath);
TIdAttachment(Msg.MessageParts.Items[MsgPart]).SaveToFile(attachpath + attachfile);
SeSkinPanel1.Visible := true;
ShellListView1.Root := attachpath;
ShellListView1.ReadOnly := true;
shelllistview1.Refresh;
attach.Append;
inc(attachcount);
Fs := TFileStream.create(attachpath + attachfile, fmOpenRead);
TBlobField(attach.FieldbyName('attach')).LoadFromStream(fS);
attach['attachheader'] := attachfile;
attach['mailid'] := msg.MsgId;
fs.Destroy;
attach.Post;
end
else
begin
if Msg.MessageParts.Items[MsgPart] is TIdText then
begin
{ richedit1.Lines.Clear;
richedit1.Lines.AddStrings(TIdText(Msg.MessageParts.Items[MsgPart]).Body);
}
//o2 := False;
createdir(attachdir + '/' + copy(msg.MsgId, 2, length(msg.MsgId) - 2) + '_mail/');
mailfile := attachdir + '/' + copy(msg.MsgId, 2, length(msg.MsgId) - 2) + '_mail/' + 'mail.html';
//richedit1.Lines.SaveToFile(mailfile);
TIdText(Msg.MessageParts.Items[MsgPart]).Body.SaveToFile(mailfile);
{ ole := mailfile;
mailedit.Navigate(mailfile);}
end
end;
end;
{ mailcotion := '';
for I := 0 to pred(richedit1.Lines.Count) do // Iterate
begin
mailcotion := mailcotion + richedit1.Lines;
end; // for
if not (pos('content="text/html', mailcotion) > 0) then
begin
mailcotion := '';
for I := 0 to pred(richedit1.Lines.Count) do // Iterate
begin
mailcotion := mailcotion + richedit1.Lines + #13#10;
end; // for
end;}
addmail.Append;
addmail['accontid'] := inttostr(revertbox.accontid);
addmail['box'] := '收件箱';
addmail['cc'] := Msg.CCList.EMailAddresses;
addmail['mailDate'] := Msg.Date;
addmail['mailfrom'] := Msg.From.Text;
addmail['mailid'] := msg.MsgId;
addmail['mailOrganization'] := Msg.Organization;
addmail['mailPriority'] := IntToStr(Ord(Msg.Priority));
addmail['readtag'] := '否';
addmail['Receipt'] := Msg.ReceiptRecipient.Text;
addmail['recevetag'] := '否';
addmail['subject'] := Msg.Subject;
addmail['mailto'] := Msg.Recipients.EmailAddresses;
// addmail['mailcontain'] := mailcotion;
addmail['mailsize'] := mailsize;
addmail['attachcount'] := attachcount;
Fs := TFileStream.create(mailfile, fmOpenRead);
TBlobField(addmail.FieldbyName('mailcontain')).LoadFromStream(fS);
fs.Destroy;
addmail.Post;
{
sqlstr := 'insert into mailcontain(accontid,box, cc, mailDate, mailfrom, mailid, mailOrganization, mailPriority, readtag, Receipt, recevetag, subject, mailto,mailcontain) values(' +
inttostr(revertbox.accontid) + ',''收件箱'',''' + Msg.CCList.EMailAddresses + ''',''' + datetimetostr(Msg.Date) + ''',''' + Msg.From.Text + ''',''' + msg.MsgId +
''',''' + Msg.Organization + ''',''' + IntToStr(Ord(Msg.Priority) + 1) + ''',''否'',''' + Msg.ReceiptRecipient.Text + ''',''否'',''' + Msg.Subject + ''',''' +
Msg.Recipients.EmailAddresses + ''',''' + mailcotion + ''')';
recevedt.Close;
recevedt.SQL.Clear;
recevedt.SQL.Add(sqlstr);
recevedt.Prepared := true;
recevedt.ExecSQL;}
if attachcount > 0 then
begin
itm.ImageIndex := 1;
end;
end;

end;

procedure Toa_revertmail_frm.POPWork(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCount: Integer);
begin
z.Position := aworkcount;
end;

procedure Toa_revertmail_frm.POPWorkBegin(Sender: TObject;
AWorkMode: TWorkMode; const AWorkCountMax: Integer);
begin
z.Max := aworkcountmax;
end;

end.

















======= 2003-05-09 15:11:00 您在来信中写道:=======

>把你的程序发过来吧
>----- Original Message -----
>From: "Han-JiaBo" <laohan@vip.sina.com>
>To: "赵振华" <zhao-zhenhua@163.net>
>Sent: Friday, May 09, 2003 1:00 PM
>Subject: Re: 朋友,你好
>
>
>赵振华,您好!
>
>首先感谢你的回信.
>
> 以下是我测试的代码:
>
>用indy的Tidmessage,idpop3,Tidsmtp
>
>**********以下为发送部分***************
>
>以html格式写正文TStrings.如:
> Memo1.Lines.Add('<html>'
> Memo1.Lines.Add('<head>');
> Memo1.Lines.Add('<title>Untitled Document</title>');
> Memo1.Lines.Add('<meta http-equiv="Content-Type" content="text/html; charset=gb2312">');
> Memo1.Lines.Add('</head>'
> ...
>idmessage1.contenttype:='text/html'
>IdMessage1.Clear;
> IdMessage1.Body.Assign(Memo1.Lines).
>
>
> IdMessage1.Recipients.EMailAddresses :=接收地址;
> IdMessage1.From.Address:=发送地址;
> //
> IdSMTP1.Host:=smtp.163.net;
> IdSMTP1.Port:=25;
> IdSMTP1.UserId:=你的邮箱用户名(如yyy@163.net,写yyy即可);
> IdSMTP1.Password:=你的邮箱口令;
> IdSMTP1.Authenticationtype:=atLogin;(是否需要验证,现在一般的都要)
> IdSMTP1.Connect;
> try
> IdSMTP1.Send(IdMessage1);
> finally
> IdSMTP1.Disconnect;
> end;
>
>************以下为接收部分***************
>
> idPOP3.Retrieve(Fmsgindex, Msg);
>.
>.
>
> for MsgPart := 0 to pred(Msg.MessageParts.Count) do
> begin
> if (Msg.MessageParts.Items[MsgPart] is TIdAttachment) then
> begin
> .....
> .....
> end
> else
> begin
> if Msg.MessageParts.Items[MsgPart] is TIdText then
> begin
> .....
> ......
> end;
> end;
>
>*****************************问题部分**************************
>问题1.
>在发送时,我如果我不加下面这句
>idmessage1.contenttype:='text/html';
>则foxmail,outlook等不能正确识别邮件内容,显示的是html源码
>加了idmessage1.contenttype:='text/html'以后,如果邮件不带附件,foxmail,outlook还
>可以识别,一旦加了附件后,foxmail,outlook显示的又是html源码.
>
>问题2.
>接上,如果我在发送时加了idmessage1.contenttype:='text/html';
>我自已用idpop3接收时
> for MsgPart := 0 to pred(Msg.MessageParts.Count) do
> begin
> if (Msg.MessageParts.Items[MsgPart] is TIdAttachment) then
> begin
> .....
> .....
> end
> else
> begin
> if Msg.MessageParts.Items[MsgPart] is TIdText then
> begin
> .....
> ......
> end;
> end;
>以上语句不执行,变量 Msg.MessageParts.Count为0
>
>如果我加发送时不加idmessage1.contenttype:='text/html';
>我自已用idpop3接收时
>
>以上代码可以正常执行,在body可提取到idtext部分可以提取到html文本
>
>
> 以上问题请帮我查看并帮助解决一下.
>
>        致
>礼!
>
>
>        Han-JiaBo
>        laohan@vip.sina.com
>          2003-05-09

= = = = = = = = = = = = = = = = = = = =


        致
礼!


        Han-JiaBo
        laohan@vip.sina.com
          2003-05-09
 
多人接受答案了。
 
后退
顶部