none
Delphi and creating Outlook email with signature RRS feed

  • Question

  • I currently have an application, written in Delphi which generates emails in Outlook without any problems. I am using Delphi 7, and the Outlook 12 unit. At the moment when I create an email, the email doesn't have any signature details attached. I'm wanting to create the emails with the users specific signature details attached, they may have several email accounts setup in Outlook and I would like the signature details relevant to each account adding to the email.

    I would like to know where and how I add the relevant signature details.

    Thank you in advance.

    My current code is as follows:

    procedure EmailViaOutlook(sRecipient, sSubject, sBody: string; sAttachment: Tstrings);
    var
     Outlook   : OutlookApplication;
     Unknown   : IUnknown;
     Result   : HResult;
     MI    : MailItem;
      bOutlookRunning: boolean;
      i: integer;
      sAliasName, AccountName: string;
      TempArray: array[0..255] of Char;
      EmailApplication: string;
    const
     olMailItem = 0;
    begin
     // via early binding:
     Result := GetActiveObject(CLASS_OutlookApplication, nil, Unknown);

      {Check if Outlook is already running, this uses Outlook 2000}
      if FindWindow('mspim_wnd32','Microsoft Outlook') <> 0 then
        bOutlookRunning := true
      else
        bOutlookRunning := false;

     if Result = MK_E_UNAVAILABLE then
      begin
        Outlook := CoOutlookApplication.Create as _Application;
        end
     else begin
      // make sure no other error occurred during GetActiveObject
      OleCheck(Result);
      OleCheck(Unknown.QueryInterface(OutlookApplication, Outlook));
     end;

     MI := Outlook.CreateItem(olMailItem) as MailItem;
     if trim(sRecipient) <> '' then
        MI.To_ := sRecipient;

     for i := 0 to pred(sAttachment.count) do
        MI.Attachments.Add(sAttachment[i],EmptyParam, EmptyParam, EmptyParam);

     MI.Subject := sSubject;
     MI.Body := sBody;
      try
        MI.Display(EmptyParam);
      except
      end;

      {Find which version of Outlook is in use and decide how to construct the email}
      GetPrivateProfileString('Email',  'Application', '', TempArray,
            sizeof(TempArray), frmPBMainMenu.AppIniFile);

      EmailApplication := TempArray;

      if (EmailApplication = 'OUT2007') or (EmailApplication = 'OUT2010') then
        begin
          sAliasName := dmBroker.PBLDatabase.AliasName;

          GetPrivateProfileString('Email',  pchar(sAliasName + ' Email Account'), '', TempArray,
            sizeof(TempArray), frmPBMainMenu.AppIniFile);

          AccountName := TempArray;

          if AccountName <> '' then
            MI.SendUsingAccount := Outlook.session.accounts.item(AccountName);
            MI.
        end;

      if not(bOutlookRunning) then
        begin
         Showmessage('Please click OK when the mail has been sent.');
          Outlook.Quit;
          Outlook := nil;
        end;
    end;

    Wednesday, June 27, 2012 4:22 PM

Answers

  • It is your responsibility to make sure that the HTML that you are inserting has the right style and the carriage returns (are you using <BR> or <P>,,,</P> instead of CR/LF?).

    Note that your code assumes the length of the <body> tag is 6 characters. It will not be true if the body tag has attributes. What you need to do is scroll to the next ">" character after finding the position of the "<body" substring (no closing ">").

    Your code will also fail if the body tag is lowercase.

    As for the security prompt, see http://www.outlookcode.com/article.aspx?id=52 for the list of your options.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Thursday, June 28, 2012 9:29 PM
  • Also, the call to rightstr() is wrong.

    All you need is to use the Insert() Delphi function.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Thursday, June 28, 2012 9:31 PM

All replies

  • Outlook adds the signature when you either display the mesage or touch the message's inspector in any way

    Try to call MailItem.GetInspector immediately after creating the item.

    Also note that when you set the MailItem.Body property, you wipe out the existing body. Even if you append to the existing Body, you will wipe out the formatting.

    You need to merge the existing HTMLBody property with your new data.



    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Wednesday, June 27, 2012 5:10 PM
  • you can also use Dmitry's advice with a twist - create your own mail and create in background without showing it another mail with default settings then using Word API copy signature from that hidden mail into your current one.
    Wednesday, June 27, 2012 6:08 PM
  • Thank you Dmitry for your response.

    I've replaced the line

    MI.body := MI.body

    with

      MI.Bodyformat := olFormatHTML;
      MI.HTMLBody := '<HTML><BODY>'+sBody+'</BODY></HTML>' + MI.HTMLBody;

    the problem now is that the Line breaks have been removed from the contents of the sBody string and the default font for the email is totally ignored. Also, when the email is raised, Outlook displays a warning message saying "A program is trying to access email addresses you have stored in Outlook. Do you want to allow this?" It then prompts you to allow for a certain number of minutes. 

    Also, what if the user's default message format is anything other than HTML, say Plain text or Rich Text?

     

    Thursday, June 28, 2012 11:27 AM
  • to manipulate mail's body from outlook 2007 onward is to use word API, casting to Document interface (QI) from Inspector.WordEditor
    Thursday, June 28, 2012 11:33 AM
  • Thanks Damian, could you give me an example of code using Word API to copy the signature to the current mailitem.

    Thanks

    Thursday, June 28, 2012 11:34 AM
  • You cannot concatenate 2 HTML strings and produce a valid HTML string.

    Retrieve the existing HTMLBody, find where <body...> tag ends, then insert your string at that position.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!


    Thursday, June 28, 2012 2:42 PM
  • After several attempts at inserting my string, I finally settled on inserting at the begining of the <BODY ...> tag, the reason for this was so that my string came before the signature!

    Although my string is inserted into the body of the email, the font and size are not the default settings, the CRLFs are removed from the string and when the email is created Outlook displays a warning message as detailed previously:

    "A program is trying to access email addresses you have stored in Outlook. Do you want to allow this?" It then prompts you to allow for a certain number of minutes. 

    My code looks like this:

      iPos := pos('<BODY>', MI.HTMLBody);
      MI.HTMLBody := leftstr(sHTML,(iPos+5)) + sBody + rightstr(sHTML,(iPos));

    Thursday, June 28, 2012 4:21 PM
  • Unfortunately I've no experience of Vb and some of my users use versions of Outlook prior to 2007, any solution for Outlook 2003? Thanks
    Thursday, June 28, 2012 4:23 PM
  • it will alsow work on 2003 for emails whose editor is set to word, so you would have to check it using property on inspector. As for other types of body in 2003 - txt is simple and for html you would have to write a little more smarter code then simply inserting txt right after body element (for example looking for span element with proper id that determines where citation of original mail starts). As for VB - classes and methods in word API are exactly the same in delphi as in VB, difference is only in language syntaxes. 
    Thursday, June 28, 2012 5:52 PM
  • You do have to be careful with WordMail 2003 and earlier though. In those versions WordMail was a subclassed msword.exe that ran out-of-process with Outlook. In WordMail 2007 and later the editor dll runs in-process with Outlook.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "DamianD" <=?utf-8?B?RGFtaWFuRA==?=> wrote in message news:04840fbc-6267-4184-80d1-deb4be3ab342...
    it will alsow work on 2003 for emails whose editor is set to word, so you would have to check it using property on inspector. As for other types of body in 2003 - txt is simple and for html you would have to write a little more smarter code then simply inserting txt right after body element (for example looking for span element with proper id that determines where citation of original mail starts). As for VB - classes and methods in word API are exactly the same in delphi as in VB, difference is only in language syntaxes. 

    Ken Slovak MVP - Outlook
    Thursday, June 28, 2012 8:46 PM
    Moderator
  • It is your responsibility to make sure that the HTML that you are inserting has the right style and the carriage returns (are you using <BR> or <P>,,,</P> instead of CR/LF?).

    Note that your code assumes the length of the <body> tag is 6 characters. It will not be true if the body tag has attributes. What you need to do is scroll to the next ">" character after finding the position of the "<body" substring (no closing ">").

    Your code will also fail if the body tag is lowercase.

    As for the security prompt, see http://www.outlookcode.com/article.aspx?id=52 for the list of your options.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Thursday, June 28, 2012 9:29 PM
  • Also, the call to rightstr() is wrong.

    All you need is to use the Insert() Delphi function.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Thursday, June 28, 2012 9:31 PM