|
Technical answers from the trenches |
|
Sending Email (and Files)
| ||||
Posted: 31 January 2002 |
||||
  |
Applies to: Delphi 4 and later |
|||
  |
Audience: Intermediate |
|||
Question: How do I send email from a Delphi application?Answer: There are a few different ways to do this, but one of the most widely-supported is to use Delphi's built-in support for the Windows Simple Mail API (MAPI). The following code sample shows one way you can using this in your own applications: function sendMail( const TargetName, TargetAddr,
SenderName, SenderAddr,
MsgSubject, MsgContent,
Attachment : String;
PreviewMsg : Boolean = TRUE ) : Integer;
var
msg : TMapiMessage; // Pointer to the message itself
mrdSender, // Who's sending it?
mrdTarget : TMapiRecipDesc; // Who's going to get it?
mfdAttach : TMapiFileDesc; // The attached file.
liFlags : Longint; // Flags for MAPI.
strError : String; // Holds MAPI error results;
begin
result := 0;
liFlags := 0;
fillChar( msg, sizeOf( msg ), 0 );
with msg do
begin
if TargetAddr = '' then
raise tMailFileException.createFmt( ERROR,
[ 'Target email address not specified' ] )
else begin
if TargetName = '' then
mrdTarget.lpszName := pChar( TargetAddr )
else
mrdTarget.lpszName := pChar( TargetName );
mrdTarget.ulRecipClass := MAPI_TO;
mrdTarget.lpszAddress := pChar( TargetAddr );
mrdTarget.ulReserved := 0;
mrdTarget.ulEIDSize := 0;
mrdTarget.lpEntryID := NIL;
nRecipCount := 1;
lpRecips := @mrdTarget;
end;
if SenderAddr = '' then
raise tMailFileException.createFmt( ERROR,
[ 'Sender email address not specified' ] )
else begin
if SenderName = '' then
mrdSender.lpszName := pChar( SenderAddr )
else
mrdSender.lpszName := pChar( SenderName );
mrdSender.ulRecipClass := MAPI_ORIG;
mrdSender.lpszAddress := pChar( 'SMTP:' + SenderAddr );
mrdSender.ulReserved := 0;
mrdSender.ulEIDSize := 0;
mrdSender.lpEntryID := NIL;
lpOriginator := @mrdSender;
end;
if MsgSubject = '' then
lpszSubject := 'The above example provides a wrapper function that calls Delphi's MAPI unit. It's designed to be placed in a library unit and called using something like this: uses mapimail;
// ...other stuff
procedure TForm1.btnSendMailClick(Sender: TObject);
begin
sendMail( edtTargetName.Text, edtTargetAddr.Text,
edtSenderName.Text, edtSenderAddr.Text,
edtMsgSubject.Text, mmoMsgContent.Text,
edtAttachment.Text, chkPreviewMsg.Checked );
end;For additional information, please consult the Microsoft Win32 Messaging (MAPI) Reference Help file installed with Delphi. If you're using the Professional or Enterprise editions of Delphi, you can also review the source of Delphi's MAPI unit in source/rtl/Win/mapi.pas. Please note the following:
|
|||
|
||||||||
|
Copyright © 2000-2004, techtricks.com; All Rights Reserved. Acknowledgements, Disclaimers, Terms and Conditions. |
||||||||
|
Article last updated on 31 May 2003
|
||
|
|
||
|
[- End -]
|