2
votes

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.

2

2 Answers

2
votes

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.

1
votes

//Here is how I did it in Delphi. It shows the part no-one else did, which is //parsing the email HTML and putting your text above the signature. //The part at the beginning loads up the string that I will be adding, so you //could ignore that.

procedure TfrmEngInfo2.CreateMSOutlookHoldEmail;
var
  i: Integer;
  Outlook: OleVariant;
  Mail: OleVariant;
  EmailTo,EmailCC: string;
  SubjectLine: string;
  PartNbr,PONbr,JobNbr: string;
  HTMLInfo: string;
  JobInfo: string;
  x: integer;
  iPos: integer;
  RealPosition,GTPosition: integer;
  const
  olMailItem = 0;
  olByValue = 1;
  olFormatHTML = 2;


begin
  SubjectLine := 'PCB Hold /';
  if JOBHEMAI.state = dsInactive then
    JOBHEMAI.open;
  if CUST.state = dsInactive then CUST.open;

  if RELEASE.FindKey([AdsQuery1.FieldByName('R-JOB#').asstring, AdsQuery1.FieldByName('R-RELEASE-NBR').asstring]) then
  begin
    PONbr := RELEASE.FieldByName('R-PO-NBR').asstring;
    JobNbr := AdsQuery1.FieldByName('R-JOB#').asstring + AdsQuery1.FieldByName('R-RELEASE-NBR').asstring;
    SubjectLine := SubjectLine + ' PO#: ' + PONbr + ' / ';
  end;

  if JOBHEMAI.FindKey([AdsQuery1.FieldByName('R-JOB#').asstring]) then
    EmailTo := JOBHEMAI.FieldByName('HoldEmailAddress').asstring;

   if HEADER.FindKey([AdsQuery1.FieldByName('R-JOB#').asstring]) then
   begin
    PartNbr := HEADER.FieldByName('H-PART#').asstring;
    SubjectLine := SubjectLine + ' Part#: ' + PartNbr;
    if CUST.FindKey([HEADER.FieldByName('H-CUST-NBR').asstring]) then
      if EmailTo = '' then
        EmailTo := CUST.FieldByName('HoldEmailAddress').asstring;
    if SALEPRSN.FindKey([HEADER.FieldByName('H-SLMN#').asstring]) then
      EmailCC := SALEPRSN.fieldByName('E-mail').asstring;

   end;
   SubjectLine := SubjectLine + JobNbr;


  try
    Outlook := GetActiveOleObject('Outlook.Application');
  except
    Outlook := CreateOleObject('Outlook.Application');
  end;
  JobInfo := 'We have released the hold for your job Part: ' + PartNbr + ' PO: ' + PONbr
  + 'Job Number: ' + JobNbr;
  Mail := Outlook.CreateItem(olMailItem);
  Mail.To := EmailTo;
  Mail.cc := EmailCC;
  Mail.Subject := SubjectLine;
  // Main.Attachments.Add('c:\test.jpg', olByValue, 1, 'My Test Image');
  Mail.BodyFormat := olFormatHTML;
  //This load the HTMLBody with all the outlook stuff including the users signature.  Basically a blank email message
  Mail.GetInspector;
  // Find the position of the opening <body command.
  x := pos('<body',Mail.HTMLBody);
  // Put the HTMLBody in to a string field so we can examine it.
  HTMLInfo := Mail.HTMLBody;

   // Now loop through and find the position of the closing tag. '>'   It must be in a position greater than the starting tag
   RealPosition := 0;
   while pos('>',HTMLInfo) > 0 do
   begin
      GTPosition := pos('>',HTMLInfo);
      RealPosition := RealPosition + GTPosition;
      if RealPosition > x then
        break;
      HTMLInfo := copy(HTMLInfo,GTPosition + 1,Length(HTMLInfo) - GTPosition);
   end;

  // Since we destroyed the HTMLInfo in the analysis, load it again.
  HTMLInfo := Mail.HTMLBody;
  // Now insert our information at the right spot.
  insert(JobInfo,HTMLInfo,RealPosition + 1);
  // Now load the modified HTMLInfo back to the Mail.HTMLBody so that it can display in the message.
  Mail.HTMLBody := HTMLInfo;
  // Finally display the Outlook Email
  Mail.Display;
  Outlook := unassigned;
end;