| Sending an e-mail message (even with attachments) in Delphi for .Net (ASP.NET or WinForms) is very simple. You don't have to learn complicated syntaxes and other commands to achieve the task. The System.Web.Mail namespace provides the classes for sending email in .NET. MailMessage class manages the mail message contents; SmtpMail class sends email to the mail server.
Here's a sample code: uses System.Web.Mail;
...
var
MailMessage : System.Web.MailMessage;
begin
mailMessage := MailMessage.create;
try
with mailMessage do
begin
From := 'delphi.guide@about.com';
&To := 'someone@somewhere.net';
Subject := 'This is the subject line';
Body := 'this is the mail body text;
BodyFormat := System.Web.Mail.MailFormat.Text;
end;
SmtpMail.SmtpServer := 'SMTPSERVER NAME';
SmtpMail.Send(mailMessage) ;
except on e : Exception do
MsgResult.Text := 'Error occured!';
end;
end;
Key Takeaways:
- Simplicity: Sending emails in .NET is straightforward, requiring minimal complex syntax.
- Built-in Functionality: The `System.Web.Mail` namespace provides the necessary classes for email management.
- Key Classes: The `MailMessage` class handles message content, while the `SmtpMail` class manages sending to the mail server.
- Code Structure: The sample code demonstrates creating a `MailMessage`, configuring its attributes, and sending it using `SmtpMail`.
- Error Handling: A `try...except` block is recommended to gracefully handle potential errors during the email sending process.
|