Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I send an email using Delphi?

E-Mail

How do I send an email using Delphi?

by  Stretchwickster  Posted    (Edited  )
This question has been asked frequently in the last few months so I thought it might be useful to collate the information in one central place.

There are two techniques that I know of, by which you can facilitate the sending of email from a Delphi application:
1) by using the simple "mailto" protocol
2) by using the MAPI

mailto
This is the simplest technique for sending an email message. It makes use of the default mail application on your computer. A disadvantage is that attachments are not always supported by the default mail application and errors and truncation occur when using Win 9x. ShellAPI will need to be added to the uses clause of the application. Here is the syntax (N.B. I have divided up the string into sections to aid readability):
Code:
ShellExecute(Self.Handle, 
             nil,
             'mailto:' +
             'me@me.com' +
             '?Subject=Test Message Subject' +
             '&Body=Test Message Body' +
             '&Attach="c:\Mail Attachments\attachment.txt"',
             nil, 
             nil, 
             SW_NORMAL);
This code has the effect of opening your default mail application, populating the "To:", "Subject:" and body sections of a new mail message with the values specified in the mailto protocol, ready for you to send. The attach line will have the effect of attaching a file at the specified location (this final line may not work on all mail programs but does function correctly on Outlook)

References:
http://msdn.microsoft.com/library/default.asp?url=/workshop/networking/predefined/mailto.asp
http://www.latiumsoftware.com/en/delphi/00041.php
http://www.marcocantu.com/code/md6htm/MailGen.htm
http://www.scalabium.com/faq/dct0049.htm

Messaging Application Programming Interface (MAPI)
This is definitely my preferred method as it doesn't rely on the use of a default mail application and is far more reliable across a range of windows platforms. I won't specify the code here but have provided links in the References section that follows, in which there are two excellent examples of its' use.

References:
http://www.delphifaq.com/fq/q5012.shtml
http://techtricks.com/delphi/sendmail.php (a contribution from our very own Lance Leonard aka footpad)

Other things to look into using the Delphi help:
- TSendMail
- Indy Components: TIdMessage & TIdSMTP (see http://www.marcocantu.com/code/md6htm/SendList.htm for an example)

Please feel free to send me corrections/suggestions/comments concerning this FAQ.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top