Thư viện Indy đồ sộ được tích hợp sẵng trong bộ lập trình Delphi từ phiên bản Delphi 7 trở về sau. Dựa trên bộ thư viện này việc gừi mail thông qua SMTP cực kỳ đơn giản và hiệu quả.
Tôi xin giới thiệu một dòng Code ngắn mô tả việc gửi mail thông qua IdSMTP
Uses
IdSMTPBase, IdSMTP,IdMessage, IdEMailAddress,IdMessageClient
procedure SendMail();
var
msg: TIdMessage;
recipients: TIdEMailAddressList;
begin
try
msg := TIdMessage.Create(nil);
recipients := TIdEMailAddressList.Create(nil);
try
IdSMTP1.Host := edtMailServer.Text;
IdSMTP1.Username := edtUser.Text;
IdSMTP1.Password := edtPassword.Text;
IdSMTP1.Port := StrToInt(edtPort.Text);
msg.Sender.Address := edtSender.Text;
msg.Subject := edtSubject.Text;
recipients.EMailAddresses := mmoRecepient.Text;
msg.Recipients.AddItems(recipients);
msg.Body.Append(mmoBody.Text + chr(13));
IdSMTP1.Connect;
IdSMTP1.Send(msg);
IdSMTP1.Disconnect(false);
finally
msg.Free;
recipients.Free;
end;
except
on ex: Exception do mmoLog.Lines.Add(ex.Message);
end;
end;