Manage Inbox

This application demonstrates how:

  1. How to logon to an EWS Session using a profile.
  2. How to get the contents of the inbox
  3. How to use filters to only get unread messages and/or messages of a specific person
  4. How to use events to respond to new and/or deleted messages
  5. How to create new email messages
  6. How to delete email messages
  7. How to create a reply, forward and/or forward all
  8. How to move a message to a different folder
  9. How to use the folder dialog to select a folder

Create the filter:


procedure TFrmMain.SetRestriction(Sender: TObject);
// Build a restriction and set the restriction
// The table event "OnRestrictDone" will rebuild the listview
var
  RestrictionAnd: IRwEWSRestrictionAnd;
  RestrictionCompare: IRwEWSRestrictionCompare;
  RestrictionContent: IRwEWSRestrictionContent;
begin
  Assert(Assigned(FContentTable));

  if chBxSuppressReadMessages.Checked then
  begin
    RestrictionCompare := FContentTable.CreateRestriction(rtCompare) as IRwEWSRestrictionCompare;
    RestrictionCompare.FieldDef := FLD_MESSAGE_ISREAD;
    RestrictionCompare.CompareOperator := opIsEqualTo;
    RestrictionCompare.Value := False;
  end else
    RestrictionCompare := nil;

  if edtShowMessagesFrom.Text  '' then
  begin
    RestrictionContent := FContentTable.CreateRestriction(rtContent) as IRwEWSRestrictionContent;
    RestrictionContent.FieldDef              := FLD_MESSAGE_SENDER;
    RestrictionContent.ContainmentMode       := cmSubstring;
    RestrictionContent.ContainmentComparison := ccIgnoreCaseAndNonSpacingCharacters;
    RestrictionContent.Value                 := edtShowMessagesFrom.Text;
  end else
    RestrictionContent := nil;

  if Assigned(RestrictionCompare) and Assigned(RestrictionContent) then
  begin
    RestrictionAnd := FContentTable.CreateRestriction(rtAnd) as IRwEWSRestrictionAnd;
    RestrictionAnd.AddRestriction(RestrictionCompare);
    RestrictionAnd.AddRestriction(RestrictionContent);
    FContentTable.Filter(RestrictionAnd);
  end else
  if Assigned(RestrictionCompare) then
    FContentTable.Filter(RestrictionCompare)
  else
  if Assigned(RestrictionContent) then
    FContentTable.Filter(RestrictionContent)
  else
    FContentTable.Filter(nil);

  RebuildMessageList(Sender);
end;

Handle a deleted item:


procedure TFrmMain.EWSFolderEventsItemDeleted(
  AFolder: IRwEWSFolder; const ATimeStamp: TDateTime; const AId, AParentFolderId: TItemId);
var
  i: Integer;
  Data: TRwListItemData;
begin
  if AFolder.ID.Id = FCurrentFolder.Id.Id then
  begin
    for i := lvMessages.Items.Count-1 downto 0 do
    begin
      Data := lvMessages.Items[i].Data;
      if Data.ItemId.Id = AId.Id then
      begin
        lvMessages.Items.Delete(i);
        Break;
      end;
    end;
  end;
end;

Create a Forward:


procedure TFrmMailMessage.actForwardExecute(Sender: TObject);
var
  Response: IRwEWSSmartResponse;
begin
  Response := FMessage.CreateForward();
   // The response itself only contains
  // a few properties which can be changed
  // In order to access all the properties
  // we must save it first
  CreateAndShowReply(Response.SaveChanges());
end;

Screenshot:


Back to Examples