Sending email messages in .Net

CK CK 3788 
Created at
Updated at  
1,033 0 0

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.
Tags EncodeURI MailMessage SmtpMail SmtpServer create a thumbnail Facebook X
Comments 0
Similar posts
  1. Implementing C#'s foreach loop in Delphi 8
    459
  2. Delphi for .Net Code Folding keyboard shortcuts
    527
  3. How to encrypt a string
    466
  4. Creating thumbnail images
    861
  5. Technology sharing related with software development. Specially content will be focused to programming language, and embedded systems.
    850
  6. Send Email Using C#
    1,377
  7. How to send email by TIdSMTP VCL ?
    1,018
  8. How to send text/html based email in UTF-8 with TIdSMTP, TIdMessage VCL
    1,077
  1. Delphi Pointer Types
    21,801
  2. How to read/wrtie INI file in Delphi ?
    3,098
  3. How to get file created time, modified time, and last accessed time ?
    3,320
  4. Delphi string conversion functions - AnsiToUTF-8, UTF8Encode, ...
    643
  5. When subject is crashing in TIdSMTP VCL
    616
  6. How to send text/html based email in UTF-8 with TIdSMTP, TIdMessage VCL
    1,077
  7. How to encode subject content when sending mail with TIdMessage ?
    1,072
  8. Base64 Encoding/Decoding function for Delphi
    1,153
  9. How to send email by TIdSMTP VCL ?
    1,018
  10. How to return exit code such as exit() function in C/C++ ?
    662
  11. How to get parameter string ?
    1,238
  12. Save TBitmap image to Jpeg format image in Delphi
    712
  13. How to get screen resolution in case of using multiple monitors ?
    450
  14. How to turn off monitor ?
    427
  15. Find File at certain directory in Delphi
    437
  16. Delphi for .Net Code Folding keyboard shortcuts
    527
  17. How to encrypt a string
    466
  18. Creating thumbnail images
    861
  19. Technology sharing related with software development. Specially content will be focused to programming language, and embedded systems.
    850
  20. How to speed up access to the RichTextBox Lines property
    830
  21. Window flashing
    379
  22. How to use TTF Font Without Installing
    1,116
  23. How to show window contents while dragging
    453
  24. How to save text from Clipboard to a file
    568
  25. How to send char as message to another application
    543
  26. How to restrict mouse movement
    408
  27. List Devices (LPT, COM ports, ...)
    478
  28. How to know whether the font is TrueTypeFont or not
    375
  29. How to track a user's idle time
    588
  30. How to remove your application from TaskBar
    655
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    39
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    34
  3. How do I create animated background for Google Meet?
    20,611
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    244
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    514
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    287
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    230
  16. TPU & GPU Cluster Management Tools
    243
  17. The Concept of Scaffolding in AI Agents
    233
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    258
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    221
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    220
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    195
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    216
  30. Understanding Data Lineage
    260