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

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:
Email Validation

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

Edited 3 Years Ago by happygeek: fixed formatting

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");

Edited 6 Years Ago by joinwithchakra: n/a

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

Edited 6 Years Ago by joinwithchakra: n/a

Comments
Don't bump old threads

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

This article has been dead for over six months. Start a new discussion instead.