Creating a mail with attachment in Outlook and displaying it
Asked Answered
P

1

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.

Planter answered 11/12, 2011 at 12:15 Comment(0)
O
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;
Olecranon answered 11/12, 2011 at 12:43 Comment(4)
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?Planter
For receipt read request check the ReadReceiptRequested property and use it as Mail.ReadReceiptRequested := True;Rank
.. and OriginatorDeliveryReportRequested propety.Olecranon
@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.Planter

© 2022 - 2024 — McMap. All rights reserved.