8

I want to create a mail with attachment in Outlook and display it before sending it, but I think I have tried almost every sample I have found on the net without any luck. I could use Indy, but I would very much like to use Outlook to be sure that the mail is proper because it is for business use.

Any input for a function that takes Address, subject, message and attachment as parameters and then displays the message in Outlook before sending it.

Mat
  • 202,337
  • 40
  • 393
  • 406
OZ8HP
  • 1,443
  • 4
  • 31
  • 61

1 Answers1

15

See MailItem.Display Method.

uses
  comobj;

..

procedure DisplayMail(Address, Subject, Body: string; Attachment: TFileName);
var
  Outlook: OleVariant;
  Mail: Variant;
const
  olMailItem = $00000000;
begin
  try
    Outlook := GetActiveOleObject('Outlook.Application');
  except
    Outlook := CreateOleObject('Outlook.Application');
  end;
  Mail := Outlook.CreateItem(olMailItem);
  Mail.To := Address;
  Mail.Subject := Subject;
  Mail.Body := Body;
  if Attachment <> '' then
    Mail.Attachments.Add(Attachment);
  Mail.Display;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DisplayMail('mailaddress', 'subject', 'message', 'attachmentfile');
end;
Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
  • I am not sure yet what I have been doing wrong because it is pretty much the same code as I have been using but with no luck. Your code on the other hand works. I am wondering if it has something to do with the fact that I have been using Outlook_TLB - but I can't see what this should do. Do you know if it is possible to add Request read receipt and delivery receipt to the code? – OZ8HP Dec 11 '11 at 13:36
  • 1
    For receipt read request check the [`ReadReceiptRequested`](http://msdn.microsoft.com/en-us/library/ff865400.aspx) property and use it as `Mail.ReadReceiptRequested := True;` – TLama Dec 11 '11 at 13:52
  • 1
    .. and `OriginatorDeliveryReportRequested` propety. – Sertac Akyuz Dec 11 '11 at 14:03
  • @Sertac Akyuz I am still struggling with this system :-) I am used to a site here in Denmark that is a bit simpler/easier to use. But I think I found out how to accept the answer. Now I am working on expandig the mail send to use a specific account. This is quite easy in VBA. – OZ8HP Dec 22 '11 at 16:06