Email message format

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

Email message format
Email message format 
bluewwol




Posts: 26
Joined: 2022-01-21
Hi,

I assume I am missing something very simple. If an email is composed as plain text and I want my addin to convert it to HTML.

My code is essentially
if Mail.BodyFormat = olText then
Mail.BodyFormat := olFormatHTML;

However the email is still sent as plain text.
Please point out what I am missing.

Thanks
-Allen
Posted 24 Oct, 2022 12:37:33 Top
Andrei Smolin


Add-in Express team


Posts: 18878
Joined: 2006-05-11
Hello Allen,

Save the email and do not send it. Instead, open the saved email and check its format.

Regards from Poland (CEST),

Andrei Smolin
Add-in Express Team Leader
Posted 24 Oct, 2022 13:22:38 Top
bluewwol




Posts: 26
Joined: 2022-01-21
Andrei;

Sorry I must still be missing something, My onItemSend event is as follows, but my message is still sent as plain text. The mrNo element of the case statement is the condition I am having an issue with


procedure TAddInModule.DoItemSend(ASender: TObject; const Item: IDispatch; var Cancel: WordBool);
var
  Mail: Outlook2010.MailItem;
begin
  if dMain.GloabEnabled then
  begin
    if Assigned(Item) then
    begin
      Item.QueryInterface(IID__MailItem, Mail);
      if Assigned(Mail) then
        if Mail.BodyFormat = olText then
        begin
          case
            MyMessageDialog('Your message is formatted as plain text, a tracking tag will not be inserted.  Do you want to send anyway?',
            mtConfirmation, mbYesNoCancel, ['Proceed', 'Convert to HTML', 'Dont Send']) of
            mrYes:
              begin
                Mail := nil;
                Exit;
              end;
            mrNo:
              begin
                Mail.BodyFormat := olFormatHTML;
                Mail.Save;
		Mail := nil;
                DoItemSend(ASender,  Item,  Cancel);
                exit;
              end;
            mrCancel:
              begin
                Mail := nil;
                Cancel := true;
                Exit;
              end;
          end;
        end;     
    end;
  end;
end;
Posted 24 Oct, 2022 14:45:37 Top
bluewwol




Posts: 26
Joined: 2022-01-21
Andrei;

I now have my code like the below, this causes the result I am after, however I would like to avoid taking the user back to the edit screen and rather complete the send operation seamlessly.

Any suggestions will be appreciated,



procedure TAddInModule.DoItemSend(ASender: TObject; const Item: IDispatch; var Cancel: WordBool);
var
  Mail: Outlook2010.MailItem;
begin
  if dMain.GloabEnabled then
  begin
    if Assigned(Item) then
    begin
      Item.QueryInterface(IID__MailItem, Mail);
      if Assigned(Mail) then
        if Mail.BodyFormat = olText then
        begin
          case
            MyMessageDialog('Your message is formatted as plain text, a tracking tag will not be inserted.  Do you want to send anyway?',
            mtConfirmation, mbYesNoCancel, ['Proceed', 'Convert to HTML', 'Dont Send']) of
            mrYes:
              begin
                Mail := nil;
                Exit;
              end;
            mrNo:
              begin
                Mail.BodyFormat := olFormatHTML;
                Mail.Save;
				Mail := nil;
                Cancel := true;
                Exit;
              end;
            mrCancel:
              begin
                Mail := nil;
                Cancel := true;
                Exit;
              end;
          end;
        end;     
    end;
  end;
end;
Posted 24 Oct, 2022 15:07:14 Top
Andrei Smolin


Add-in Express team


Posts: 18878
Joined: 2006-05-11
bluewwol writes:
I would like to avoid taking the user back to the edit screen and rather complete the send operation seamlessly.


I suppose this means you do not need to set Cancel := true if the user chooses 'Convert to HTML'. If this isn't so, please explain.

Regards from Poland (CEST),

Andrei Smolin
Add-in Express Team Leader
Posted 25 Oct, 2022 03:20:16 Top
bluewwol




Posts: 26
Joined: 2022-01-21
Andrei;

Andrei Smolin writes:
I suppose this means you do not need to set Cancel := true if the user chooses 'Convert to HTML'. If this isn't so, please explain.


When I dont cancel after setting "Mail.BodyFormat := olFormatHTML;" the email is still sent as Plain text.

I have also tried saving the Mail and recursively calling the DoItemSend function, this also still sends it as plain text. Reading your first reply led me to where I am now. Also observationally after setting the "Mail.BodyFormat := olFormatHTML;" and canceling the send, when the UI opens I can see the Outlook mail format indicator change from Plain to HTML.

-Allen
Posted 25 Oct, 2022 09:10:13 Top
Andrei Smolin


Add-in Express team


Posts: 18878
Joined: 2006-05-11
Hello Allen,

Try to cancel the send, wait for a bit (20-200 ms) - use a timer running on the same thread - and send the email anew.

bluewwol writes:
recursively calling the DoItemSend function


I would recommend not doing such things: DoItemSend is an event procedure; it is called when Office decides to do so.

Regards from Poland (CEST),

Andrei Smolin
Add-in Express Team Leader
Posted 26 Oct, 2022 06:04:29 Top
bluewwol




Posts: 26
Joined: 2022-01-21
Andrei;

Thanks and understood, one remaining issue then is how from the timer event would I initiate the Outlook send operation to in turn call my DoItemSend?

-Allen
Posted 26 Oct, 2022 09:47:41 Top
Andrei Smolin


Add-in Express team


Posts: 18878
Joined: 2006-05-11
Hello Allen,

MailItem.Send() starts the send process; the ItemSend event is raised along the way.

Regards from Poland (CEST),

Andrei Smolin
Add-in Express Team Leader
Posted 26 Oct, 2022 10:17:16 Top
bluewwol




Posts: 26
Joined: 2022-01-21
Thanks Andrei;
Posted 26 Oct, 2022 12:58:55 Top