多线程群发邮件(50)

  • 主题发起人 zhk7324385
  • 开始时间
Z

zhk7324385

Unregistered / Unconfirmed
GUEST, unregistred user!
下面这段群发邮件代码如何改成多线程的,请高手帮忙! with Query1do
begin
Query1.Close;
Query1.SQL.Text := 'SELECT * FROM fasong';
//这里存放的是多个收件人地址 Query1.Open;
while not Query1.Eofdo
//看有多少个收件人的循环,这里怎么改为多线程 begin
//构造邮件 MailMessage.Subject :=ObjectE.Text;//标题 MailMessage.Body.Assign(BodyMemo.Lines);
//正文 MailMessage.From.Address :=Trim(d);
//发信人地址 MailMessage.Recipients.EMailAddresses :=Query1.FieldByName('mail').AsString;
//收信人地址 // mailMessage.MessageParts with MailMessage do
begin
if SlaveLB.Count > 0 then
begin
for k:=0 to SlaveLB.Count - 1 do
TIdAttachment.Create(MailMessage.MessageParts,SlaveLB.Items[k]);
end;
end;
//初始化SMTP属性 IdSMTP.Host :=Trim(a);
//SMTP服务器地址 IdSMTP.Port :=StrToInt(b);
//SMTP服务器端口 // IdSMTP.UserId:='zhk870319@sina.com';
// IdSMTP.Password:='zhk7322541';
IdSMTP.UserId :=Trim(d );
IdSMTP.Password :=Trim(e);
//连接到SMTP服务器 StatusBar1.SimpleText:='准备连接到服务器:'+a;
// MemoInfo.Lines.Add('准备连接到服务器:'+SetMailForm.SMTPE.Text);
Try IdSMTP.Connect ;
except begin
StatusBar1.SimpleText:='无法连接到服务器:'+a;
// MemoInfo.Lines.Add('无法连接到服务器:'+SetMailForm.SMTPE.Text);
end;
end;
//end try //验证身份 if (IdSMTP.AuthSchemesSupported.IndexOf('LOGIN')<>-1) then
begin
//服务器要求验证 StatusBar1.SimpleText:='服务器要求进行身份验证' ;
// MemoInfo.Lines.Add('服务器要求进行身份验证');
IdSMTP.Authen
ticationType :=atLogin;
StatusBar1.SimpleText:='服务器开始进行身份验证' ;
// MemoInfo.Lines.Add('服务器开始进行身份验证');
try if IdSMTP.Authen
ticate then
//通过验证 StatusBar1.SimpleText:='服务器通过身份验证' // MemoInfo.Lines.Add('服务器通过身份验证') else
StatusBar1.SimpleText:='服务器验证失败' ;
// MemoInfo.Lines.Add('服务器验证失败');
except begin
Application.MessageBox('服务器验证失败', '', MB_OK + MB_ICONINFORMATION);
StatusBar1.SimpleText:='服务器验证失败' ;
// MemoInfo.Lines.Add('服务器验证失败');
IdSMTP.Disconnect ;
//异常刚断开连接 end end;
//end try end else
begin
//服务器不要求验证 StatusBar1.SimpleText:='服务器不需要验证' ;
// MemoInfo.Lines.Add('服务器不需要身份验证');
end;
//发送信件 try IdSMTP.Send(MailMessage);
i:=i+1;
t:=t+' '+Query1.FieldByName('mail').AsString;
except StatusBar1.SimpleText:='发送失败' ;
j:=j+1;
f:=f+' '+Query1.FieldByName('mail').AsString;
// MemoInfo.Lines.Add('发送失败');
end;
IdSMTP.Disconnect ;
//发送完后断开连接 query1.Next;
end;
end;
 
群发应到考虑到必须用线程方式,用上面的该显然不是很容易,建议从网上找找群发的代码参照下吧
 
没接触过线程,那么上面的代码要怎么改成线程方式呢?
 
进入全文检索页面,搜索“多线程”[8D]
 
//初始化SMTP属性 IdSMTP.Host :=Trim(a);
//SMTP服务器地址 IdSMTP.Port :=StrToInt(b);
//SMTP服务器端口 IdSMTP.UserId :=Trim(d );
IdSMTP.Password :=Trim(e);
//连接到SMTP服务器 StatusBar1.SimpleText:='准备连接到服务器:'+a;
// MemoInfo.Lines.Add('准备连接到服务器:'+SetMailForm.SMTPE.Text);
Try IdSMTP.Connect ;
except begin
StatusBar1.SimpleText:='无法连接到服务器:'+a;
// MemoInfo.Lines.Add('无法连接到服务器:'+SetMailForm.SMTPE.Text);
Exit;
end;
end;
//end try //验证身份 if (IdSMTP.AuthSchemesSupported.IndexOf('LOGIN')<>-1) then
begin
//服务器要求验证 StatusBar1.SimpleText:='服务器要求进行身份验证' ;
// MemoInfo.Lines.Add('服务器要求进行身份验证');
IdSMTP.Authen
ticationType :=atLogin;
StatusBar1.SimpleText:='服务器开始进行身份验证' ;
// MemoInfo.Lines.Add('服务器开始进行身份验证');
try if IdSMTP.Authen
ticate then
//通过验证 StatusBar1.SimpleText:='服务器通过身份验证' // MemoInfo.Lines.Add('服务器通过身份验证') else
begin
StatusBar1.SimpleText:='服务器验证失败' ;
IdSMTP.Disconnect ;
Exit;
end;
// MemoInfo.Lines.Add('服务器验证失败');
except begin
Application.MessageBox('服务器验证失败', '', MB_OK + MB_ICONINFORMATION);
StatusBar1.SimpleText:='服务器验证失败' ;
// MemoInfo.Lines.Add('服务器验证失败');
IdSMTP.Disconnect ;
//异常刚断开连接 Exit;
end end;
//end try end else
begin
//服务器不要求验证 StatusBar1.SimpleText:='服务器不需要验证' ;
// MemoInfo.Lines.Add('服务器不需要身份验证');
end;
//发送信件 try [red]for q := 0 to MailAddressListMemo.Lines.Count - 1do
begin
//循环发送 //构造邮件 MailMessage.Subject :=ObjectE.Text;//标题 MailMessage.Body.Assign(richedit1.Lines);
//正文 MailMessage.From.Address :=Trim(d);
//发信人地址 MailMessage.Recipients.EMailAddresses :=MailAddressListMemo.Lines.Strings[q];
with MailMessage do
begin
if SlaveLB.Count > 0 then
begin
for k:=0 to SlaveLB.Count - 1 do
TIdAttachment.Create(MailMessage.MessageParts,SlaveLB.Items[k]);
end;
end;
IdSMTP.Send(MailMessage);
Application.ProcessMessages;
end;
//这块循环为什么只能发到第一个邮箱,第二个时就报错 [/red] except StatusBar1.SimpleText:='发送失败' ;
IdSMTP.Disconnect ;
Exit;
end;
IdSMTP.Disconnect ;
//发送完后断开连接
 
可以考虑并发线程发送,一个线程发一封.比如,每次启动50个线程,每10秒启动一次.我就是这样做的,效果很好.
 
就是不会多线程啊,谁给改改啊
 
不会多线程?自己看书去。
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
926
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
顶部