---
title: "How do you send an email using SmtpClient in C#?"  
description: "How do you send an email using SmtpClient in C#?"  
author: "ICSM Computer"  
published: 2025-05-30  
updated: 2025-05-30  
canonical: https://www.mindstick.com/interview/34185/how-do-you-send-an-email-using-smtpclient-in-c-sharp  
category: "c#"  
tags: ["c#", "mail"]  
reading_time: 3 minutes  

---

# How do you send an email using SmtpClient in C#?

To send an email using `SmtpClient` in C#, you typically need:

1. The SMTP server address and port
2. A sender and recipient email address
3. Credentials if the SMTP server requires authentication

### Basic Example: Sending an Email with `SmtpClient`

```cs
using System.Net;
using System.Net.Mail;

class Program
{
    static void Main()
    {
        var fromAddress = new MailAddress("your-email@example.com", "Your Name");
        var toAddress = new MailAddress("recipient@example.com", "Recipient Name");
        const string subject = "Test Email";
        const string body = "This is a test email sent from C#.";

        var smtp = new SmtpClient
        {
            Host = "smtp.example.com", // e.g., smtp.gmail.com
            Port = 587, // Or 465 for SSL
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential("your-email@example.com", "your-email-password")
        };

        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body
        })
        {
            smtp.Send(message);
        }
    }
}
```

### Security Note (especially for Gmail and others):

1. If you're using Gmail or similar services:
2. Enable **App Passwords** if 2FA is on.
3. Use `smtp.gmail.com` with port `587` (TLS) or `465` (SSL).
4. Avoid hardcoding credentials—use a secure store or environment variables.

### Async Version

Use this in modern apps (e.g., ASP.NET MVC, WPF):

```cs
await smtp.SendMailAsync(message);
```

## Answers

### Answer by ICSM Computer

To send an email using `SmtpClient` in C#, you typically need:

1. The SMTP server address and port
2. A sender and recipient email address
3. Credentials if the SMTP server requires authentication

### Basic Example: Sending an Email with `SmtpClient`

```cs
using System.Net;
using System.Net.Mail;

class Program
{
    static void Main()
    {
        var fromAddress = new MailAddress("your-email@example.com", "Your Name");
        var toAddress = new MailAddress("recipient@example.com", "Recipient Name");
        const string subject = "Test Email";
        const string body = "This is a test email sent from C#.";

        var smtp = new SmtpClient
        {
            Host = "smtp.example.com", // e.g., smtp.gmail.com
            Port = 587, // Or 465 for SSL
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential("your-email@example.com", "your-email-password")
        };

        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body
        })
        {
            smtp.Send(message);
        }
    }
}
```

### Security Note (especially for Gmail and others):

1. If you're using Gmail or similar services:
2. Enable **App Passwords** if 2FA is on.
3. Use `smtp.gmail.com` with port `587` (TLS) or `465` (SSL).
4. Avoid hardcoding credentials—use a secure store or environment variables.

### Async Version

Use this in modern apps (e.g., ASP.NET MVC, WPF):

```cs
await smtp.SendMailAsync(message);
```


---

Original Source: https://www.mindstick.com/interview/34185/how-do-you-send-an-email-using-smtpclient-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
