发邮件单元
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