如何用java编写一个验证邮箱是否正确的程序

解决方案 »

  1.   

       public final static void isEMail(final ActionMessages errors,
                                        final String formName,
                                        final String propertyName,
                                        final String value) {
          RE regexpValid = null;
          RE regexpInvalid = null;      try {
             regexpValid = new RE("^\\S+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
             regexpInvalid = new RE("(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)");         if ((value == null) || !regexpValid.isMatch(value) ||
                     regexpInvalid.isMatch(value)) {
                addError(errors, formName, propertyName, NO_EMAIL);
             }
          } catch (REException exception) {
             exception.printStackTrace();
          }
       }这个是我做的项目中的mail邮箱地址是否合法的验证。希望对楼主有帮助
      

  2.   

    你是想验证邮箱格式的真确性,还是想验证邮箱的合法性和正确存在啊???我把2种的代码都贴出来,希望对你有帮助!验证格式的:
    public class RegisterCheck {    /**     * 验证输入的邮箱格式是否符合     * @param email     * @return 是否合法     */public static boolean emailFormat(String email)    {        boolean tag = true;        final String pattern1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";        final Pattern pattern = Pattern.compile(pattern1);        final Matcher mat = pattern.matcher(email);        if (!mat.find()) {            tag = false;        }        return tag;    }} 验证合法性和是否存在的:
    import java.io.*;import javax.mail.MessagingException;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeUtility;
    /**
    * Utility methods once in TextUtils that require the mail.jar library.
    *
    * @author $Author: hani $
    * @version $Revision: 1.1.1.1 $
    */
    public class MailUtils {
        //~ Methods ////////////////////////////////////////////////////////////////    /**
        * Decode binary data from String using base64.
        *
        * @see #encodeBytes(byte[])
        */
        public final static byte[] decodeBytes(String str) throws IOException {
            try {
                ByteArrayInputStream encodedStringStream = new ByteArrayInputStream(str.getBytes());
                InputStream decoder = MimeUtility.decode(encodedStringStream, "base64");
                ByteArrayOutputStream decodedByteStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[256];            while (true) {
                    int read = decoder.read(buffer);                if (read == -1) {
                        break;
                    }                decodedByteStream.write(buffer, 0, read);
                }            decodedByteStream.flush();            return decodedByteStream.toByteArray();
            } catch (MessagingException me) {
                throw new IOException("Cannot decode data.");
            }
        }    /**
         * Encode binary data into String using base64.
         *
         * @see #decodeBytes(java.lang.String)
         */
        public final static String encodeBytes(byte[] data) throws IOException {
            try {
                ByteArrayOutputStream encodedByteStream = new ByteArrayOutputStream();
                OutputStream encoder = MimeUtility.encode(encodedByteStream, "base64");
                encoder.write(data);
                encoder.flush();            return new String(encodedByteStream.toByteArray());
            } catch (MessagingException me) {
                throw new IOException("Cannot encode data.");
            }
        }    /**
         * Verify that the given string is a valid email address.
        * "Validity" in this context only means that the address conforms
        * to the correct syntax (not if the address actually exists).
         *
         * @param email The email address to verify.
        * @return a boolean indicating whether the email address is correctly formatted.
         */
        public final static boolean verifyEmail(String email) {
            if (email == null) {
                return false;
            }        if (email.indexOf('@') < 1) {
                return false;
            }        try {
                new InternetAddress(email);            return true;
            } catch (AddressException e) {
                return false;
            }
        }
    }
      

  3.   

    谁知道哪里有java语言程序设计  基础篇  第五版的练习答案啊~谢谢!