前段时间由于业务的需要,需封装一个能群发邮件的dll供系统调用。于是考虑到indy了。Indy 10 version 10.5.8.3。
调用者提供 收信人列表(邮件之间用;分割),邮件主题,附件列表(附件之间用;分割)。
function sendmail(v_mails,v_subject,v_files:PAnsiChar):Integer;stdcall;
var
idsmtp2: TIdSMTP;
idmsg1: TIdMessage;
IdEmailAddressItem: TIdEmailAddressItem;
lst : TStringList;
v_i:Integer;
begin
idsmtp2:=TIdSMTP.Create(nil);
idmsg1 :=TIdMessage.Create(nil);
lst:=TStringList.Create;
lst.Delimiter:=';';
lst.DelimitedText:=string(v_mails);
//添加收信人。
for v_i := 0 to lst.Count-1 do
begin
if Trim(lst[v_i])<>'' then
begin
with idmsg1 do
begin
From.Name:='☆';
From.Address:='☆@163.com';
IdEmailAddressItem := Recipients.Add;
IdEmailAddressItem.Address := lst[v_i]+'@163.com';
IdEmailAddressItem.Name:=lst[v_i];
Subject :=string(v_subject);
end;
end;
end;
lst.Clear;
lst.Delimiter:=';';
lst.DelimitedText:=string(v_files);
//添加附件
for v_i := 0 to lst.Count-1 do
begin
if FileExists(lst[v_i]) then
begin
tidattachmentFile.Create(idmsg1.MessageParts,lst[v_i]);
end;
end;
idsmtp2.AuthType := satDefault;
idsmtp2.Username :='☆☆';
idsmtp2.Password :='☆☆☆';
idsmtp2.Host := 'mail.163.com';
idsmtp2.Port := 25;
try
idsmtp2.Connect;
try
idsmtp2.Send(idmsg1);
Result:=1;
finally
idsmtp2.Disconnect;
end;
except
on e:Exception do
begin
Result:=0;
end;
end;
end;