기존의 웹에서 구한 MX Record를 얻어오는 모듈에 문제가 좀 있는 것 같아서 좀 더 찾다가 발견한 글이다.
글 서두에 보면, .Net Framework은 많은 native APIs의 wrapper를 제공하는데, 모두를 제공하는 것은 아니라고 한다. 그래서 MX Record를 얻어오는 빠져있는 것인거 같다.
Java의 경우는 JDK에 포함되어 있다고 하던데...
하여간 그래서 아래의 소스는 실제 MX Record를 얻어오는 코드가 아니고, 실제 구현이 되어 있는 'Dnsapi.dll'의 메소드를 call해 주는 소스인 것 같다.
Build a C# DNS MX (Mail Exchange) Record Query Class by Peter A. Bromberg, Ph.D. |
The .NET Framework provides wrappers to native APIs for a very large number of classes and functions, including the System.Net.Dns classes. However, it doesn't cover all of them. One useful API is the set of DNS query methods in the Dnsapi.dll library, and a particularly useful technique of note is the ability to query a domain and get back the list of MX (Mail Exchange) records of the mail servers associated with the domain. Once you have the code completed to handle this, you will be able to validate an email address against its domain for the mail exchange ("MX") type entries. If there aren't any, then you have a pretty good idea that the email address you are dealing with is BOGUS. Obviously, this can be helpful in tasks such as verifying user registrations on your web site and to fight SPAM. The implementation I provide here is a simplified and very "narrow" one that is intended solely to deal with the MX record lookup. If you want a more complete implementation, there are a number of examples in the Net-o-sphere. One interesting one can be found here. For starters, it might be wise to visit the Platform SDK section on DNS to familiarize ourselves with what we need to create a managed C# PInvoke call to the underlying method. As can be seen, there are three DnsQuery calls, with DnsQuery_W (Unicode) being the most commonly used. There is an fOptions parameter where the types of queries can be combined, the wType type of record parameter, and a ppQueryResultsSet [in, out] pointer parameter. Finally, we are told that we need to free the returned RR Sets with the DnsRecordListFree API call. Note that in the sample code implementation below, VBByRefStr is used for the marshaling directive. The argument is treated as "byref" on the managed side, but stays "byval" on the unmanaged side. If the unmanaged side writes into the buffer, it won't mangle the managed string, which remains immutable. Instead, a different string is propagated to the managed side, preserving managed string immutability. In VB.Net, you could code as if the VBByRefStr is actually byval on the managed side, and the compiler will clean up after you, but in C# you must explicitly add the "ref" keyword in the right places:
The downloadable sample solution includes a simple Console Tester app that makes the following call: static void Main(string[] args) which will return the following output: Remember, hamburger@hamburgerhal.com is a valid email address. But, that doesn't mean that you can actually send mail to it. This handy class, along with some good REGEX match strings, should be sufficient to validate most any email address that can be thrown at you. The downloadable solution has more user-friendly exception handling built in.
Download the Visual Studio.NET Solution that accompanies this article |
'programming > c#' 카테고리의 다른 글
[펌] 다중처리를 위하여 사용하는 비동기 구현 (0) | 2005.03.03 |
---|---|
[펌] C# Coding Guidelines (Microsoft) (3) | 2005.02.02 |
[펌]C# Documenting and Commenting (0) | 2005.01.25 |