当前位置: 首页 > news >正文

b2c购物网站系统软文写作的三个要素

b2c购物网站系统,软文写作的三个要素,wordpress 学校 政府,嘉兴专业的嘉兴专业网站建设项目Java验证邮箱是否真实存在有效要检测邮箱是否真实存在,必须了解两方面知识:1. MX记录,winodws的nslookup命令。 查看学习2. SMTP协议,如何通过telnet发送邮件。查看学习 有个网站可以校验,http://verify-email.org/&a…
Java验证邮箱是否真实存在有效
要检测邮箱是否真实存在,必须了解两方面知识:
1. MX记录,winodws的nslookup命令。 查看学习

2. SMTP协议,如何通过telnet发送邮件。查看学习

有个网站可以校验,http://verify-email.org/, 不过一小时只允许验证10次。

代码如下(补充了一些注释):

import java.io.IOException;import org.apache.commons.net.smtp.SMTPClient;
import org.apache.commons.net.smtp.SMTPReply;
import org.apache.log4j.Logger;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.Type;/*** * 校验邮箱:1、格式是否正确 2、是否真实有效的邮箱地址* 步骤:* 1、从dns缓存服务器上查询邮箱域名对应的SMTP服务器地址* 2、尝试建立Socket连接* 3、尝试发送一条消息给SMTP服务器* 4、设置邮件发送者* 5、设置邮件接收者* 6、检查响应码是否为250(为250则说明这个邮箱地址是真实有效的)* @author Michael Ran**/
public class CheckEmailValidityUtil {private static final Logger logger = Logger.getLogger(CheckEmailValidityUtil.class);/*** @param email 待校验的邮箱地址* @return*/public static boolean isEmailValid(String email) {if (!email.matches("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+")) {logger.error("邮箱("+email+")校验未通过,格式不对!");return false;}String host = "";String hostName = email.split("@")[1];//Record: A generic DNS resource record. The specific record types //extend this class. A record contains a name, type, class, ttl, and rdata.Record[] result = null;SMTPClient client = new SMTPClient();try {// 查找DNS缓存服务器上为MX类型的缓存域名信息Lookup lookup = new Lookup(hostName, Type.MX);lookup.run();if (lookup.getResult() != Lookup.SUCCESSFUL) {//查找失败logger.error("邮箱("+email+")校验未通过,未找到对应的MX记录!");return false;} else {//查找成功result = lookup.getAnswers();}//尝试和SMTP邮箱服务器建立Socket连接for (int i = 0; i < result.length; i++) {host = result[i].getAdditionalName().toString();logger.info("SMTPClient try connect to host:"+host);//此connect()方法来自SMTPClient的父类:org.apache.commons.net.SocketClient//继承关系结构:org.apache.commons.net.smtp.SMTPClient-->org.apache.commons.net.smtp.SMTP-->org.apache.commons.net.SocketClient//Opens a Socket connected to a remote host at the current default port and //originating from the current host at a system assigned port. Before returning,//_connectAction_() is called to perform connection initialization actions. //尝试Socket连接到SMTP服务器client.connect(host);//Determine if a reply code is a positive completion response(查看响应码是否正常). //All codes beginning with a 2 are positive completion responses(所有以2开头的响应码都是正常的响应). //The SMTP server will send a positive completion response on the final successful completion of a command. if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) {//断开socket连接client.disconnect();continue;} else {logger.info("找到MX记录:"+hostName);logger.info("建立链接成功:"+hostName);break;}}logger.info("SMTPClient ReplyString:"+client.getReplyString());String emailSuffix="163.com";String emailPrefix="ranguisheng";String fromEmail = emailPrefix+"@"+emailSuffix; //Login to the SMTP server by sending the HELO command with the given hostname as an argument. //Before performing any mail commands, you must first login. //尝试和SMTP服务器建立连接,发送一条消息给SMTP服务器client.login(emailPrefix);logger.info("SMTPClient login:"+emailPrefix+"...");logger.info("SMTPClient ReplyString:"+client.getReplyString());//Set the sender of a message using the SMTP MAIL command, //specifying a reverse relay path. //The sender must be set first before any recipients may be specified, //otherwise the mail server will reject your commands. //设置发送者,在设置接受者之前必须要先设置发送者client.setSender(fromEmail);logger.info("设置发送者 :"+fromEmail);logger.info("SMTPClient ReplyString:"+client.getReplyString());//Add a recipient for a message using the SMTP RCPT command, //specifying a forward relay path. The sender must be set first before any recipients may be specified, //otherwise the mail server will reject your commands. //设置接收者,在设置接受者必须先设置发送者,否则SMTP服务器会拒绝你的命令client.addRecipient(email);logger.info("设置接收者:"+email);logger.info("SMTPClient ReplyString:"+client.getReplyString());logger.info("SMTPClient ReplyCode:"+client.getReplyCode()+"(250表示正常)");if (250 == client.getReplyCode()) {return true;}} catch (Exception e) {e.printStackTrace();} finally {try {client.disconnect();} catch (IOException e) {}}return false;}public static void main(String[] args) {System.out.println(isEmailValid("903109360@qq.com"));}
}

执行结果:

MX record about qq.com exists.
Connection succeeded to mx3.qq.com.
220 newmx21.qq.com MX QQ Mail Server
>HELO 163.com
=250 newmx21.qq.com
>MAIL FROM: <163.com@ranguisheng>
=250 Ok
>RCPT TO: <903109360@qq.com>
=250 Ok

Outcome: true

如果将被验证的邮箱换为:903109360@qq.con,就会验证失败:

找不到MX记录
Outcome: false


值得注意的是犹豫校验的第一步是从DNS服务器查询MX记录 所以必须联网  否则校验会失效 因为找不到MX记录会导致真实的有效地址也校验为无效 这点要特别注意。


此代码需要两个jar包:

1、Apache Commons Net  

maven地址:http://mvnrepository.com/artifact/commons-net/commons-net/

2、dnsjava

maven地址:http://mvnrepository.com/artifact/dnsjava/dnsjava/


PS:目前还没发验证部分企业邮箱,后面想办法解决这个问题之后更新此文章。

相关资源下载>>>:

dnsjava下载

Apache-commons-net下载 


参考文档:

apache-commons-net API

dnsjava API


http://www.wooajung.com/news/34992.html

相关文章:

  • 长葛做网站娱乐热搜榜今日排名
  • 网站建设营销型新闻营销
  • 寻求南宁网站建设人员威海seo优化公司
  • css网站开发站长统计app软件下载官网安卓
  • 哈尔滨专业优化网站个人南通百度网站快速优化
  • 福建泉州网站建设公司互联网营销师怎么考
  • 网站logo衔接西安高端网站建设公司
  • 网站建设公司推广方案百度推广多少钱一天
  • 后台网站如何建设南宁seo推广服务
  • 郑州专业做淘宝直播网站济南竞价托管
  • 东莞建网站的公司app开发公司
  • 门户网站制作seo网站技术培训
  • 免费网站模版下载本地推广平台
  • 重庆专业网站推广时间交换链接的作用
  • 百度搜索站长平台搜索引擎营销的特征
  • 网站建设推广保举火13星百度引流平台
  • 中国建设银行网站打不开网站交换链接的常见形式
  • 新乐市建设银行网站千锋教育课程
  • 专业做网站团队网站页面优化方法
  • 特产网站开发的好处中国企业网
  • 淄博专业网站建设公司店铺推广渠道有哪些
  • wordpress 更新缓存seo公司是什么意思
  • 做快照网站和推广 哪个效果好广州企业网站建设
  • 淘宝店铺转让平台哪个靠谱舟山百度seo
  • 网站 图片防盗链怎么申请网站空间
  • seo是什么意思啊电商优化生育政策
  • 网站空间大小怎么查看怎么联系百度客服
  • 做banner拉伸网站会糊免费刷网站百度关键词
  • 中国石化工程建设公司网站百度云搜索引擎入口官方
  • 网站跨省备案seochan是什么意思