The example in this article describes the method of sending mass Email messages in Delphi7. Share it with everyone for your reference. The specific analysis is as follows:
During this period, candidate information and exam information need to be sent to all candidates taking the exam via email. After many debuggings, it is considered a success. I am sending it for your reference:
The general idea is:
1. Install a LAN version of the mail server , and set up DNS so that the server can send emails to users outside the network. There are many software in this area, such as WinWebMail, which is good and can be downloaded from the official website;
2. The controls that need to be used: Indy10.0.15, which can be downloaded through Baidu search;
3. Set the email server and account information to be sent
Copy the code as follows: function setEmailInfo:integer; //Return value 0: Email setting failed; 1: Email setting successful
var
selectStr:string;
thisresult:integer;
begin
thisresult := 0;
//Set up account
IdSMTP1.AuthType := atNone; //or atSASL;
IdSMTP1.Host := hostString;
IdSMTP1.Username := userNameString;
IdSMTP1.Password := passWordString;
try
IdSMTP1.connect;
thisresult := 1;
if not IdSMTP1.Authenticate then
begin
showmessage('Sending email account verification failed! Please check SMTP account settings!');
thisresult := 0;
end
except
showmessage('SMTP server connection failed! Please check whether the SMTP account settings and network are normal!');
thisresult := 0;
end
end;
result := thisresult;
end;
4. Send a message by email as an independent function, and delay control is required when sending.
Copy the code as follows: procedure sendEmailOnce(emailusername:string;
formAddress:string;receiptRecipientAddress:string;sendtoAdd:string;emailSubject:string);
begin
MsgKsbkxx.From.Name := emailusername; //Email sender’s name
MsgKsbkxx.From.Address := formAddress; //Email sender address
MsgKsbkxx.ReceiptRecipient.Address := receiptRecipientAddress;
//Reply address, which can be different from the email sender's address
MsgKsbkxx.Recipients.EMailAddresses := sendtoAdd; //Sending address?
MsgKsbkxx.Sender.Address := formAddress; //sendtoAdd; //Send email to... address
MsgKsbkxx.Subject := emailSubject; //Subject
MsgKsbkxx.Body.Assign(emailMemo.Lines); //Email content
IdSMTP1.Send(MsgKsbkxx); //Send email command
end;
5. Simply determine the legitimacy of the email address
Copy the code. The code is as follows: // Separate the string s into several strings according to the representation of the string Separator, and store them in the rs string list.
procedure SeparateTerms2(s:string;Separator:string;var rs:TStringList);
var
AStr: string;
idx: Integer;
ASubStr: string;
begin
AStr := Trim(s);
while Pos(Separator, AStr) > 0 do
begin
idx := Pos(Separator, AStr);
ASubStr := Copy(AStr, 1, idx - 1);
rs.Add(ASubStr);
AStr := Copy(AStr, idx + 1, Length(AStr));
end;
if AStr+'a' <> 'a' then rs.Add(AStr); //If there are remaining strings, store them in the string list
end;
//Determine whether a string meets the email address standard
//Correct: return ok, error return error
function emailAddressYesOrNo (emailAddress:String):string;
var
getStrings:TStringList;
getYesOrNo:string;
begin
getYesOrNo := 'error';
getStrings := TStringList.Create;
SeparateTerms2(emailAddress,'@',getStrings);
if getStrings.Count=2 then
begin
getStrings.Clear;
SeparateTerms2(emailAddress,'.',getStrings);
if getStrings.Count>1 then getYesOrNo := 'ok';
end;
getStrings.Free;
result := getYesOrNo;
end;
6. Send emails in batches
Copy the code as follows: procedure bEmailKsxxClick;
var
AccordAmount,i,j,tag:integer;
emailusername,formAddress,sendtoAdd,emailSubject,receiptRecipientAddress,selectStr:string;
begin
//Determine whether there is data in the data table that needs to be sent by email, and if so, send it
if Bmb.RecordCount >0 then
AccordAmount := Bmb.RecordCount
else
exit;
//
//Get account information, usually stored in a data table or INI file
emailusername := userNameString; //Sender of email
formAddress := fromAddressString; //Sending email address
receiptRecipientAddress := receiptRecipientAddressString; //Reply email address
emailSubject := emailSubjectString; //Email subject
//
tag := 1; //Flag bit: stop when sending error
i:=1; //Total number of emails sent
j:=0; //The number of records processed to let the progress bar gradually advance
list1.Clear; //Display information about students whose emails have been successfully sent
//Determine whether it can be connected to the set mailbox. If the return value is 1, obtain the email content and related settings and send it
if setEmailInfo = 1 then
begin
Pb.BlockSize := 1;
Pb.Max := AccordAmount ;
Formsjtj.Refresh;
Bmb.First;
try
while (not tBmb.Eof) do
begin
//Determine whether the email address is empty and meets the email specifications. If both conditions are met, send an email to the address.
if Bmb.FieldByName('s_emailAddress').AsString+'a' <> 'a' then
begin
//Get the content of the email sent
emailMemo.Clear;
............................
//Set the email information. If there is an error in sending the email, exit directly.
if tag=0 then exit;
//Send email
sendtoAdd := trim(Bmb.FieldByName('s_emailAddress').AsString); //Needs to send to...mailbox
if emailAddressYesOrNo(sendtoAdd)= 'ok' then //Check whether the email format is correct
begin
sendEmailOnce(emailusername,formAddress,receiptRecipientAddress,sendtoAdd,emailSubject); //Send email
i := i+1; //counter plus 1
list1.Items.Add(......); //Put the candidate information that has been successfully sent emails in the list
end;
//
end;
//It stalls for 2 seconds every time 50 emails are sent.
if (i mod 50) = 0 then sleep(2000);
dm_sjtj.ListBmb.next;
Application.ProcessMessages; //Process process information in the loop
//Refresh progress indicator
j := j+1;
PB.Progress:=j;
PB.StepIt;
PB.Refresh;
end;
finally
tag := 0;
IdSMTP1.Disconnect;
end;
end;
showmessage('Total sent'+intToStr(i-1)+' emails.');
end;
7. Issues that need attention
① Spam emails are not allowed by the country
② Since bulk email is controlled by most email providers, there is often a delay between two emails. It is recommended to set up your own email server.
③ Since the Indy control only has two modes: atNone and atSASL, after setting up the mail server, you need to set up an IP address and account that do not require verification;
④ Since internal email addresses cannot obtain email replies, the reply address and the sending address are often set to be different. One problem that arises now is that you cannot reply when opening an email through a browser, but you can get a reply after receiving an email through Foxmail. This needs attention.
I hope this article will be helpful to everyone's Delphi programming.