四联光电智能照明论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1930|回复: 0
打印 上一主题 下一主题

验证邮箱是否真实存在

[复制链接]
  • TA的每日心情
    开心
    2018-7-4 09:08
  • 97

    主题

    392

    帖子

    6095

    积分

    论坛元老

    Rank: 8Rank: 8

    积分
    6095
    跳转到指定楼层
    楼主
    发表于 2016-10-31 15:55:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Text;
    7. using System.Windows.Forms;
    8. using System.Net.Sockets;
    9. using System.Text.RegularExpressions;
    10. using System.IO;
    11. using System.Diagnostics;

    12. namespace WindowsFormsApplication2
    13. {
    14.     public partial class Form1 : Form
    15.     {
    16.         public Form1()
    17.         {
    18.             InitializeComponent();
    19.         }

    20.         TcpClient tcpc;
    21.         NetworkStream s;
    22.         string strDomain;
    23.         byte[] bb;
    24.         int len;
    25.         string read;
    26.         string stringTosend;
    27.         byte[] arrayToSend;
    28.         bool flag;
    29.         int count = 0;




    30.         public string getMailServer(string strEmail, bool IsCheck)
    31.         {
    32.             strDomain = strEmail.Split('@')[1];
    33.             if (IsCheck == true)
    34.                 this.textBoxShow.Text += "分离出邮箱域名:" + strDomain + "\r\n";
    35.             ProcessStartInfo info = new ProcessStartInfo();   //指定启动进程时使用的一组值。
    36.             info.UseShellExecute = false;
    37.             info.RedirectStandardInput = true;
    38.             info.RedirectStandardOutput = true;
    39.             info.FileName = "nslookup";
    40.             info.CreateNoWindow = true;
    41.             info.Arguments = "-type=mx " + strDomain;
    42.             Process ns = Process.Start(info);        //提供对本地和远程进程的访问并使您能够启动和停止本地系统进程。
    43.             StreamReader sout = ns.StandardOutput;

    44.             Regex reg = new Regex(@"mail exchanger = (?<mailServer>[^\s]+)");
    45.             string strResponse = "";
    46.             while ((strResponse = sout.ReadLine()) != null)
    47.             {

    48.                 Match amatch = reg.Match(strResponse);   // Match  表示单个正则表达式匹配的结果。

    49.                 if (reg.Match(strResponse).Success)
    50.                 {
    51.                     return amatch.Groups["mailServer"].Value;   //获取由正则表达式匹配的组的集合

    52.                 }
    53.             }
    54.             return null;
    55.         }

    56.         private void Connect(string mailServer)
    57.         {
    58.             try
    59.             {
    60.                 tcpc.Connect(mailServer, 25);
    61.                 s = tcpc.GetStream();
    62.                 len = s.Read(bb, 0, bb.Length);
    63.                 read = Encoding.UTF8.GetString(bb);
    64.                 if (read.StartsWith("220") == true)
    65.                     this.textBoxShow.Text += "连接服务器成功!" + "\r\n";
    66.             }
    67.             catch (Exception e)
    68.             {
    69.                 MessageBox.Show(e.ToString());

    70.             }
    71.         }

    72.         private bool SendCommand(string command)
    73.         {
    74.             try
    75.             {

    76.                 arrayToSend = Encoding.UTF8.GetBytes(command.ToCharArray());
    77.                 s.Write(arrayToSend, 0, arrayToSend.Length);
    78.                 len = s.Read(bb, 0, bb.Length);
    79.                 read = Encoding.UTF8.GetString(bb);
    80.                 this.textBoxShow.Text += "收到:" + read.Substring(0, len) + "\r\n";
    81.             }
    82.             catch (IOException e)
    83.             {
    84.                 MessageBox.Show(e.ToString());
    85.             }
    86.             if (read.StartsWith("250"))
    87.                 return true;
    88.             else
    89.                 return false;
    90.         }

    91.         public void checkEmail(string mailAddress)
    92.         {

    93.             Regex reg = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

    94.             if (!reg.IsMatch(mailAddress))
    95.             {
    96.                 this.textBoxShow.Text += "Email地址形式上就不对" + "\r\n";
    97.                 return;
    98.             }//Email地址形式上就不对


    99.             string mailServer = getMailServer(mailAddress, true);

    100.             if (mailServer == null)
    101.             {
    102.                 this.textBoxShow.Text += "邮件服务器不存在!" + "\r\n";
    103.                 return;
    104.                 //邮件服务器探测错误
    105.             }

    106.             this.textBoxShow.Text += "解析出域名" + strDomain + "的mx记录:" + mailServer + "\r\n";
    107.             tcpc = new TcpClient();      //为 TCP 网络服务提供客户端连接。
    108.             tcpc.NoDelay = true;
    109.             tcpc.ReceiveTimeout = 3000;
    110.             tcpc.SendTimeout = 3000;
    111.             bb = new byte[512];

    112.             try
    113.             {

    114.                 Connect(mailServer);//创建连接

    115.                 stringTosend = "helo " + mailServer + "\r\n"; ////写入HELO命令
    116.                 this.textBoxShow.Text += "发送:" + stringTosend;
    117.                 flag = SendCommand(stringTosend);
    118.                 if (flag == false)
    119.                 {
    120.                     //timer1.Enabled = false;
    121.                     return;
    122.                 }



    123.                 stringTosend = "mail from:<" + mailAddress + ">" + "\r\n"; ////写入Mail From命令
    124.                 this.textBoxShow.Text += "发送:" + stringTosend;
    125.                 flag = SendCommand(stringTosend);
    126.                 if (flag == false)
    127.                 {
    128.                     return;
    129.                 }



    130.                 stringTosend = "rcpt to:<" + mailAddress + ">" + "\r\n";//写入RCPT命令,这是关键的一步,后面的参数便是查询的Email的地址
    131.                 this.textBoxShow.Text += "发送:" + stringTosend;
    132.                 flag = SendCommand(stringTosend);

    133.                 if (flag == true)
    134.                 {
    135.                     //邮箱存在
    136.                 }
    137.                 else
    138.                 {
    139.                     //邮箱不存在
    140.                 }






    141.             }
    142.             catch (Exception ee)
    143.             {
    144.                 MessageBox.Show(ee.ToString());   //发生错误或邮件服务器不可达

    145.             }
    146.         }

    147.         private void button1_Click(object sender, EventArgs e)
    148.         {

    149.             checkEmail("wangzh300@163.com");
    150.         }
    151.     }
    152. }
    复制代码

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|小黑屋|Silian Lighting+ ( 蜀ICP备14004521号-1 )

    GMT+8, 2024-5-5 06:22 , Processed in 1.062500 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表