我如何...从txt或html文件中收集电子邮件地址。 [英] How do I...collect email address from txt or html file.

查看:94
本文介绍了我如何...从txt或html文件中收集电子邮件地址。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我解决这个问题吗?这是从txt或html文件收集电子邮件地址的代码。但有一些方法我无法理解。我将它从vb转换为c#。



Can you help me about this issue? This a code for collect email address from txt or html file. but there are some method I can't understand. I convert it from vb to c#.

protected void btnParse_Click(object sender, EventArgs e)
    {
        object app;
        object doc;
        string docFileName;
        string docPath;
        string contents;
        
        
        //Cursor.Current = Cursors.WaitCursor

        txtResults.Text = "";
        lblMsg.Text="";

    

        //validate file name

        docFileName = Server.MapPath(fuFile.FileName);
        if(docFileName.Length==0)
        {
            lblMsg.Text="Please enter a file name";
        }
        

        //if no path use APP_BASE
        docPath = Server.MapPath(fuFile.FileName) ;//Path.GetFullPath(docFileName);
        if(docPath.Length == 0)
        {
            //docFileName = Application.StartupPath & "\" & docFileName;
        }
        

        //extract contents of file
        contents = "";
        
        if(Path.GetExtension(docFileName).ToLower()==".txt")
        {
            StreamReader fs;

            fs=new StreamReader(docFileName);
            try
            {
                contents=fs.ReadToEnd();
            }
            catch
            {
                lblMsg.Text="Unable to read from text input file";
                contents="";
            }                
        }
        else
        {
            try
            {                
                app =  Activator.CreateInstance(Type.GetTypeFromProgID("Word.Application"));
            }
            catch
            {
                lblMsg.Text="Unable to start Word";
            }
            try
            {
                doc = Server.MapPath(fuFile.FileName);//OpenFile(docFileName);
            }
            catch
            {
                lblMsg.Text="Unable to load document in Word";
            }

            //contents = fuFile.PostedFile.InputStream.Read().ToString();//doc.Content.Text;
        }
        //search for email addresses
        string emails;
        string email;
        StringBuilder results=new StringBuilder();
        
        emails = ExtractEmailAddressesFromString(contents);
        foreach(char email1 in emails)
        {
            results.Append(email1 + Environment.NewLine);            
        }
        //display results
        lblMsg.Text=String.Format("{0} match(es) found.", emails.Length);
        txtResults.Text = results.ToString();           
       
            
    }

    private string ExtractEmailAddressesFromString(string source)
    {
        MatchCollection mc;
        int i;
     	

        //expression garnered from www.regexlib.com - thanks guys!
        mc = Regex.Matches(source, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
        string results;
        results=(mc.Count - 1).ToString();

        for (i = 0; i <= results.Length - 1; i++)
        {
            results[i]=mc[i].Value;
        }
        return results;
            
    }

推荐答案

这篇关于我如何...从txt或html文件中收集电子邮件地址。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆