Mail.Delete

Add-in Express™ Support Service
That's what is more important than anything else

Mail.Delete
 
Giancarlo




Posts: 67
Joined: 2016-08-17
i have this object
FMail: TMailItem;


The command
FMail.Delete

move mail into trash can

It's possible permanently delete?
Posted 05 Sep, 2016 10:03:32 Top
Andrei Smolin


Add-in Express team


Posts: 14140
Joined: 2006-05-11
Hello Giancarlo,

The only way to do this programmatically is to delete it from the Deleted Items folder.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 05 Sep, 2016 10:28:07 Top
Giancarlo




Posts: 67
Joined: 2016-08-17
I did many tests.
I did not understand a basic concept:

The pointer to an e-mail item.

I hoock to email starting from this event:

procedure TAddInModule.adxOutlookAppEvents1ItemLoad(ASender: TObject;
  const Item: IDispatch);
var
  IMail : _MailItem;
begin
  IMail:=nil;
  Item.QueryInterface(IID__MailItem, IMail);
  if (Assigned(IMail)) then begin
    Try
      FMail := TMailItem.Create(nil);
      FMail.ConnectTo(IMail);
    Finally
      IMail := nil;
    end;
  end;
end;


In another part of the code, I create a button:

procedure TAddInModule.adxRibbonTabMailReadControls0Controls0Click(Sender: TObject; const RibbonControl: IRibbonControl);
begin
MyMail := FMail; //hook email
end;




And in another part of the code, I create a second button:

procedure TAddInModule.adxRibbonTabMailReadControls0Controls0Click(Sender: TObject; const RibbonControl: IRibbonControl);
begin
MyMail.delete; //try to delete hooked email
end;


Every time I read an email,
the object FMail is connected to this

When I press the first button,
I would like that the variable MyMail is hooked to current email

So, when I read other email
FMail points to the new email,
but MyMail always points to the same

That way I could do:

MyMail.delete; //move to trash can
MyMail.delete; //permanently erases


Can you suggest a code snippet?
Posted 07 Sep, 2016 08:46:31 Top
Andrei Smolin


Add-in Express team


Posts: 14140
Joined: 2006-05-11
Hello Giancarlo,

You won't be able to call MyMail.delete twice to delete an item permanently. The *only* way to delete an item permanently is to connect to the events of the Items collection of the Deleted Items folder and delete the item there.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 07 Sep, 2016 09:39:41 Top
Giancarlo




Posts: 67
Joined: 2016-08-17
How can i connect to the events of the Items collection of the Deleted Items folder?

Can you suggest a code snippet?
Posted 07 Sep, 2016 10:38:37 Top
Andrei Smolin


Add-in Express team


Posts: 14140
Joined: 2006-05-11
Check section Step 11. Handle events of Outlook Items object at https://www.add-in-express.com/docs/vcl-outlook-addins.php. This example is available for download: at https://www.add-in-express.com/downloads/adxvcl.php see the download item labelled as Add-in Express for Office and VCL sample project.

See also https://www.add-in-express.com/forum/read.php?FID=1&TID=9926.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 07 Sep, 2016 10:57:32 Top
Giancarlo




Posts: 67
Joined: 2016-08-17
I have an office professional plus 2013 and an office professional plus 2010
in the first, the plugin run correctly:
first time mail.delete move mail into trashcan
and then i search the mail into trashcan for to call mail.delete

In office 2010, i can not delete the email from the trash

I also tried to find the e-mail in all folders
When I find it, I run:

ShowMessage('try to delete ' + FMai.Subject + ' into ' + folder.Name );
  Try
    trash := OutlookApp.GetNamespace('MAPI').GetDefaultFolder(olFolderDeletedItems);
    FMai.Move(trash);
  Except
ShowMessage('I can not');
    IMai.Delete;
  End;


In office professional plus 2013 run correctly

In office professional plus 2010 the mail is duplicated:
it appears, both in the trash that in the inbox

if I re-run the procedure,
emails become 3

I think it's a bug outlook
Posted 20 Sep, 2016 10:55:45 Top
Andrei Smolin


Add-in Express team


Posts: 14140
Joined: 2006-05-11
Giancarlo,

You can handle the ItemAdd event of the TItems object connected to the Items collection of the Deleted Items folder.

Also, I would delete the item instead of moving it to the Deleted Items folder.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 21 Sep, 2016 01:09:26 Top
Giancarlo




Posts: 67
Joined: 2016-08-17
I also tried this solution, but it does not work on 2010


procedure TAddInModule.SetIsFolderTracked(const Value: Boolean);
begin
// trash can hook events
  if Assigned(ItemsDelEvents) then
  begin
    if not Value then
    begin
      ItemsDelEvents.Disconnect;
      FreeAndNil(ItemsDelEvents);
    end;
  end
  else if Value then
  begin
    ItemsDelEvents := TItems.Create(Self);
    ItemsDelEvents.OnItemAdd := ItemsDeleted;
    ConnectToFolderByIndex(ItemsDelEvents, 1);
  end;
end;


procedure TAddInModule.ItemsDeleted(ASender: TObject; const Item: IDispatch);
var
  IMai : _MailItem;
  FMai: TMailItem; //
begin
//quando spostano un messaggio nel cestino, lo distruggo
  IMai:=nil;
  Item.QueryInterface(IID__MailItem, IMai);
  if (Assigned(IMai)) then begin
    FMai := TMailItem.Create(nil);
    FMai.ConnectTo(IMai);
    if Assigned(FMai.UserProperties.Find('X-MyProperty', True)) then begin
      if (FMai.UserProperties.Find('X-MyProperty', True).Value = 'y') then
        FMai.Delete;
    end else begin
      FMai.UserProperties.Add('X-MyProperty', olText, True, True).Value := 'n'; //serve solo per non far crashare il find in TimerX-MyProperty (so che ci dev'essere un modo più furbo)
    end;

    FreeAndNil(FMai);
    IMai:=nil;
  end;
end;
Posted 21 Sep, 2016 03:03:19 Top
Andrei Smolin


Add-in Express team


Posts: 14140
Joined: 2006-05-11
Giancarlo,

in what way it doesn't work?

You can also try this approach: call Delete on the mail item object that TMailItem.Move returns (ignore my earlier suggestion to use Delete instead of Move).

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 21 Sep, 2016 06:42:56 Top