ImapDemo1.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 57998: ImapDemo1.pas
  11. {
  12. { Rev 1.0 13/04/2004 22:31:28 CCostelloe
  13. { Basic demo
  14. }
  15. unit ImapDemo1;
  16. interface
  17. uses
  18. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  19. Dialogs, Grids, StdCtrls,
  20. IdUserPassProvider,
  21. IdSASLCollection,
  22. IdSASLLogin,
  23. IdMessage,
  24. IdIMAP4;
  25. type
  26. TForm1 = class(TForm)
  27. Label1: TLabel;
  28. Label2: TLabel;
  29. Label3: TLabel;
  30. Edit1: TEdit;
  31. Edit2: TEdit;
  32. Edit3: TEdit;
  33. Button1: TButton;
  34. Label4: TLabel;
  35. StringGrid1: TStringGrid;
  36. Button2: TButton;
  37. Label5: TLabel;
  38. ListBox1: TListBox;
  39. Label6: TLabel;
  40. Label7: TLabel;
  41. procedure Button1Click(Sender: TObject);
  42. procedure FormCreate(Sender: TObject);
  43. procedure ListBox1Click(Sender: TObject);
  44. procedure FormDestroy(Sender: TObject);
  45. procedure Button2Click(Sender: TObject);
  46. private
  47. { Private declarations }
  48. public
  49. { Public declarations }
  50. //LTheSASLListEntry: TIdSASLListEntry;
  51. TheImap: TIdIMAP4;
  52. ThePassProvider: TIdUserPassProvider;
  53. TheSASLLogin: TIdSASLLogin;
  54. UsersFolders: TStringList;
  55. end;
  56. var
  57. Form1: TForm1;
  58. implementation
  59. {$R *.dfm}
  60. procedure TForm1.Button1Click(Sender: TObject);
  61. var
  62. i: integer;
  63. bRet: Boolean;
  64. begin
  65. if Button1.Caption = 'Disconnect' then begin
  66. Screen.Cursor := crHourGlass;
  67. TheImap.Disconnect;
  68. Button1.Caption := 'Connect';
  69. Screen.Cursor := crDefault;
  70. end else begin
  71. Screen.Cursor := crHourGlass;
  72. TheImap.Host := Edit1.Text;
  73. TheImap.Username := Edit2.Text;
  74. TheImap.Password := Edit3.Text;
  75. //LTheSASLListEntry := TheSmtp.FSASLMechanisms.Add;
  76. //LTheSASLListEntry.SASL := TheSASLLogin;
  77. TheImap.Connect;
  78. ListBox1.Clear;
  79. bRet := TheImap.ListMailBoxes(UsersFolders);
  80. if bRet = False then begin
  81. ShowMessage('Failed to retrieve folder names!');
  82. end;
  83. for i := 0 to UsersFolders.Count-1 do begin
  84. ListBox1.Items.Add(UsersFolders[i]);
  85. end;
  86. Button1.Caption := 'Disconnect';
  87. Screen.Cursor := crDefault;
  88. end;
  89. end;
  90. procedure TForm1.FormCreate(Sender: TObject);
  91. begin
  92. TheImap := TIdIMAP4.Create(nil);
  93. ThePassProvider := TIdUserPassProvider.Create(nil);
  94. TheSASLLogin := TIdSASLLogin.Create(nil);
  95. UsersFolders := TStringList.Create;
  96. StringGrid1.Cells[0, 0] := 'Relative number';
  97. StringGrid1.Cells[1, 0] := 'UID';
  98. StringGrid1.Cells[2, 0] := 'Read?';
  99. StringGrid1.Cells[3, 0] := 'Subject';
  100. StringGrid1.ColWidths[0] := 100;
  101. StringGrid1.ColWidths[1] := 100;
  102. StringGrid1.ColWidths[2] := 50;
  103. StringGrid1.ColWidths[3] := 290;
  104. StringGrid1.RowCount := 2;
  105. StringGrid1.Cells[0, 1] := '';
  106. StringGrid1.Cells[1, 1] := '';
  107. StringGrid1.Cells[2, 1] := '';
  108. StringGrid1.Cells[3, 1] := '';
  109. end;
  110. procedure TForm1.ListBox1Click(Sender: TObject);
  111. var
  112. TheFlags: TIdMessageFlagsSet;
  113. TheUID: string;
  114. i: integer;
  115. nCount: integer;
  116. TheMsg: TIdMessage;
  117. MailBoxName: string;
  118. begin
  119. if ListBox1.ItemIndex <> -1 then begin
  120. Screen.Cursor := crHourGlass;
  121. MailBoxName := ListBox1.Items[ListBox1.ItemIndex];
  122. if TheImap.SelectMailBox(MailBoxName) = False then begin
  123. Screen.Cursor := crDefault;
  124. ShowMessage('Error selecting '+MailBoxName);
  125. Exit;
  126. end;
  127. TheMsg := TIdMessage.Create(nil);
  128. nCount := TheImap.MailBox.TotalMsgs;
  129. if nCount = 0 then begin
  130. StringGrid1.RowCount := 2;
  131. StringGrid1.Cells[0, 1] := '';
  132. StringGrid1.Cells[1, 1] := '';
  133. StringGrid1.Cells[2, 1] := '';
  134. StringGrid1.Cells[3, 1] := '';
  135. ShowMessage('There are no messages in '+MailBoxName);
  136. end else begin
  137. StringGrid1.RowCount := nCount + 1;
  138. for i := 0 to nCount-1 do begin
  139. TheImap.GetUID(i+1, TheUID);
  140. TheImap.UIDRetrieveFlags(TheUID, TheFlags);
  141. TheImap.UIDRetrieveHeader(TheUID, TheMsg);
  142. StringGrid1.Cells[0, i+1] := IntToStr(i+1);
  143. StringGrid1.Cells[1, i+1] := TheUID;
  144. if mfSeen in TheFlags then begin
  145. StringGrid1.Cells[2, i+1] := 'Yes';
  146. end else begin
  147. StringGrid1.Cells[2, i+1] := 'No';
  148. end;
  149. StringGrid1.Cells[3, i+1] := TheMsg.Subject;
  150. end;
  151. end;
  152. TheMsg.Destroy;
  153. Screen.Cursor := crDefault;
  154. end;
  155. end;
  156. procedure TForm1.FormDestroy(Sender: TObject);
  157. begin
  158. if Button1.Caption = 'Disconnect' then begin
  159. TheImap.Disconnect;
  160. end;
  161. UsersFolders.Destroy;
  162. TheSASLLogin.Destroy;
  163. ThePassProvider.Destroy;
  164. TheImap.Destroy;
  165. end;
  166. procedure TForm1.Button2Click(Sender: TObject);
  167. var
  168. TheUID: string;
  169. begin
  170. //Delete selected message..
  171. if StringGrid1.Selection.Top > 0 then begin
  172. Screen.Cursor := crHourGlass;
  173. TheUID := StringGrid1.Cells[1, StringGrid1.Selection.Top];
  174. if TheImap.UIDDeleteMsg(TheUID) = True then begin
  175. if TheImap.ExpungeMailBox = True then begin
  176. Screen.Cursor := crDefault;
  177. ShowMessage('Successfully deleted message - select another mailbox then reselect this mailbox to see its omission');
  178. end else begin
  179. Screen.Cursor := crDefault;
  180. ShowMessage('Succeeded in setting delete flag on message, but expunge failed - is this a read-only mailbox?');
  181. end;
  182. end else begin
  183. Screen.Cursor := crDefault;
  184. ShowMessage('Failed to set delete flag on message - is this a read-only mailbox?');
  185. end;
  186. end;
  187. end;
  188. end.