Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/email/csharp$ _

Generate Random Email Addresses in C# - Testing & QA

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: C#

What: Generate random email addresses in C# for testing, QA, and mock data with Bogus library and custom patterns.

Try it: Use our interactive Email generator or integrate this code into your C# application.

Generate random email addresses in C# for testing, QA, and mock data with Bogus library and custom patterns. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C++ , Ruby and Go or use our interactive web generator.

C# Code Example

using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

class EmailGenerator
{
    private static readonly Random random = new Random();
    private static readonly string[] Adjectives = { "happy", "sunny", "clever", "bright", "quick" };
    private static readonly string[] Nouns = { "panda", "tiger", "eagle", "dolphin", "falcon" };

    static void Main()
    {
        // Random Email
        Console.WriteLine($"Random Email: {RandomEmail("example.com")}");

        // Readable Email
        Console.WriteLine($"Readable Email: {ReadableEmail("example.com")}");

        // Name-based Email
        Console.WriteLine($"Name-based: {EmailFromName("John", "Doe", "company.com")}");

        // Test Email
        Console.WriteLine($"Test Email: {TestEmail("test.local")}");

        // Multiple Emails
        var emails = GenerateEmails(3, "example.com");
        Console.WriteLine($"Multiple Emails: {string.Join(", ", emails)}");

        // Validate
        string email = RandomEmail("example.com");
        Console.WriteLine($"Valid: {(IsValidEmail(email) ? "Yes" : "No")}");
    }

    public static string RandomEmail(string domain = "example.com")
    {
        string username = GenerateRandomString(12);
        return $"{username}@{domain}";
    }

    public static string ReadableEmail(string domain = "example.com")
    {
        string adj = Adjectives[random.Next(Adjectives.Length)];
        string noun = Nouns[random.Next(Nouns.Length)];
        int num = random.Next(100, 1000);
        return $"{adj}{noun}{num}@{domain}";
    }

    public static string EmailFromName(string firstName, string lastName, string domain = "company.com")
    {
        string[] patterns = {
            $"{firstName.ToLower()}.{lastName.ToLower()}",
            $"{firstName[0].ToString().ToLower()}{lastName.ToLower()}",
            $"{firstName.ToLower()}{lastName[0].ToString().ToLower()}",
            $"{firstName.ToLower()}_{lastName.ToLower()}"
        };
        string username = patterns[random.Next(patterns.Length)];
        return $"{username}@{domain}";
    }

    public static string TestEmail(string domain = "test.local")
    {
        long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
        int randomNum = random.Next(1000, 10000);
        return $"test_{timestamp}_{randomNum}@{domain}";
    }

    public static List<string> GenerateEmails(int count, string domain = "example.com")
    {
        long baseTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
        return Enumerable.Range(0, count)
            .Select(i => $"user{baseTime}{i}@{domain}")
            .ToList();
    }

    public static bool IsValidEmail(string email)
    {
        string pattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";
        return Regex.IsMatch(email, pattern);
    }

    private static string GenerateRandomString(int length)
    {
        const string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        return new string(Enumerable.Range(0, length)
            .Select(_ => chars[random.Next(chars.Length)])
            .ToArray());
    }

    // Using Bogus library (Install-Package Bogus)
    // using Bogus;
    // var faker = new Faker();
    // string email = faker.Internet.Email();
    // string exampleEmail = faker.Internet.ExampleEmail();
    // string companyEmail = faker.Internet.Email(provider: "company.com");
}

[EXPLANATION]

C# uses Random for number generation and LINQ for collection operations. DateTimeOffset.UtcNow.ToUnixTimeSeconds() provides timestamp-based uniqueness. String interpolation with $"" creates clean email formats. Enumerable.Range() with Select() generates multiple emails efficiently. Regex.IsMatch() validates email format. For production, install Bogus NuGet package (Install-Package Bogus) which provides faker.Internet.Email(), ExampleEmail(), and customizable email generation with proper formatting.

Expected Output

Random Email: k8j2n5p7x9w3@example.com
Readable Email: clevertigerread847@example.com
Name-based: john.doe@company.com
Test Email: test_1703001234_7392@test.local
Multiple Emails: user17030012345670@example.com, user17030012345671@example.com, user17030012345672@example.com
Valid: Yes

Common Use Cases

  • Generate test data for ASP.NET Core applications
  • Create mock users for xUnit/NUnit test suites
  • Populate Entity Framework databases with seed data
  • Generate emails for API integration tests
  • Create test accounts for Selenium/SpecFlow automation

Important Notes

  • Install Bogus: Install-Package Bogus for production-ready emails
  • Use RandomNumberGenerator for cryptographic randomness
  • DateTimeOffset provides timezone-aware timestamps
  • Consider FluentEmail for actual email sending in tests
  • Use test domains (.test, .local, example.com) in test environments

Try Our Interactive Generator

Don't want to write code? Use our free web-based Email generator with instant results.

TRY EMAIL GENERATOR →