procedure TSendMailf.BtnSendClick(Sender: TObject);
var inifile:tinifile;
sSender:string; //发件人地址
i,j,iCount:integer;
sReceipter,sAddress:string;//收件人和地址
sBody : string; //邮件内容,有替代
begin
Unsend.Lines.Clear;
//1.删除空行
i:=0;
while (i<self.txtRecipters.Lines.Count) do
begin
if self.txtRecipters.Lines.Strings = '' then
begin
self.txtRecipters.Lines.Delete(i);
i:=i-1;
end;
i:=i+1;
end;
if self.txtRecipters.Lines.Count = 0 then
begin
showmessage('没有收件人!');
abort;
end;
//1 总进度
self.ProgressBar.Max := self.txtRecipters.Lines.Count;
self.ProgressBar.Step :=1;
self.ProgressBar.Position :=0;
//1.读取配置文件
inifile:=tinifile.Create(extractfilepath(application.exename)+'/setting.ini');
try
self.IdSMTP.AuthenticationType := atLogin;
self.IdSMTP.Host := inifile.ReadString('Setting','HostName','');
self.IdSMTP.Port := inifile.ReadInteger('Setting','port',25);
self.IdSMTP.Username := inifile.ReadString('Setting','UserName','');
self.IdSMTP.Password := inifile.ReadString('Setting','Password','');
sSender := inifile.ReadString('Setting','Sender','');
finally
inifile.free;
end;
//2.构建Idmessage
self.IdMessage.Subject := trim(self.txtTitle.Text);
self.IdMessage.From.Text := sSender;
//3.添加附件
for i:=0 to self.ListView.Items.Count-1 do
begin
TidAttachment.Create(idMessage.MessageParts ,self.ListView.Items.Caption);
end;
//
iCount := 0;
try
IdSMTP.Connect(5000);
for i:=0 to self.txtRecipters.Lines.Count -1 do
begin
sReceipter := '';
sAddress := '';
sBody := self.txtBody.Lines.Text;
sReceipter := copy(self.txtRecipters.Lines.Strings,1,pos(',',self.txtRecipters.Lines.Strings)-1);
sAddress := copy(self.txtRecipters.Lines.Strings,length(sReceipter)+2,length(self.txtRecipters.Lines.Strings));
if sReceipter='' then sReceipter := copy(sAddress,1,pos('@',sAddress)-1); //修正receipter;
self.IdMessage.Recipients.EMailAddresses := sAddress;
sBody := stringreplace(sBody,'[Receipter]',sReceipter,[rfReplaceAll]);
self.IdMessage.Body.Text := sBody;
try
IdSMTP.Send(IdMessage);
Unsend.Lines.Add(self.txtRecipters.Lines.Strings);
iCount := iCount +1;
except
raise;
//错误地址以后处理
end;
self.ProgressBar.Position := self.ProgressBar.Position + self.ProgressBar.Step;
end;
finally
IdSMTP.Disconnect;
end;
//
showmessage('发送完成!');
end;