unit uSendEmail;

interface

uses
   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdAntiFreezeBase, Vcl.IdAntiFreeze, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase, IdMessageClient,
  IdSMTPBase, IdSMTP, Vcl.StdCtrls, Vcl.ExtCtrls, IdMessage, IdCustomTransparentProxy, IdSocks,
  IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL;

type
  TSendEmail = class(TDataModule)
    IdSMTP1: TIdSMTP;
  private
    { Private declarations }
  public
    { Public declarations }

    function SendEmail(Subject,EmailContent,RecipientEmail:string):Boolean;

  end;

function SendEmail: TSendEmail;

implementation

{$R *.dfm}

uses
  UniGUIVars, uniGUIMainModule, MainModule;

function SendEmail: TSendEmail;
begin
  Result := TSendEmail(UniMainModule.GetModuleInstance(TSendEmail));
end;

{ TSendEmail }

function TSendEmail.SendEmail(Subject, EmailContent, RecipientEmail: string):boolean;
var
  SMTP    : TIdSMTP;
  msg     : TIdMessage;
  SSLOpen : TIdSSLIOHandlerSocketOpenSSL;

begin
  SMTP := TIdSMTP.Create(Application);
  SMTP.Host := 'smtp.yandex.ru';
  SMTP.Port := 465;
  SMTP.AuthType := satDefault;
  SMTP.Username := 'yourMailHere@yandex.ru';{Должно совпадать с msg.From.Address}
  SMTP.Password := 'YourPasswordToMailHere';

  //это необходимо использовать для SSL
  SSLOpen := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  SSLOpen.Destination := SMTP.Host+':'+IntToStr(SMTP.Port);
  SSLOpen.Host := SMTP.Host;
  SSLOpen.Port := SMTP.Port;
  SSLOpen.DefaultPort := 0;
  SSLOpen.SSLOptions.Method := sslvSSLv23;
  SSLOpen.SSLOptions.Mode := sslmUnassigned;



  SMTP.IOHandler := SSLOpen;
  SMTP.UseTLS := utUseImplicitTLS;

  msg := TIdMessage.Create(Application);
  msg.ContentType:='text/html; charset=windows-1251';
  msg.Body.Text:=EmailContent;
  msg.Subject := Subject;
  msg.From.Address := 'yourMailHere@yandex.ru'; {&lt;&lt;Должно совпадать с SMTP.UserName}
  msg.From.Name := 'Stas';
  msg.Recipients.EMailAddresses :=RecipientEmail;

  SMTP.Connect;
  if SMTP.Connected then
    begin
      SMTP.Send(msg);
//      ShowMessage ('Сообщение отправлено');
    end else
//      ShowMessage ('Не удалось отправить сообщение');
  SMTP.Disconnect();
  SMTP.Free;
  msg.Free;
end;

initialization
  RegisterModuleClass(TSendEmail);

end.
