OSDN Git Service

CVS最新版の全ファイルを追加
[spring-ext/ozacc-mail.git] / sandbox / src / com / ozacc / mail / util / MXCheck.java
1 package com.ozacc.mail.util;\r
2 \r
3 import javax.mail.internet.AddressException;\r
4 import javax.mail.internet.InternetAddress;\r
5 import javax.naming.NamingEnumeration;\r
6 import javax.naming.NamingException;\r
7 import javax.naming.directory.Attributes;\r
8 import javax.naming.directory.DirContext;\r
9 import javax.naming.directory.InitialDirContext;\r
10 \r
11 /**\r
12  * DNS ¤Ë MX ¥ì¥³¡¼¥É¤¬ÅÐÏ¿¤µ¤ì¤Æ¤¤¤ë¤«¤ò¥Á¥§¥Ã¥¯¤¹¤ë¥¯¥é¥¹¡£\r
13  * Sun JDK 1.4 °Ê¾å¤Ç»ÈÍѤǤ­¤Þ¤¹¡£\r
14  * <p>\r
15  * TODO: Ì¤´°À®¡£Àµ¤·¤¯Æ°ºî¤·¤Þ¤»¤ó¡£\r
16  * \r
17  * @since 1.1\r
18  * @version $Id: MXCheck.java,v 1.1 2004/09/14 22:27:57 otsuka Exp $\r
19  * @author Tomohiro Otsuka\r
20  */\r
21 public class MXCheck {\r
22 \r
23         public static boolean isValidEmailAddress(String email, String dns) {\r
24                 try {\r
25                         new InternetAddress(email, true);\r
26                 } catch (AddressException e) {\r
27                         return false;\r
28                 }\r
29 \r
30                 int pos = email.lastIndexOf("@");\r
31                 String hostName = email.substring(pos + 1);\r
32                 return hasMXRecord(hostName, dns);\r
33         }\r
34 \r
35         /**\r
36          * »ØÄꤵ¤ì¤¿ host ¤Ë MX ¥ì¥³¡¼¥É¤¬´ØÏ¢ÉÕ¤±¤é¤ì¤Æ¤¤¤ë¤«\r
37          * ¤É¤¦¤«È½Äꤷ¤Þ¤¹¡£\r
38          * \r
39          * @param hostName ¸¡ºº¤¹¤ë¥Û¥¹¥È̾\r
40          * @param dns ¸¡ºº¤Ë»ÈÍѤ¹¤ë DNS ¥µ¡¼¥Ð\r
41          * @return MX ¥ì¥³¡¼¥É¤¬Â¸ºß¤¹¤ì¤Ð true\r
42          */\r
43         public static boolean hasMXRecord(String hostName, String dns) {\r
44                 String name = "dns://" + dns + "/" + hostName;\r
45                 try {\r
46                         DirContext ictx = new InitialDirContext();\r
47                         Attributes attrs = ictx.getAttributes(name, new String[] { "MX" });\r
48                         NamingEnumeration namingEnumeration = attrs.getAll();\r
49                         if (namingEnumeration.hasMore()) {\r
50                                 return true;\r
51                         }\r
52                 } catch (NamingException e) {\r
53                         return false;\r
54                 }\r
55 \r
56                 return false;\r
57         }\r
58 \r
59         /**\r
60          * »ØÄꤵ¤ì¤¿ host ¤Ë MX ¥ì¥³¡¼¥É¤¬´ØÏ¢ÉÕ¤±¤é¤ì¤Æ¤¤¤ë¤«¤É¤¦¤«È½Äꤷ¤Þ¤¹¡£\r
61          * ¸¡ºº¤Ë»ÈÍѤ¹¤ë DNS ¥µ¡¼¥Ð¤Ï localhost ¤Ç¤¹¡£\r
62          * \r
63          * @param hostName ¸¡ºº¤¹¤ë¥Û¥¹¥È̾\r
64          * @return MX ¥ì¥³¡¼¥É¤¬Â¸ºß¤¹¤ì¤Ð true\r
65          */\r
66         public static boolean hasMXRecord(String hostName) {\r
67                 return hasMXRecord(hostName, "localhost");\r
68         }\r
69 \r
70         /**\r
71          * ¼¡¤Î¥³¥Þ¥ó¥É¤Ç¡¢¥Û¥¹¥È̾¤Ë MX ¥ì¥³¡¼¥É¤¬Â¸ºß¤¹¤ë¤«¤É¤¦¤«Ä´¤Ù¤Þ¤¹¡£\r
72          * <p>\r
73          * <code>java com.ozacc.mail.util.MXChecker [¥Û¥¹¥È̾] [»ÈÍѤ¹¤ëDNS¥µ¡¼¥Ð]</code>\r
74          * \r
75          */\r
76         public static void main(String[] args) {\r
77                 boolean result = false;\r
78                 if (args.length == 1) {\r
79                         result = hasMXRecord(args[0]);\r
80                 } else {\r
81                         result = hasMXRecord(args[0], args[1]);\r
82                 }\r
83 \r
84                 if (result)\r
85                         System.out.println(args[0] + " ¤Î MX ¥ì¥³¡¼¥É¤Ï¸ºß¤·¤Þ¤¹¡£");\r
86                 else\r
87                         System.out.println(args[0] + " ¤Î MX ¥ì¥³¡¼¥É¤ÏÅÐÏ¿¤µ¤ì¤Æ¤¤¤Þ¤»¤ó¡£");\r
88         }\r
89 }