I am trying to send a mail through my application developed in BDS 2006 via MS office Outlook.
It works totally fine with my outlook running ,but it fails in the try block if outlook is closed.
It displays error EOlesystem error : Operation unavailable and does not go to Exceptblock
my code
procedure TMyform.BTN_mailClick(Sender: TObject);
const
olMailItem =0;
var
Outlook: OleVariant;
vMailItem: variant;
begin
try
Outlook := GetActiveOleObject('Outlook.Application');
except
Outlook := CreateOleObject('Outlook.Application');
end;
vMailItem := Outlook.CreateItem(olMailItem);
vMailItem.Recipients.Add(mailaddress);
vMailItem.Subject := 'mymail';
vMailItem.Body := 'Dear '
vMailItem.Attachments.Add(path);
vMailItem.Send;
VarClear(Outlook);
end;
How can I overcome this ?
Thanks
It has to go to the except block. Did you set a breakpoint there to check?
But nevertheless you can prevent the exception from happening:
var
Outlook: OleVariant;
ClassID: TCLSID;
Unknown: IUnknown;
begin
if Succeeded(GetActiveObject(ClassID, nil, Unknown)) then
OleCheck(Unknown.QueryInterface(IDispatch, Outlook)) else
Outlook := CreateOleObject('Outlook.Application');
{ ... }
I had the same problem. But recently I found a workaround. Instead of adding multiple e mail addresses using "vMailItem.Recipients.Add(mailaddress);", I used "vMailItem.To := 'mailID';". I hope it helps you.
Here is an example:
procedure TForm1.SendMailClick(Sender: TObject);
const olMailItem = $00000000;
Var
Outlook: OleVariant;
Mail: Variant;
begin
try
try
Outlook := GetActiveOleObject('Outlook.Application');
except
Outlook := CreateOleObject('Outlook.Application');
end;
Mail := Outlook.CreateItem(olMailItem);
Mail.To := '[email protected]' + ';' + '[email protected]';
Mail.Subject := 'your subject';
Mail.Display; //Mail.Send; if you want to send directly
Except
on E : Exception do
ShowMessage(E.ClassName+' error raised, with message : '+E.Message);
End;
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With