Mega Code Archive

 
Categories / Delphi / Activex OLE
 

Sending mail with attachment via Outlook

Title: Sending mail with attachment via Outlook Question: How to send mail with attachment to someone from your database Answer: This is upgrade from my previous article "Mail list", but however I will show you all. First off all you have one database, and you have (for example) customers and you want to send them from time to time some mail with some attachment (for example some newsletter or something else), but you don't want to do copy paste every mail address into outlook, so what should you do. 1) Put text "comobj" (without quotes) into usees list, so now you can use outlook aplication, you must enter this text because aplication will not run without this. 2) On the form put 3 Edit box (Edit1,Edit2,Edit3, in the code you will see why you need this Edit box); Memo box(Memo1, that is for your message to your customers); DriveComboBox (DriveComboBox1, that is for selecting drive from which you will choose attachment); DirectoryListBox (DirectoryListBox1, that is for selecting directory from which you will send attachment); FileListBox (FileListBox1, that is for selecting file that you will send like attachment); If you dont know DriveComboBox,DirectoryListBox,FileListBox you will find in the Win 3.1 on the toolbars. 3) Now after you added everything you need you will to do next a) Click on the DriveComboBox1 and on the Object Inspector in the field DirLis select DirectoryListBox1; b) Click on the DirectoryListBox1 and on the Object Inspector in the field FileList select FileListBox1; c) Click on the FileListBox1 and on the Object Inspector in the field FieldEdit select Edit2; Now you have connected all them and when you select Drive you will see the list of the Directories in that drive and when you select Directory you will see the list of the files in that directory 4) procedure TForm10.Action1Execute(Sender: TObject); //this procedure is attach to Action1, but you can attach following cod for anything you want (Button, Events) const olMailItem = 0; olByValue = 1; var OutlookApp, MailItem: OLEVariant; var i:integer; //this is for counter that will count record from your base begin edit1.Clear; // this is for clean up edit1 i:=0; Datasource1.DataSet.First; //this moves pointer to first record in database, you should change this to your datasource try OutlookApp := GetActiveOleObject('Outlook.Application'); except OutlookApp := CreateOleObject('Outlook.Application'); end; try MailItem := OutlookApp.CreateItem(olMailItem); repeat edit1.Text:=adoquery1Email.DisplayText; //this use edit1 to display text from your field Email, I have use Adoquery, because SQL, but if you have used AdoTable (for example ADOTable1)and want to display text from field (for example) TEST then it will be AdoTable1TEST.DisplayText MailItem.Recipients.Add(edit1.Text); //this is use to add email address to mailitem recipients from edit1 datasource1.DataSet.Next; //this move pointer to next record in your database i:=i+1; until i=adoquery1.RecordCount; //and email adress will be added until last record in database MailItem.Subject := 'Enter subject for your mail'; MailItem.Body :=Memo1.Text; // this is for message //this is code for attach file to mail message if edit2.Text'*.*' then //because edit2 which show name of the file in case that you realy select something to send like attachment applicatin will add that to attach, but if you didn't than you will send just a message from Memo1 begin edit3.Text:=directorylistbox1.GetItemPath(directorylistbox1.ItemIndex)+'\'+ edit2.Text; //this is use to attach name of file that you choose to the edit3 mailitem.attachments.add(edit3.Text); //this is use to add that file to attachment end else begin showmessage('Please wait a moment until database send your mail.'); end; MailItem.Send; Showmessage('Mail send !!!'); // this is for showing message that your mail is send finally OutlookApp := VarNull; // to close outlook aplication end; end; Warning : After you enter this code try to run it, in case that your Outlook Express is not open you will get error message. But do not worry, message will be sent, if you do not belive me try to Compile code ( you will I belive get warrnig message because you use Outlook and DriveComboBox, but dont be worry do Compile one more time after you get warnig message and program will compile, dont worry this cant harm your PC), after that run it (offcource run Nameofyourproject.exe, and enter your mail adress like recipients (offcource into field in your database where email address should be)and you will see that you get the Email message. P.S. Plese send me a mail if you have some question, I promise that I will replay to you with answer or just to say "Sorry but I dont know".