mysql判断邮件地址是不是合法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql判断邮件地址是不是合法相关的知识,希望对你有一定的参考价值。
参考技术A import java.io.*;import java.net.*;
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
public class SMTPMXLookup
private static int hear( BufferedReader in ) throws IOException
String line = null;
int res = 0;
while ( (line = in.readLine()) != null )
String pfx = line.substring( 0, 3 );
try
res = Integer.parseInt( pfx );
catch (Exception ex)
res = -1;
if ( line.charAt( 3 ) != '-' ) break;
return res;
private static void say( BufferedWriter wr, String text )
throws IOException
wr.write( text + "\r\n" );
wr.flush();
return;
private static ArrayList getMX( String hostName )
throws NamingException
// Perform a DNS lookup for MX records in the domain
Hashtable env = new Hashtable();
env.put("java.naming.factory.initial",
"com.sun.jndi.dns.DnsContextFactory");
DirContext ictx = new InitialDirContext( env );
Attributes attrs = ictx.getAttributes
( hostName, new String[] "MX" );
Attribute attr = attrs.get( "MX" );
// if we don't have an MX record, try the machine itself
if (( attr == null ) || ( attr.size() == 0 ))
attrs = ictx.getAttributes( hostName, new String[] "A" );
attr = attrs.get( "A" );
if( attr == null )
throw new NamingException
( "No match for name '" + hostName + "'" );
// Huzzah! we have machines to try. Return them as an array list
// NOTE: We SHOULD take the preference into account to be absolutely
// correct. This is left as an exercise for anyone who cares.
ArrayList res = new ArrayList();
NamingEnumeration en = attr.getAll();
while ( en.hasMore() )
String mailhost;
String x = (String) en.next();
String f[] = x.split( " " );
// THE fix *************
if (f.length == 1)
mailhost = f[0];
else if ( f[1].endsWith( "." ) )
mailhost = f[1].substring( 0, (f[1].length() - 1));
else
mailhost = f[1];
// THE fix *************
res.add( mailhost );
return res;
public static boolean isAddressValid( String address )
// Find the separator for the domain name
int pos = address.indexOf( '@' );
// If the address does not contain an '@', it's not valid
if ( pos == -1 ) return false;
// Isolate the domain/machine name and get a list of mail exchangers
String domain = address.substring( ++pos );
ArrayList mxList = null;
try
mxList = getMX( domain );
catch (NamingException ex)
return false;
// Just because we can send mail to the domain, doesn't mean that the
// address is valid, but if we can't, it's a sure sign that it isn't
if ( mxList.size() == 0 ) return false;
// Now, do the SMTP validation, try each mail exchanger until we get
// a positive acceptance. It *MAY* be possible for one MX to allow
// a message [store and forwarder for example] and another [like
// the actual mail server] to reject it. This is why we REALLY ought
// to take the preference into account.
for ( int mx = 0 ; mx < mxList.size() ; mx++ )
boolean valid = false;
try
int res;
//
Socket skt = new Socket( (String) mxList.get( mx ), 25 );
BufferedReader rdr = new BufferedReader
( new InputStreamReader( skt.getInputStream() ) );
BufferedWriter wtr = new BufferedWriter
( new OutputStreamWriter( skt.getOutputStream() ) );
res = hear( rdr );
if ( res != 220 ) throw new Exception( "Invalid header" );
say( wtr, "EHLO rgagnon.com" );
res = hear( rdr );
if ( res != 250 ) throw new Exception( "Not ESMTP" );
// validate the sender address
say( wtr, "MAIL FROM: <tim@orbaker.com>" );
res = hear( rdr );
if ( res != 250 ) throw new Exception( "Sender rejected" );
say( wtr, "RCPT TO: <" + address + ">" );
res = hear( rdr );
// be polite
say( wtr, "RSET" ); hear( rdr );
say( wtr, "QUIT" ); hear( rdr );
if ( res != 250 )
throw new Exception( "Address is not valid!" );
valid = true;
rdr.close();
wtr.close();
skt.close();
catch (Exception ex)
// Do nothing but try next host
ex.printStackTrace();
finally
if ( valid ) return true;
return false;
public static void main( String args[] )
String testData[] =
"real@rgagnon.com",
"you@acquisto.net",
"fail.me@nowhere.spam", // Invalid domain name
"arkham@bigmeanogre.net", // Invalid address
"nosuchaddress@yahoo.com" // Failure of this method
;
for ( int ctr = 0 ; ctr < testData.length ; ctr++ )
System.out.println( testData[ ctr ] + " is valid? " +
isAddressValid( testData[ ctr ] ) );
return;
算法判断IP地址是不是合法的,包含IPv4和IPv6
1. 概述
类似题目:
LeetCode468: https://leetcode.com/problems/validate-ip-address/
LeetCode93: https://leetcode.com/problems/restore-ip-addresses/
编写一个函数来验证输入的每个字符串是否是有效的IPv4或IPv6地址。
- 如果是有效的 IPv4 地址,返回 “IPv4”;
- 如果是有效的 IPv6 地址,发挥 “IPv6”;
- 如果不是上述类型的IP地址,返回 “Neither”。
IPv4地址由十进制数和点来表示,每个地址包含4个十进制数,其范围为 0 ~ 255,用 (“.”) 分割,比如:
172.16.254.1
同时,IPv4地址内的数不会以0开头。比如,地址 172.16.254.1
是不合法的。
IPv6地址由8组16进制的数字来表示,每组表示16比特。这些数字通过 (“:”) 分割,比如:2001:0db8:85a3:0000:0000:8a2e:0370:7334
是一个有效的地址。而且,我们可以加入一些以0开头的数字,字母可以使用大写,也可以是小写。所以,2001:0db8:85a3:0000:0000:8A2E:0370:7334
也是一个有效的 IPv6 地址(即,忽略0开头,忽略大小写)。
然而,我们不能因为某个组的值为0,而使用一个空的组,以至于出现(::)的情况。比如,2001:0db8:85a3::8A2E:0370:7334
是无效的 IPv6 地址。
同时,在IPv6地址中,多余的0也是不被允许的。比如,02001:0db8:85a3:0000:0000:8a2e:0370:7334
是无效的。
例子
输入:IP = “172.16.254.1”
输出:“IPv4”
解释:有效的 IPv4 地址,返回 “IPv4”
2. 代码及测试
Java
方法1
class Solution
public String validIPAddress(String queryIP)
if (queryIP == null || queryIP.length() < 1) return "Neither";
int n = queryIP.length();
int index = 0;
while (index < n && queryIP.charAt(index) != '.' && queryIP.charAt(index) != ':') index++;
if (index == n) return "Neither";
int cnt = 0;
if (queryIP.charAt(index) == '.')
// check IPv4
index = 0;
int left = 0;
while (index < n)
while (index < n && queryIP.charAt(index) != '.') index++;
String str = queryIP.substring(left, index);
if (cnt > 4 || str == null || str.length() < 1 || str.length() > 3 || (str.length() > 1 && str.charAt(0) == '0')) return "Neither";
for (int i = 0; i < str.length(); i++)
if (str.charAt(i) < '0' || str.charAt(i) > '9') return "Neither";
int val = Integer.parseInt(str);
if (val < 0 || val > 255) return "Neither";
cnt++;
left = index + 1;
index++;
if (cnt == 4 && index == n + 1) return "IPv4";
else
// check IPv6
index = 0;
int left = 0;
while (index < n)
while (index < n && queryIP.charAt(index) != ':') index++;
String str = queryIP.substring(left, index);
if (cnt > 8 || str == null || str.length() < 1 || str.length() > 4) return "Neither";
for (int i = 0; i < str.length(); i++)
if (!(str.charAt(i) >= '0' && str.charAt(i) <= '9') && !(str.charAt(i) >= 'a' && str.charAt(i) <= 'f') && !(str.charAt(i) >= 'A' && str.charAt(i) <= 'F')) return "Neither";
cnt++;
left = index + 1;
index++;
if (cnt == 8 && index == n + 1) return "IPv6";
return "Neither";
方法2
class Solution
public String validIPAddress(String queryIP)
if (queryIP == null || queryIP.length() < 1)
return "Neither";
queryIP = queryIP.toLowerCase();
int n = queryIP.length();
if (queryIP.charAt(n - 1) == '.' || queryIP.charAt(n - 1) == ':')
return "Neither";
int index = 0;
while (index < n && queryIP.charAt(index) != '.' && queryIP.charAt(index) != ':') index++;
if (index == n) return "Neither";
if (queryIP.charAt(index) == '.')
// check IPv4
String[] strs = queryIP.split("\\\\.");
if (strs.length != 4) return "Neither";
for (int i = 0; i < 4; i++)
if (strs[i].length() < 1 || strs[i].length() > 3 || (strs[i].length() > 1 && strs[i].charAt(0) == '0')) return "Neither";
for (int j = 0; j < strs[i].length(); j++)
if (strs[i].charAt(j) < '0' || strs[i].charAt(j) > '9') return "Neither";
int val = Integer.parseInt(strs[i]);
if (val < 0 || val > 255) return "Neither";
return "IPv4";
else
// check IPv6
String[] strs = queryIP.split(":");
if (strs.length != 8) return "Neither";
for (int i = 0; i < 8; i++)
if (strs[i].length() < 1 || strs[i].length() > 4) return "Neither";
for (int j = 0; j < strs[i].length(); j++)
if (!(strs[i].charAt(j) >= '0' && strs[i].charAt(j) <= '9') && !(strs[i].charAt(j) >= 'a' && strs[i].charAt(j) <= 'f'))
return "Neither";
return "IPv6";
C++
方法1
class Solution
public:
string validIPAddress(string queryIP)
int n = queryIP.size();
if(n < 7) return "Neither";
int index = 0;
while (index < n && queryIP[index] != '.' && queryIP[index] != ':') index++;
if (index == n) return "Neither";
int cnt = 0;
if (queryIP[index] == '.')
// check IPv4
index = 0;
int left = index;
while (index < n)
while (index < n && queryIP[index] != '.') index++;
string t = queryIP.substr(left, index - left);
if (cnt > 4 || t.empty() || (t.size() > 1 && t[0] == '0') || t.size() > 3) return "Neither";
for (char c: t)
if (c < '0' || c > '9') return "Neither";
int val = stoi(t);
if (val < 0 || val > 255) return "Neither";
cnt++;
left = index + 1;
index++;
if (cnt == 4 && index == n + 1) return "IPv4";
else
// check IPv6
index = 0;
int left = index;
while (index < n)
while (index < n && queryIP[index] != ':') index++;
string t = queryIP.substr(left, index - left);
if (cnt > 8 || t.empty() || t.size() > 4) return "Neither";
for (char c: t)
if (!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f') && !(c >= 'A' && c <= 'F')) return "Neither";
cnt++;
left = index + 1;
index++;
if (cnt == 8 && index == n + 1) return "IPv6";
return "Neither";
;
方法2
class Solution
public:
string validIPAddress(string IP)
istringstream is(IP);
string t = "";
int cnt = 0;
if (IP.find(':') == string::npos) // Check IPv4
while (getline(is, t, '.'))
++cnt;
if (cnt > 4 || t.empty() || (t.size() > 1 && t[0] == '0') || t.size() > 3) return "Neither";
for (char c: t)
if (c < '0' || c > '9') return "Neither";
int val = stoi(t);
if (val < 0 || val > 255) return "Neither";
return (cnt == 4 && IP.back() != '.') ? "IPv4" : "Neither";
else // Check IPv6
while (getline(is, t, ':'))
++cnt;
if (cnt > 8 || t.empty() || t.size() > 4) return "Neither";
for (char c: t)
if (!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f') && !(c >= 'A' && c <= 'F')) return "Neither";
return (cnt == 8 && IP.back() != ':') ? "IPv6" : "Neither";
;
以上是关于mysql判断邮件地址是不是合法的主要内容,如果未能解决你的问题,请参考以下文章