aceswildab1
asked on
Sending Mail through Delphi via Default Email Application
I am looking at ways to send e-mail via the default mail application through Delphi. I am currently using MAPI to open the e-mail program (SendMail). The problem I am running into is that the window is opening up Modal. I would like to open the form as a normal form and be able to still open and use other windows. I could use ShellAPI to open the e-mail program, but I have read this has problems sending attachments.
Any thoughts on what is the best way to go or how you can use MAPI to open the e-mail Window not modally? Thanks for the insight.
Any thoughts on what is the best way to go or how you can use MAPI to open the e-mail Window not modally? Thanks for the insight.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If your mail application is OUTLOOK, you can easily control it by OLE automation...
procedure SendMailViaOutlook;
const
OutlookItemType = 0; //olMailItem
var
Outlook: OleVariant;
TheMailItem: variant;
begin
try
try
//connect running outlook instance
Outlook := GetActiveOleObject('Outlook.Application');
except
//start new outlook instance
Outlook := CreateOleObject('Outlook.Application');
end;
TheMailItem := Outlook.CreateItem(OutlookItemType);
TheMailItem.Recipients.Add('MailAdress@gmx.com');
TheMailItem.Subject := 'This is an e-mail sent by Delphi app using OutLook...';
TheMailItem.Body := 'Body text...';
TheMailItem.Attachments.Add('c:\test\attachment.txt');
TheMailItem.Send;
finally
VarClear(Outlook);
end;
end;
I doubt that works, I mean to perform MAPISendMail and get back to use the caller application before it is closed. Unless you are using thread, as shown by ewangoya.
I can provide you code that allows to disable modal effects and enable you get back to the main form even if there is another Form showing by modal and it works fine, but when you use that in any application that's calling SendMail the email application will block the I/O of your application until it's done and the email application is closed.
By applying the code that disables the modal restriction you will be able to activate your application and get the focus on it but you will not be able to click a button or even if you have timer there it will be disabled until the e-mail application is closed and the control is returned back to the caller application.
I have tried this on the OutLook and the Lotus Notes and same behavior in both applications.
So in short words that is not possible if it involves e-mail application unless using thread.
I can provide you code that allows to disable modal effects and enable you get back to the main form even if there is another Form showing by modal and it works fine, but when you use that in any application that's calling SendMail the email application will block the I/O of your application until it's done and the email application is closed.
By applying the code that disables the modal restriction you will be able to activate your application and get the focus on it but you will not be able to click a button or even if you have timer there it will be disabled until the e-mail application is closed and the control is returned back to the caller application.
I have tried this on the OutLook and the Lotus Notes and same behavior in both applications.
So in short words that is not possible if it involves e-mail application unless using thread.
ASKER
The thread has been the best answer I've seen so far. It still opens Outlook/E-mail program modally, but it does open up the calling program to allow it to run. Thanks for the help!
http://www.latiumsoftware.com/en/delphi/00041.php