验证邮件地址是否存在(包含了更新和添加两种情况)

html代码:

...

邮箱地址:(如xxx@163.com)

<input id="txtUEmailAddress" runat="server" type="text" οnblur="changetext(this)" />


            <table id="EMailDetail" name="EMailDetail" cellpadding="0" cellspacing="0">
                <tr>
                    <td class="style2" align="right">
                        接收服务器地址:
                    </td>
                    <td>
                        <div style="height: 17px; text-align: center; vertical-align: bottom; width: 372px;">
                            <div style="float: left;">
                                <asp:TextBox ID="txtPop3Address" runat="server" Width="148px"></asp:TextBox></div>
                            <div style="float: left; margin-left: 10px; width: 65px; text-align: left;">
                                <asp:CheckBox ID="chkPopSsl" runat="server" Text="SSL加密" /></div>
                            <div style="float: Right; margin-top: 3px; width: 135px;">
                                (如:imap.xxx.com:993)</div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td class="style2" align="right">
                        接收服务器协议:
                    </td>
                    <td>
                        <asp:DropDownList ID="ddlPopProtocol" runat="server">
                            <asp:ListItem Value="1">IMap</asp:ListItem>
                            <asp:ListItem Value="2">Pop</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td class="style2" align="right">
                        发送服务器地址:
                    </td>
                    <td>
                        <div style="float: left;">
                            <asp:TextBox ID="txtSmtpAddress" runat="server" Width="148px"></asp:TextBox></div>
                        <div style="float: left; width: 65px; margin-left: 10px; text-align: left;">
                            <asp:CheckBox ID="chkSmtp" runat="server" Text="SSL加密" /></div>
                    </td>
                </tr>
                <tr>
                    <td class="style2" align="right">
                        发送服务器协议:
                    </td>
                    <td>
                        <asp:DropDownList ID="ddlSmtpProtocol" runat="server">
                            <asp:ListItem Value="1">SMTP</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
            </table>

...

javascript代码:

                                <script type="text/javascript">

获取请求中德querystring参数

        function QueryString(fieldName) {
            var urlString = document.location.search;
            if (urlString != null) {
                var typeQu = fieldName + "=";
                var urlEnd = urlString.indexOf(typeQu);
                if (urlEnd != -1) {
                    var paramsUrl = urlString.substring(urlEnd + typeQu.length);
                    var isEnd = paramsUrl.indexOf('&');
                    if (isEnd != -1) {
                        return paramsUrl.substring(0, isEnd);
                    }
                    else {
                        return paramsUrl;
                    }
                }
                else {
                    return null;
                }
            }
            else {
                return null;
            }
        }

//界面初始化

$(document).ready(function () {
            $("#EMailDetail").hide();
            changetext($("#txtUEmailAddress")[0]);
        });


                                    function changetext(obj) {
                                        var actiontype = QueryString("type");
                                        var entUserID = QueryString("entUserID");
                                        if (obj.value == "") {
                                            $("#lblEntUserMsg").val("邮件地址不能为空!");
                                            $("#lblEntUserMsg").focus();
                                            return;
                                        }
                                        $.get("mailExist.ashx?strEMailAddr=" + obj.value + "&type=" + actiontype + "&entUserID=" + entUserID, function (data) {
                                            var args = data.toString().split("|");
                                            var Exist = args[0].split("=")[1];
                                            var Show = args[1].split("=")[1];
                                            if (Show == "true") {
                                                $("#EMailDetail").show();
                                            }
                                            else {
                                                $("#EMailDetail").hide();
                                                $("#txtPop3Address").val("");
                                                $("#txtSmtpAddress").val("");
                                            }
                                            if (Exist == "true") {
                                                $("#lblEntUserMsg").text("邮件地址已经存在!");
                                            }
                                            else {
                                                $("#lblEntUserMsg").text("");
                                            }
                                        });
                                    }
                                    
                                </script>

mailExist.ashx文件:

//主要函数


public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            StringBuilder strresult = new StringBuilder();
            string strEMailAddr = context.Request.QueryString["strEMailAddr"].ToString();
            string strType = context.Request.QueryString["type"].ToString();
            string strEntUserID = context.Request.QueryString["entUserID"].ToString();
            UserEmailBLL uebll = new UserEmailBLL();
            if (strType == "modify")
            {
                //修改
                int iEntUserID = Convert.ToInt32(strEntUserID);
                int ret = uebll.GetNumberExceptSelf(iEntUserID, strEMailAddr);
                if (ret != 0)
                {
                    strresult.Append("Exist=true");
                }
                else
                {
                    strresult.Append("Exist=false");
                }
            }
            else
            {
                //添加
                UserEmailInfo info = uebll.GetInfoByEmail(strEMailAddr);
                if (info != null)
                {
                    strresult.Append("Exist=true");
                }
                else
                {
                    strresult.Append("Exist=false");
                }
            }

            //是否是默认的邮件配置,如果是自定义界面不用展开,否则展开供用户输入
            MailDoMainConfigBLL bll = new MailDoMainConfigBLL();

            int idx = strEMailAddr.IndexOf('@');
            if (idx > 0)
            {
                string strDomain = strEMailAddr.Substring(idx + 1);
                MailDoMainConfigInfo configinfo = bll.GetByDomain(strDomain);

                if (configinfo == null)
                {
                    strresult.Append("|Show=true");
                }
                else
                {
                    strresult.Append("|Show=false");
                }
            } context.Response.Write(strresult.ToString());
        }

以上仅作参考,缺少环境无法运行
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#验证Email是否真正存在 在以往的编程中,比如编写用户的资料时,有时需要确认用户输入的Email是否真实有效,以前我们最多只能做到验证Email是否包含了某些特殊的字符,比如"@",".",".com"等,做到的只是判断了Email的合法性,证明用户填写的Email格式是正确的,但是这个Email是否真正的存在于网络中,则没有办法。  首先需要大家了解一下SMTP协议。 1.SMTP是工作在两种情况下:一是电子邮件从客户机传输到服务器;二是从某一个服务器传输到另一个   服务器 2.SMTP是个请求/响应协议,命令和响应都是基于ASCII文本,并以CR和LF符结束。响应包括一个表示返    回状态的三位数字代码 3.SMTP在TCP协议25号端口监听连接请求 4.连接和发送过程 SMTP协议说复杂也不复杂(明明带有“简单”这个词嘛),说简单如果你懂得Sock。不过现在只是我们利用的就是第一条中说的,从客户机传输到服务器,当我们向一台服务器发送邮件时,邮件服务器会首先验证邮件发送地址是否真的存在于本服务器上。 操作的步骤如下: 连接服务器的25端口(如果没有邮件服务,连了也是白连) 发送helo问候 发送mail from命令,如果返回250表示正确可以,连接本服务器,否则则表示服务器需要发送人验证。 发送rcpt to命令,如果返回250表示则Email存在 发送quit命令,退出连接

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值