954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need Email validation source code (C#)

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

Prasadd
Newbie Poster
7 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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

cambia
Newbie Poster
17 posts since Jul 2005
Reputation Points: 10
Solved Threads: 3
 

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);
cambia
Newbie Poster
17 posts since Jul 2005
Reputation Points: 10
Solved Threads: 3
 

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

UseRegularExpression.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

chirag.3g
Newbie Poster
1 post since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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

joinwithchakra
Newbie Poster
2 posts since May 2010
Reputation Points: 9
Solved Threads: 0
 

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

joinwithchakra
Newbie Poster
2 posts since May 2010
Reputation Points: 9
Solved Threads: 0
 

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

Babitha.p
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

**Thread Locked**

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You