unit Msg;

interface

uses
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
   StdCtrls, ComCtrls, ExtCtrls, Grids, Buttons, Menus, IdBaseComponent,
   IdMessage, IdComponent, IdTCPConnection, IdTCPClient, IdMessageClient,
   IdSMTP, ImgList;

type
   TfrmMessageEditor = class(TForm)
      bbtnAdvanced: TBitBtn;
      bbtnCancel: TBitBtn;
      bbtnOk: TBitBtn;
      btnAttachment: TBitBtn;
      cboPriority: TComboBox;
      chkReturnReciept: TCheckBox;
      edtSubject: TEdit;
      edtTo: TEdit;
      grpAttachment: TGroupBox;
      IdMsgSend: TIdMessage;
      lblPriority: TLabel;
      lblSubject: TLabel;
      lblTo: TLabel;
      lvFiles: TListView;
      Memo1: TMemo;
      pnlBottom: TPanel;
      pnlButtons: TPanel;
      pnlMainDetails: TPanel;
      pnlSmallButtons: TPanel;
      pnlTop: TPanel;
      pnlTopLeft: TPanel;
    SMTP: TIdSMTP;
    OpenDialog1: TOpenDialog;
    StatusBar1: TStatusBar;
      procedure bbtnAdvancedClick(Sender: TObject);
      procedure bbtnOkClick(Sender: TObject);
      procedure btnAttachmentClick(Sender: TObject);
      procedure FormCreate(Sender: TObject);
    procedure SMTPStatus(ASender: TObject; const AStatus: TIdStatus;
      const AStatusText: String);


   private
    { Private declarations }
      procedure ResetAttachmentListView;
       procedure ReadConfiguration;
   public
    { Public declarations }
   end;

var
   frmMessageEditor: TfrmMessageEditor;
   SmtpServerName: String;
   SmtpServerPort: Integer;
   SmtpServerUser: String;
   SmtpServerPassword: String;
   UserEmail: String;

implementation
uses inifiles,setup,msgedtadv;

{$R *.DFM}

procedure TfrmMessageEditor.bbtnOkClick(Sender: TObject);
begin
    with IdMsgSend do
     begin
      IdMsgSend.Body.Assign(Memo1.Lines);
        IdMsgSend.From.Text := UserEmail;
         IdMsgSend.ReplyTo.EMailAddresses := UserEmail;
         IdMsgSend.Recipients.EMailAddresses := edtTo.Text; { do: naglowek }
         IdMsgSend.Subject := edtSubject.Text; { temat: naglowek }
         IdMsgSend.Priority := TIdMessagePriority(cboPriority.ItemIndex); { priorytet wiadomosci }
         if chkReturnReciept.Checked then
            begin { Zaznaczenie o zwrot do nadawcy }
               IdMsgSend.ReceiptRecipient.Text := IdMsgSend.From.Text;
            end
         else
            begin { nie ma zwrotu }
               IdMsgSend.ReceiptRecipient.Text := '';
      end;
end;

  {autoryzacja}

   SMTP.Username := SmtpServerUser;
   SMTP.Password := SmtpServerPassword;

   {glowne dane}
   SMTP.Host := SmtpServerName;
   SMTP.Port := SmtpServerPort;

   {wysylka}
   SMTP.Connect;
   try
      SMTP.Send(IdMsgSend);
   finally
      SMTP.Disconnect;
   end;
end;

procedure TfrmMessageEditor.bbtnAdvancedClick(Sender: TObject);
begin
fmSetup:=TfmSetup.Create(application);
fmSetup.ShowModal;
fmSetup.Free;
end;

procedure TfrmMessageEditor.FormCreate(Sender: TObject);
begin
   cboPriority.ItemIndex := Ord(IdMsgSend.Priority);
   // odczyt konfiguracji z pliku ini
  ReadConfiguration;
  name := 'tfrmMessageeditor';  // ??
end;

procedure tfrmMessageeditor.ReadConfiguration;
var
  MailIni: TIniFile;
begin
  MailIni := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'Mail.ini');
  with MailIni do begin


    SmtpServerName := ReadString('Smtp', 'ServerName', 'wpisz swoj serwer');
    SmtpServerPort := StrToInt(ReadString('Smtp', 'ServerPort', '25'));
    SmtpServerUser := ReadString('Smtp', 'ServerUser', 'twoj login');
    SmtpServerPassword := ReadString('Smtp', 'ServerPassword', 'twoje haslo');
    UserEmail := ReadString('Email', 'PersonalEmail', 'twojanazwa@twojserwer.pl');
  end;
  MailIni.Free;
end;

procedure TfrmMessageEditor.btnAttachmentClick(Sender: TObject);
begin
   if OpenDialog1.Execute then
      begin
         TIdAttachment.Create(IdMsgSend.MessageParts, OpenDialog1.FileName);
         ResetAttachmentListView;
      end;
end;

procedure TfrmMessageEditor.ResetAttachmentListView;
var li: TListItem;
   idx: Integer;
begin
   lvFiles.Items.Clear;
   for idx := 0 to Pred(IdMsgSend.MessageParts.Count) do
      begin
         li := lvFiles.Items.Add;
         if IdMsgSend.MessageParts.Items[idx] is TIdAttachment then
            begin
               li.ImageIndex := 0;
               li.Caption := TIdAttachment(IdMsgSend.MessageParts.Items[idx]).Filename;
               li.SubItems.Add(TIdAttachment(IdMsgSend.MessageParts.Items[idx]).ContentType);
            end
         else
            begin
               li.ImageIndex := 1;
               li.Caption := IdMsgSend.MessageParts.Items[idx].ContentType;
            end;
      end;
end;
procedure TfrmMessageEditor.SMTPStatus(ASender: TObject;
  const AStatus: TIdStatus; const AStatusText: String);
begin
statusbar1.SimpleText:=astatustext;
end;

end.

