Delphi7 the method of sending email message _delphi

Source: Internet
Author: User
Tags trim

This article illustrates the method of sending email messages in Delphi7. Share to everyone for your reference. The specific analysis is as follows:

This time need for all candidates to take the examination information and examination information through e-mail, after many debugging, is successful, sent to you for reference:

The general idea is:

1, the installation of the LAN version of the mail server , and by setting up DNS so that the server can send e-mail to the users outside the network, this aspect of the software more, such as Winwebmail is good, you can download from the official website;

2, need to use the control: Indy10.0.15, can be downloaded through Baidu search;

3, set up the need to send e-mail server and account information

Copy Code code as follows:
function Setemailinfo:integer; Return value 0: Mailbox settings failed; 1: Mailbox Settings succeeded
Var
selectstr:string;
Thisresult:integer;
Begin
Thisresult: = 0;
Set up an account
Idsmtp1.authtype: = Atnone; or a ATSASL;
Idsmtp1.host: = hoststring;
Idsmtp1.username: = usernamestring;
Idsmtp1.password: = passwordstring;
Try
Idsmtp1.connect;
Thisresult: = 1;

If not idsmtp1.authenticate then
Begin
ShowMessage (' Send mailbox account verification failed! Please check SMTP account settings! ');
Thisresult: = 0;
End
Except
ShowMessage (' SMTP server connection failed! Please check the SMTP account settings and the network is normal! ');
Thisresult: = 0;
End
End
Result: = Thisresult;
End


4, send an email message as an independent function, send the need for time delay control
Copy Code code as follows:
Procedure Sendemailonce (emailusername:string;
formaddress:string;receiptrecipientaddress:string;sendtoadd:string;emailsubject:string);
Begin
MsgKsbkxx.From.Name: = Emailusername; Mail Sender Name
MsgKsbkxx.From.Address: = formaddress; Mail Sender Address
MsgKsbkxx.ReceiptRecipient.Address: = receiptrecipientaddress;
Reply Address, which can be different from the sender's address.
MsgKsbkxx.Recipients.EMailAddresses: = Sendtoadd; Send an address?
MsgKsbkxx.Sender.Address: = formaddress;  Sendtoadd; Send mail to ... Address
Msgksbkxx.subject: = EmailSubject; Theme
MsgKsbkxx.Body.Assign (Emailmemo.lines); Message content
Idsmtp1.send (MSGKSBKXX); Send mail instructions
End


5, simple to determine the legality of e-mail addresses
Copy Code code as follows:
Separates the string s into several strings based on the representation of the string separator, stored 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 the remaining string exists, it is stored in the string list
End

Determine if a string meets the email address standard
Correct: Return OK, error return errors
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 email in bulk
Copy Code code as follows:
Procedure Bemailksxxclick;
Var
Accordamount,i,j,tag:integer;
emailusername,formaddress,sendtoadd,emailsubject,receiptrecipientaddress,selectstr:string;
Begin
Determine if there is data in the datasheet that needs to be sent to e-mail, and if so, send
If Bmb.recordcount >0 Then
Accordamount: = Bmb.recordcount
Else
Exit
//
Get account information, usually stored in a datasheet or INI file
Emailusername: = usernamestring; Send mail person
Formaddress: = fromaddressstring; Send mail address
Receiptrecipientaddress: = receiptrecipientaddressstring; Reply Email Address
EmailSubject: = emailsubjectstring; Message subject
//
Tag: = 1; Flag bit: Stop if send error
I:=1; Total number of messages sent
j:=0; Number of records processed to make progress bar progressively progressive
List1. Clear; Show student information that has been successfully sent to e-mail
Determine if you can connect to a set mailbox, and if the return value is 1, get the message content and related settings and send
If Setemailinfo = 1 Then
Begin
Pb.blocksize: = 1;
Pb.max: = Accordamount;
Formsjtj.refresh;
Bmb.first;
Try
while (don't tbmb.eof) do
Begin
To determine if the email address is empty and meets the email specification, send an email address if all conditions are met
If Bmb.fieldbyname (' s_emailaddress '). asstring+ ' A ' <> ' a ' then
Begin
Get the contents of a Send Message
Emailmemo.clear;
..........................
Set message information, such as sending mail error, exit directly
If Tag=0 then exit;
Send mail
Sendtoadd: = Trim (Bmb.fieldbyname (' s_emailaddress '). asstring); Need to send to ... Mailbox
If Emailaddressyesorno (sendtoadd) = ' OK ' then//check e-mail format is correct
Begin
Sendemailonce (Emailusername,formaddress,receiptrecipientaddress,sendtoadd,emailsubject); Send mail
I: = i+1; Counter plus 1
List1.  Items.Add (...); Put the candidate information that has been successfully sent to the email in the list
End
//
End
50 messages per send stagnation 2s clock
if (i mod) = 0 Then sleep (2000);
Dm_sjtj. Listbmb.next;
Application.processmessages; Processing process information in loops
Refresh Progress Indicator
J: = J+1;
PB. Progress:=j;
PB. StepIt;
PB. Refresh;
End
Finally
Tag: = 0;
Idsmtp1.disconnect;
End
End
ShowMessage (' Send ' +inttostr (i-1) + ' email. ');
End


7, need to pay attention to the problem

① email spamming is not allowed by the state
② because the group of e-mail is the majority of e-mail providers control, often 2 of emails must have a period of delay between the proposal to set up a mail server
③ because the Indy control only has Atnone and ATSASL two kinds of modes, after setting up the mail server, need to set up the IP address and account that does not need to authenticate;
④ because the internal mail address can not get e-mail reply, so often reply to address and send address set to different. One problem that comes up now is that you can't reply by opening a message through a browser, but you can get a reply after you receive it via Foxmail. This should be noted.

I hope this article will help you with the Delphi program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.