Discussion in 'C, C++, C#' started by kytro360, Aug 10, 2011.
How can I make my program verify emails?
You can attempt to verify an email is correct by constructing a regular expression in C#. Regular expressions is a method for pattern matching closed under regular language (see computational theory). Regular expressions (regex for short) has a set of rules which one must understand before using the language. Here are some common examples of character classes in regex using some of the major operators:
[0-9] matches any single digit
[0-9]? matches none or 1 digit
[0-9]* matches none or more digits
[0-9]+ matches 1 or more digits
[0-9a-zA-Z_]+ matches a string of characters containing any number 0-9 and any letter (either lowercase or uppercase) and _'s. An example match for this regex could be 01a8adji_9sd
Let's choose an example regex:
This would an extremely (and over simplified) regex for phone numbers (no area code, assuming xxx-xxxx format)
This post isn't complete, I cannot yet post links and thus my original response was throwing moderator warnings. In process of re-writing solution.
As a follow up to my original post:
Now, email addresses are a bit more complicated and we still need to know how to apply regex in C#. The code will look something like this:
NOTE: Since my regex looked similar to an email, the filters prevent me from submitting it. As a result, I have put my code at a separate location. I apologize for the inconvenience. Code at:
pastebin /37nKMQRk -- note, this is actually a link to pastebin . sea oh em, slash, 37nKMQRk.
Sorry, still cannot post links -- it's pretty ridiculous. Hope it works for you
Ultimately, the regex you want will follow these rules:
1. An email starts with any number of digits, letters, underscores, or periods, etc.
2. Followed by a .
3. Followed by any number of digits, letters, underscores, or periods, etc.
4. Followed by an at symbol
5. Followed by any number of digits, letters, underscores, or periods, etc.
It depends on the level of accuracy you need. As per the RFC 822 compliant regex spec, there isn't a master formula which will match all emails. There are many exceptions to the above regex, like emals containing +'s in them, etc.
Hope this at least points you in the right direction!
As Baybo.it suggest you can use regex.
If you want to verify that the user is valid you can send out an email with a verification link and then make the user click it.
If you want detailed source code or sample reply.
Thanks Baybo. Quick question is the code to verify if an email is valid? What Im trying to do is an email verification so for example my program goes into my users email and clicks the links in certain emails to verify their accounts on several sites. Sorry for the mixup :/
If you are trying to verify that an email actually belongs to a user, and not that the email itself is of valid format, dangerbear offers a good solution.
You can automate the process of sending the end-user a verification email with a unique generated link (which can only be access via their email). If you do this, you can verify that the email is active and belongs to the correct user.
As a programmer, I tend to develop these email activation messages with a home-built solution, however, there are several services for email registration, like constantcontact.
Can you tell us a little more about your situation / budget / skill set and maybe we can offer you further guidance?
Also, when you say writing a program to verify emails, are you suggesting you'd like to write a macro or program which goes through YOUR inbox and clicks the link to certain activation messages? Or sending out email activations to users of some service / on some mailing list?
Sorry for the initial misunderstanding and best of luck with email verification!
Well Im am trying to program my own SEO automation program. Basically it helps you build backlinks. Basically it lets you create accounts on several sites and I want my program to go to Hotmail, Yahoo, GMail,etc. and click on the emails they received for registering on the site sites and a certain link inside the email to confirm their accounts.
I see. You will have to integrate with each of the authentication modules for each individual email server. For instance, you may consider writing a binding for google's gmail over IMAP or SMTP using OAuth (assuming a binding doesn't already exist for c#, which I'm sure it does). In general, this is a non trivial problem as you have to interface with a variety of secure interfaces.
Essentially what you'd have to do is produce a script which performs a secure login via the method published by each provider (yahoo, google, etc) and then write a script which GETs or POSTs each request internally. You'll then need to use some mail client package (sendmail is the common one) to send out an email. Checking mail can be done fairly easily if you have access to the account, simply my iterating over the messages. There really is no concept of 'clicking' though. Instead, when a message is queried and accessed, this is equivalent to it being 'read'.
It depends how you want to accomplish this. If you intend for this to be a standalone program which anyone can run on any machine, you're in for a long development process -- this is not trivial.
However, if you are simply trying to write a script which parses the mail on your local machine and can provide the access credentials manually without interfacing with an API, it shouldn't be too challenging at all.
I hope this sheds a little light on the problem.
Can you point me to some tutorials on how to accomplish this? I havent worked with POP3 clients at all.
Also is it possible to do this in the webbrowser?
You can do it with webbrowser, but just reading the mail from pop3 is much easier. If you are going to use gmail not that they require a ssl connection and there are special modifications to the normal packages for reading pop3.
Basically you would parse the pop3 looking for the email with the activation link. Then you would trigger a webbrowser or httpwebrequest to trigger the activation link.
That link should get you started
Thanks @Bots for the article. It should give me some insight hopefully
What a lot of people do, thought it's a very dirty solution is basically simulate a web browser using native components in C#. I had a job where I had to port one of those bots from .Net (Windows) to Mono (Linux).
So that's a possible solution, but the cleanest solution would be to try to "reverse engineer the whole process" and write a custom bot to do so.
If you have any experience with Python, a few good libs for writing bots are Urllib and Urllib2, BeautifulSoup ...
I tried to do it in the webbrowser but i couldnt figure it out
before using regex , you need to decode email (base16,base64,quoted printable etc...).
this is the hardest part i encountered when automating validation as there is no .Net library to decode/encode them.
There are many examples of how to do it online and I remember finding two dlls with guide. I use the ImapX dll, am not sure this is the home page, but looks good
No point using a webbrowser, just pull the page
WebClient webClient = new WebClient();
WebClient webClient = new WebClient();
This is a little bit older but it might give you a push in the right direction:
And here's an even older POP3 client in C#, might give you a starting point:
Chilkat OP or straight sockets thru SSL will get the thing done if you can access pop3 / imap.
Chilkat is not for free... Use IMAPX instead. very easy to use, very handy and the best thing its for free.. from my personal experience I prefer to use IMAP to download the emails from the server. POP3 will just download the emails and then automatically delete them from the server which is not a good choice if you have stored important email and you don't want to lose it. still not all email providers support IMAP for free but the best ones do support that i.e. Gmail and Yahoo Mail Plus (its not for free) but with some simple hacks you can make it available for free.. as for MSN as far as I know it doesn't support IMAP. but it does support POP3 with limited functionality...
so go for IMAPX it does support search and more cool stuff...
Separate names with a comma.