Hi,

I am working on a Desktop application using C# .NET

Looking for some code that does 'email syntax validation' and 'send mail' tasks.
Since it is not a web based application this cannot be done using Web based namespaces etc.


Can anyone please help me out or even suggest sumthing..

Thanx
- Prasadd

Dani AI

Generated

Short summary and practical choices for a desktop C# app: syntax validation and sending are separate problems. Many replies here (for example , and ) used regular expressions; regexes can work for quick checks but are brittle and easy to get wrong for uncommon-but-valid addresses. A more robust, simple-first approach is: 1) use the framework parser to validate syntax, 2) if you need deliverability, verify the domain (MX) or use a validation API, and 3) send mail with a proper SMTP client while keeping the UI responsive.

A compact, reliable syntax check using the framework parser:

bool IsValidEmail(string email)
{
    if (string.IsNullOrWhiteSpace(email)) return false;
    try
    {
        var addr = new System.Net.Mail.MailAddress(email);
        return addr.Address == email; // rejects strings that contain a display name
    }
    catch (FormatException) { return false; }
    catch (ArgumentNullException) { return false; }
}

For sending from a desktop app, prefer the built-in SMTP client on .NET Framework; for modern .NET consider a maintained library (MailKit) if you need advanced features or async streams. Simple send example (synchronous; avoid on UI thread):

using (var client = new System.Net.Mail.SmtpClient("smtp.example.com", 587))
{
    client.EnableSsl = true;
    client.Credentials = new System.Net.NetworkCredential("user","pass");
    var msg = new System.Net.Mail.MailMessage("from@example.com","to@example.com","Subject","Body");
    client.Send(msg);
}

Troubleshooting notes and cautions: whitelisting TLDs or providers (as in ) is fragile. Syntax checks do not prove deliverability — MX/DNS checks or a validation service are needed for that. Common send failures come from wrong port/SSL, blocked outbound SMTP, or bad credentials; check inner exceptions and use async sending or background threads to avoid freezing the UI. If you need help adapting any of this to your target .NET version or to add MX checks or async sending, the thread posts give good starting points to build from.

Recommended Answers

All 7 Replies

Hi Prasadd,

1) First the email syntax validation.

I have written an article on this at the following link. In short, you can use regular expressions:

2) Send email from a desktop application.

You can use the SmtpMail class just as you would in an ASP.NET application. You just need to add a reference to the System.Web.dll library. If you are using Visual Studio .NET, go to the Solution Explorer and right click 'References' in your project. Choose to Add Reference. Then browse the .NET tab for System.Web.dll and add it. Then the following should work.

System.Web.Mail.SmtpMail.SmtpServer = "your server";
string to = "you@email.com";
string from = "someone@email.com";
string subject = "Email Test";
string body = "Test Body";
Sysetm.Web.Mail.SmtpMail.SmtpServer.Send(to, from subject, body);

Cheers,
Steve

Oops. The last line should read

System.Web.Mail.SmtpMail.Send(to, from subject, body);
System.Web.Mail.SmtpMail.SmtpServer = "your server";
string to = "you@email.com";
string from = "someone@email.com";
string subject = "Email Test";
string body = "Test Body";
Sysetm.Web.Mail.SmtpMail.SmtpServer.Send(to, from subject, body);

hi, Prasadd

Use RegularExpression.RegEx class
here is example for Email validation that i have made try it.

public static bool ValidEmail(string validatingstring, string displaymessage)
{
if (!Regex.Match(validatingstring, @"^[a-zA-Z][a-zA-Z0-9_-]+@[a-zA-Z]+[.]{1}[a-
zA-Z]+$").Success)
{
//Email was incorrect
ErrorMessage(displaymessage);
return false;
}
else
{
return true;
}
}

Regards,
Chirag Shah

I'm not satisfied with your example...
To validate Email id in c#

using System.Text.RegularExpressions;

Coding:

if (!Regex.Match(textBox1.Text, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*").Success)
MessageBox.Show(textBox1.Text+" is Invalid Email");
else
MessageBox.Show(textBox1.Text+" is valid Email");

I'm not satisfied with your example...
To validate Email id in c#

using System.Text.RegularExpressions;

Coding:

if (Regex.Match(textBox1.Text, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*").Success)
MessageBox.Show("valid Email");
else
MessageBox.Show("Invalid Email");

Chakrapani,
Hyderabad

commented: Don't bump old threads -1

string strRegex = @"^([a-zA-Z0-9\.]+)@(|yahoo|gmail|).(|com|in|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|)$";

**Thread Locked**

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.