Mega Code Archive

 
Categories / Delphi / Activex OLE
 

Sending e mail from Delphi with attachment via Outlook

Title: Sending e-mail from Delphi with attachment via Outlook Question: There have been many questions about how to send an e-mail with an attachment. I came up with the vbs script that did it with Outlook. Well here's the Delphi translation that uses OLE to accomplish the same thing: Answer: Change recipientaddress@recipienthost.com to your own e-mail address and give it a try. You must have Outlook installed, {I'm not sure if this will work with Outlook Express.} uses ComObj; {Delphi 5} procedure TForm1.Button1Click(Sender: TObject); const // OlItemType constants olMailItem = 0; olAppointmentItem = 1; olContactItem = 2; olTaskItem = 3; olJournalItem = 4; olNoteItem = 5; olPostItem = 6; // OlAttachmentType constants olByValue = 1; olByReference = 4; olEmbeddedItem = 5; olOLE = 6; var myOlApp, myItem, myRecipient, myAttachments: OleVariant; begin // VBScript file to create a mail and add an attachment myOlApp := CreateOLEObject('Outlook.Application'); myItem := myOlApp.CreateItem(olMailItem); myItem.Subject := 'This is the Subject'; myRecipient := myItem.Recipients.Add('recipientaddress@recipienthost.com'); myItem.Body := #13; myItem.Body := myItem.Body + #13; myItem.Body := myItem.Body + 'Hello,' + #13; myItem.Body := myItem.Body + 'This code created this message and '+ ' sent it and I didn''t even have' + #13; myItem.Body := myItem.Body + 'to click the send button!!!' + #13; myItem.Body := myItem.Body + #13; myItem.Body := myItem.Body + 'If you have any more problems, let me know' + #13; myItem.Body := myItem.Body + 'rename to blah.vbs and run like this:' + #13; myItem.Body := myItem.Body + 'wscript c:\blah.vbs' + #13; myItem.Body := myItem.Body + #13; myItem.Body := myItem.Body + 'Eddie' + #13; myItem.Body := myItem.Body + #13; myItem.Body := myItem.Body + #13; myItem.Body := myItem.Body + 'const'+ #13; myItem.Body := myItem.Body + ' // OlItemType constants'+ #13; myItem.Body := myItem.Body + ' olMailItem = 0;'+ #13; myItem.Body := myItem.Body + ' olAppointmentItem = 1;'+ #13; myItem.Body := myItem.Body + ' olContactItem = 2;'+ #13; myItem.Body := myItem.Body + ' olTaskItem = 3;'+ #13; myItem.Body := myItem.Body + ' olJournalItem = 4;'+ #13; myItem.Body := myItem.Body + ' olNoteItem = 5;'+ #13; myItem.Body := myItem.Body + ' olPostItem = 6;'+ #13; myItem.Body := myItem.Body + ' // OlAttachmentType constants'+ #13; myItem.Body := myItem.Body + ' olByValue = 1;'+ #13; myItem.Body := myItem.Body + ' olByReference = 4;'+ #13; myItem.Body := myItem.Body + ' olEmbeddedItem = 5;'+ #13; myItem.Body := myItem.Body + ' olOLE = 6;'+ #13; myItem.Body := myItem.Body + #13; myItem.Body := myItem.Body + 'var'+ #13; myItem.Body := myItem.Body + ' myOlApp, myItem, myRecipient, myAttachments: OleVariant;'+ #13; myItem.Body := myItem.Body + 'begin'+ #13; myItem.Body := myItem.Body + ' myOlApp := CreateObject(''Outlook.Application'')' + #13; myItem.Body := myItem.Body + ' myItem := myOlApp.CreateItem(olMailItem)' + #13; myItem.Body := myItem.Body + ' myItem.Subject := ''This is the Subject''' + #13; myItem.Body := myItem.Body + ' myItem.Body := ''This is the body''' + #13; myItem.Body := myItem.Body + ' myRecipient := myItem.Recipients.Add ('recipientaddress@recipienthost.com')' + #13; myItem.Body := myItem.Body + ' myAttachments := myItem.Attachments' + #13; myItem.Body := myItem.Body + ' // Now let''s attach the files...' + #13; myItem.Body := myItem.Body + ' myAttachments.Add ''C:\blah.txt'', olByValue, 1, ''Blah.txt Attachment''' + #13; myItem.Body := myItem.Body + ' myItem.Send' + #13; myItem.Body := myItem.Body + ' myOlApp := VarNull;' + #13; myItem.Body := myItem.Body + ' myItem := VarNull;' + #13; myItem.Body := myItem.Body + ' myRecipient := VarNull;' + #13; myItem.Body := myItem.Body + ' myAttachments := VarNull;' + #13; myItem.Body := myItem.Body + 'end;' + #13; // Now let's attach the files... myAttachments := myItem.Attachments; myAttachments.Add('C:\blah.txt', olByValue, 1, 'Blah.txt Attachment'); myItem.Send myOlApp := VarNull; myItem := VarNull; myRecipient := VarNull; myAttachments := VarNull; end;