Search code examples
delphiemailoutlookdelphi-2010ole

Send email with MS Outlook don't add signature


I use Delphi RAD Studio 2010 and the next code to send email with Outlook:

procedure SendOutlookMail(email,subject,body,fileat:string);
const
  olMailItem = 0;
var
   vMailItem: variant;
   Outlook: OutlookApplication;
   NmSpace: NameSpace;
   Folder: MAPIFolder;
begin
  Outlook := CoOutlookApplication.Create;
  NmSpace := Outlook.GetNamespace('MAPI');
  NmSpace.Logon('', '', False, False);
  Folder := NmSpace.GetDefaultFolder(olFolderInbox);
  Folder.Display;

  vMailItem := Outlook.CreateItem(olMailItem);
  if email<>'' then  vMailItem.Recipients.Add(email);
  vMailItem.Subject := subject;
  vMailItem.Body := Body;
  vMailItem.Attachments.Add(fileat);
  vMailItem.Display(false);
end;

It opens a new Outlook message and bring it to front to just press "Send" to send it. That's ok. The problem is, creating a new email message with this method doesn't add the signature. If I create a new message within Ms Outlook, the signature is added automatically.
Is there anyway I can add the signature that the user has configured in MS Outlook? (without adding the signature text to "Body" string variable). Thanks in advance.


Solution

  • the signature is added when you call MailItem.Display or access MailItem.GetInspecrtor.

    Call MailItem.Display first (the signature will be added at that moment), then merge your data with the existing body. Note that setting the plain text Body property will wipe out formatting, so you will need to work with the HTMLBody property. Keep in mind that 2 HTML strings cannot be simply concatenated - read the HTMLBody property, find the appropriate insertion position (after the <body> tag?), then insert your data.