The easiest way to compose a new mail message is to use the BuildMessage method from the MailMessage component. This method automatically creates an appropriate MIME body structure.
A list of the overloaded BuildMessage methods allows you specifying pretty much of all possible combinations of input parameters. Please check out the examples below for more details.
If you decide to add attachments manually, you will have to compose a corresponding MIME body structure yourself.
Please refer the following article for more details Adding text and attachments to the E-mail message
If you call to the BuildMessage method with a non-empty AText parameter only, a simgle message with one body part will be generated.
clMailMessage.BuildMessage('Hello World!', '');
clMailMessage.From.Email := 'john @ domain.com';
clMailMessage.ToList.Add('steve @ another.com');
clMailMessage.Subject := 'Hello';
messageSource := clMailMessage.MessageSource; |
This method creates new email in Plain Text format:
Show Message Source
From: john @ domain.com
To: steve @ another.com
Subject: Hello
Date: Fri, 31 Oct 2003 7:00:22 +0300
Content-Type: multipart/mixed;
X-Priority: 3
X-MSMail-Priority: Normal
X-MimeOLE: Clever Internet Suite
Hello World! |
If you call to the BuildMessage method with both non-empty AText and AHtml parameters, a multi-part alternative message with two body parts will be generated.
clMailMessage.BuildMessage('Hello World!', '<html><body><p>Hello World!</p></body></html>');
messageSource := clMailMessage.MessageSource; |
The method above creates multipart message in Rich Text (HTML) format:
Show Message Source
From:
To:
Subject:
Date: Fri, 31 Oct 2003 7:00:22 +0300
Content-Type: multipart/alternative;
boundary="----=_NextPart_37929.3334428704.31"
X-Priority: 3
X-MSMail-Priority: Normal
X-MimeOLE: Clever Internet Suite
This is a multi-part message in MIME format.
------=_NextPart_37929.3334428704.31
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!
------=_NextPart_37929.3334428704.31
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<html><body><p>Hello World!</p></body></html>
------=_NextPart_37929.3334428704.31-- |
If you call to the BuildMessage method with both non-empty AText and AHtml parameters where AHtml contains image including, the resulting message will be in Rich Text (HTML) format with embedded images:
clMailMessage.BuildMessage('Hello World!',
'<html><body><p>Hello World!</p><img src="c:\pictures\icon.gif"></body></html>',
['c:\pictures\icon.gif'], []);
messageSource := clMailMessage.MessageSource; |
As a result, the following message will be generated:
Show Message Source
From:
To:
Subject:
Date: Fri, 31 Oct 2003 7:00:22 +0300
Content-Type: multipart/related;
type="multipart/alternative";
boundary="----=_NextPart_37923.2275540972.31"
X-Priority: 3
X-MSMail-Priority: Normal
X-MimeOLE: Clever Internet Suite
This is a multi-part message in MIME format.
------=_NextPart_37923.2275540972.31
Content-Type: multipart/alternative;
boundary="----=_NextPart_37923.2275540972.861"
------=_NextPart_37923.2275540972.861
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!
------=_NextPart_37923.2275540972.861
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<html><body><p>Hello World!</p><img src="cid:icon.gif@37923.2275540972.0"></body></html>
------=_NextPart_37923.2275540972.861--
------=_NextPart_37923.2275540972.31
Content-Type: image/gif;
name="icon.gif"
Content-Transfer-Encoding: base64
Content-ID: <icon.gif@37923.2275540972.0>
R0lGODlhEAAQAMQWAMDAwN+QAD8/P2BgYHBwAP//T/9QAK+vr09PT/+vIFAAAAwMDL9/AH9/f29v
b/+/T1AwAODg4PDw8P+vAP///wAAAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BA
EAABYALAAAAAAQABAAQAVvoCWOVkVajSJWUgNBzyRPkCNFy1hRkjQEMQbiFJAJHIBexHTS8SS
VX4LhWFEAhEmhEJNBmLouDXkDQ466ZbO5YzYaCchp18MGq85WtjsA0xsyBmI0UDoSAGIMAgcRai
MBBhOKjGUkEJJDLJVrc2shADt=
------=_NextPart_37923.2275540972.31-- |
With BuildMessage you can easily add any number of file attachments to all message formats mentioned above.
clMailMessage.BuildMessage('Hello World!', ['c:\archives\file1.zip', 'c:\archives\file2.zip']);
messageSource := clMailMessage.MessageSource; |
This method creates new email in Plain Text format with one attached file:
Show Message Source
From:
To:
Subject: test
Date: Thu, 31 Aug 2006 14:18:28 +0400
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_NextPart_000_000A_01C6CD08.54348B80"
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Clever Internet Suite
X-MimeOLE: Clever Internet Suite
This is a multi-part message in MIME format.
------=_NextPart_000_000A_01C6CD08.54348B80
Content-Type: text/plain;
charset="koi8-r"
Content-Transfer-Encoding: 7bit
Letter with attachments
------=_NextPart_000_000A_01C6CD08.54348B80
Content-Type: application/octet-stream;
name="file.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="file.zip"
UEsDBAoAAAAAAEZyHzX1c6f2BgAAAAYAAAAIAAAAZmlsZS50eHRTYW1wbGVQSwECKAAK
ch819XOn9gYAAAAGAAAACAAkAAAAAAAAACAAAAAAAAAAZmlsZS50eHQKACAAAAAAAAE
5szGASBek8LmzMYBIF6TwubMxgFQSwUGAAAAAAEAAQBaAAAALAAAAAAA
------=_NextPart_000_000A_01C6CD08.54348B80
Content-Type: application/octet-stream;
name="file2.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="file2.zip"
ch819XOn9gYAAAAGAAAACAA50eHQKACAAAAAAAAEkAAAAAAAAACAAAAAAAAAAZmlsZS
UEsDBAoAAAAAAEZyHzX1c6f2BgAAAAYAAAAIAAAAZmlsZS50eHRTYW1wbGVQSwECKAAK
------=_NextPart_000_000A_01C6CD08.54348B80-- |
|