Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5326104
  • 博文数量: 671
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 7310
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-14 09:56
文章分类

全部博文(671)

文章存档

2011年(1)

2010年(2)

2009年(24)

2008年(271)

2007年(319)

2006年(54)

我的朋友

分类: C/C++

2008-08-26 11:12:59

Introduction

When we have received a task from the manager to create a system for sending different kinds of messages for thousands of our customers, first of all we have to validate the email addresses which are stored in our database.

So how do you reliably determine if an email address is correct or incorrect before sending messages? The answer is – you can't. But you can reduce the count of wrong addresses and eventually do it after sending.

This is an article regarding this issue.

Three Ways of Validation

I have found two ways of validation in the MSDN library. The first way is by using the Regex class from System.Text.RegularExpressions namespace for spell checking.

Regex mailRegex=new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

But it checks only the spelling format of addresses. It can't answer if an address really exists or not.

The second way is to check the answer of MAIL server by using the SMTP protocol. The problem is that .NET Framework does not have the classes for this protocol, but you can find them .

The name of the project is .

In this way, you can check the address before sending. It gets the connection with the SMTP Server, then sends the message to the server for validation. Then the server does the validation and answers if it's incorrect, it returns the error message. But actually it always checks only the spelling and existing server name. It means it does not check the name of the email box. or are always correct, but really do not exist. However, the server answers that the addresses exist.

Most SMTP servers will accept mail addressed to just about anyone in their domain, and only later figure out that the user does not exist. That means that whatever app you use to send mail will almost never know that there is a problem.

Only the third way can answer exactly whether the address is correct or not. This is the way of sending messages and receiving bounced messages.

After sending a message, if the address does not exist, the MAIL Server sends the answer: bounced messages with special subject. It is somewhat like "Delivery Status" or "Failure Notice" message. Our task now is to retrieve this message by using the POP3 protocol and recognize what is wrong. Actually this issue was described in another article on The Code Project: "".

But the other problem appears here. .NET 2.0 does not have a class for working with POP3. It is ridiculous but…

In the article stated above, the author uses the library EasyMail which does the entire job with POP3.

It is not convenient, because we cannot see legally what is inside and need to use the LicenseKey of the library. I have decided to create my own class for this task. For this, I use the other article from The Code Project: "".

It's a pretty interesting library but it has a few bugs. I did not fix them. I have cut some functionality. It is O.K. for our task because validation is not necessary to parse all messages. You can see it in the example.

That is all. After reading bounced messages, we can change the information in the database and never send anything anymore to those addresses.

History

  • 9th February, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under

阅读(1171) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~