Mega Code Archive

 
Categories / Delphi / Activex OLE
 

Sending e mail with attachment using MS Outlook

Title: Sending e-mail with attachment using MS Outlook . Question: How to send e-mail with attachment using MS outlook. Answer: The whole demo stuffs (*.dpr,*.pas etc) are in the MailDemo.Zip. (Probably we have to wait for some days to get the Zip file available. I have already sent the zip file to the webmaster through email). The unit that can do the job is scratched below: unit OutLookMail; interface USES Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Outlook8, OleServer, COMobj, ActiveX; type TMailRecord= record FileToAttach : String; MailTo : String; CC : String; BCC : String; Subject : String; Body : String; end; procedure OutLookMailProc(MailDetail:TMailRecord); implementation procedure OutLookMailProc(MailDetail:TMailRecord); VAR objOutlook : OutlookApplication; CurrentInterface : IUnknown; ActiveApplication : HResult; CurrentMailItem : MailItem; MailInspector : Inspector; begin ActiveApplication:=GetActiveObject(CLASS_OutlookApplication, nil, CurrentInterface); if ActiveApplication = MK_E_UNAVAILABLE then objOutlook := CoOutlookApplication.Create else begin OleCheck(ActiveApplication); OleCheck(CurrentInterface.QueryInterface(OutlookApplication, objOutlook)); end; CurrentMailItem:= objOutlook.CreateItem(0) as MailItem; CurrentMailItem.To_ :=MailDetail.MailTo; if MailDetail.FileToAttach'' then CurrentMailItem.Attachments.Add(MailDetail.FileToAttach,EmptyParam, EmptyParam, EmptyParam); CurrentMailItem.cc:=MailDetail.CC; CurrentMailItem.BCC:=MailDetail.BCC; CurrentMailItem.Subject := MailDetail.Subject; CurrentMailItem.Body := MailDetail.Body; MailInspector := CurrentMailItem.GetInspector; MailInspector.Display(False); Showmessage('I am waiting you to finish the mail process. Please click OK when done !'); objOutlook.Quit; objOutlook := nil; end; end. Unit for the Demo: ================= unit MailDemo; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Db,qrprntr, Qrctrls,qrExtra,qrexport, DBTables, QuickRpt, ExtCtrls; type TForm1 = class(TForm) Button1: TButton; EditMailTo: TEdit; Label1: TLabel; Label2: TLabel; EditSubject: TEdit; Label3: TLabel; EditFileToAttach: TEdit; Memo1: TMemo; Label4: TLabel; Label5: TLabel; EditCC: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation uses OutLookMail; {$R *.DFM} const CRLF=chr(13)+chr(10); procedure TForm1.Button1Click(Sender: TObject); var MailDetail:TMailRecord; x:integer; begin MailDetail.FileToAttach:=EditFileToAttach.Text; MailDetail.MailTo:=EditMailTo.Text; MailDetail.CC:=EditCC.Text; MailDetail.subject:=EditSubject.Text; MailDetail.Body:=''; for x:=0 to Memo1.Lines.Count-1 do MailDetail.Body:=MailDetail.Body+Memo1.lines[x]+CRLF; OutLookMailProc(MailDetail); end; end.