梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks
package com.roadway.edserver.util;

import java.util.HashMap;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class FetchSMTP {
    
private static FetchSMTP smtpFetcher = new FetchSMTP();
    
public static HashMap<String, String> smtps = new HashMap<String, String>();
    
private FetchSMTP(){};
    
public static FetchSMTP getInstance(){
        
return smtpFetcher;
    }

    
private String getSmtpByAtBackside(String atBackside) {
        String smtpServer 
= null;
        
try {
            Hashtable
<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.dns.DnsContextFactory");
            DirContext ctx 
= new InitialDirContext(env);
            Attributes attrsAll 
= ctx.getAttributes(atBackside);
            Attribute attrMx 
= attrsAll.get("MX");
            String recordMx 
= (String) attrMx.get();
            smtpServer 
= recordMx.substring(recordMx.indexOf(" "+ 1);
        }
 catch (Exception e) {}
        
return smtpServer;
    }


    
public String getSmtpByEmail(String email) {
        String smtp 
= null;
        
if (email != null ) {
            String atBackside 
= email.substring(email.indexOf("@"+ 1, email.length());
            smtp 
= smtps.get(atBackside);
            
if (smtp == null{
                smtp 
= this.getSmtpByAtBackside(atBackside);
                
if (smtp != null{
                    FetchSMTP.smtps.put(atBackside, smtp);
                }

            }

        }

        
return smtp;
    }

    
public static void main(String args[]){
        FetchSMTP f 
= FetchSMTP.getInstance();
        System.out.println(f.getSmtpByEmail(
"hwpok@163.com"));
    }

}
posted on 2008-01-12 20:51 HUIKK 阅读(527) 评论(1)  编辑  收藏 所属分类: Java

评论

# re: 跟据Email 查找 SMTP 服务器[未登录] 2008-01-14 17:14 lucy
完全看不懂呀  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: