2009-08-25 10 views

Odpowiedz

55

Nie przejmuj się własną weryfikacją. .NET 4.0 znacznie poprawił sprawdzanie poprawności za pośrednictwem klasy MailAddress. Po prostu użyj MailAddress address = new MailAddress(input), a jeśli to wyrzuci, to nie jest prawidłowe. Jeśli istnieje jakakolwiek interpretacja danych wejściowych jako specyfikacja adresu e-mail zgodna z RFC 2822, zostanie ona przeanalizowana jako taka. Wyrażenia powyższe, nawet artykuł MSDN pierwszy, są błędne, ponieważ nie uwzględniają nazwy wyświetlanej, cytowanej części lokalnej, wartości literowej domeny, poprawnej specyfikacji dot-atom dla części lokalnej, możliwości, że adres e-mail może być w nawiasach ostrych, wielokrotnych cytowanych wartościach znaków dla nazwy wyświetlanej, znaków zbiegów, znaków Unicode w wyświetlanej nazwie, komentarzach i maksymalnej poprawnej długości adresu e-mail. Spędziłem trzy tygodnie na ponownym pisaniu parsera adresów pocztowych w .NET 4.0 dla System.Net.Mail i uwierzcie mi, było to o wiele trudniejsze niż zwykłe wymyślanie niektórych wyrażeń regularnych, ponieważ jest wiele skrzynek na krawędzi. Klasa MailAddress w .NET 4.0 beta 2 będzie miała tę ulepszoną funkcjonalność.

Jeszcze jedno, jedyną rzeczą, którą można sprawdzić, jest format adresu e-mail. Nie można nigdy potwierdzić, że adres e-mail jest rzeczywiście ważny do odbierania wiadomości e-mail bez wysyłania wiadomości e-mail na ten adres i sprawdzania, czy serwer akceptuje je do dostarczenia. Jest to niemożliwe i podczas gdy istnieją polecenia SMTP, które możesz dać serwerowi pocztowemu, aby spróbować go zweryfikować, wiele razy te będą wyłączone lub zwrócą nieprawidłowe wyniki, ponieważ jest to popularny sposób, w jaki spamerzy mogą znaleźć adresy e-mail.

+1

Dziękuję za odpowiedź.Ciesz się słyszeć, że Microsoft zwrócił na to uwagę! Ale, aby wyjaśnić, mówisz, że najlepszą praktyką * jest * wychwycenie wyjątku, tak jak ja to zrobiłem w powyższej funkcji? –

+0

Tak, musisz złapać wyjątek FormatException, który zostanie zgłoszony, jeśli adres jest nieprawidłowy. Twój kod jest poprawny. –

+0

Ostatnia rzecz: System.Net.Mail zyskał wiele uwagi ze strony Microsoftu dla .NET 4.0. Możesz zobaczyć, co poprawiliśmy w moim poście na blogu NCL: http://blogs.msdn.com/ncl/archive/2009/08/06/what-s-new-in-system-net-mail .aspx –

1

Powinieneś użyć Regular Expressions, aby potwierdzić adresy e-mail.

+20

więc może masz 2 problemy :) – voyager

+3

Internecie i tej stronie są szok pełen * * błędnych prób rozwiązania tego z regex. O ile nie jesteś kreatorem regex ** i ** kreatorem e-maili, nawet o tym nie myśl. (Jestem, i zrobiłem to, i postanowiłem tego nie robić.) – tripleee

0

Możesz użyć Regex, aby to zrobić.

Napisano wiele artykułów na ten temat; to pojawiło się, gdy szukałem w Google wyrażenia "regex do sprawdzania poprawności adresu e-mail": Find or Validate an Email Address.

+0

Zgadzam się z podróżnikiem. Teraz mam dwa problemy. –

+1

Są one lepsze niż wyrażenie regularne w artykule MSDN, ale nadal kończą się niepowodzeniem w wielu przypadkach krawędzi. –

5

MSDN Artykuł: How to: Verify That Strings are in Valid E-Mail Format

Ten przykład wywołuje metodę Regex.IsMatch (String, String), aby sprawdzić, czy ciąg jest zgodny z wzorcu wyrażenia regularnego.

Function IsValidEmailFormat(ByVal s As String) As Boolean 
    Return Regex.IsMatch(s, "^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$") 
End Function 
+0

To wydaje się ostateczne. Ale nadal wolałbym znaleźć taką funkcję wbudowaną i squirreled gdzieś w System.Net.Mail. :( –

+10

Nie uwzględnia to wielu różnych przypadków brzegowych z adresami e-mail, z których najważniejszym jest cytowana część lokalna. Istnieje wiele innych niealfanumerycznych znaków, które są również prawidłowe, ponieważ to wyrażenie nie pozwala na Wiem, że jest to artykuł MSDN, ale jest źle. Ponownie napisałem parsowanie adresu dla .NET 4.0 w System.Net.Mail.MailAddress, aby być dużo bardziej odpornym i wziąć te rzeczy pod uwagę. Zobacz moją odpowiedź poniżej, aby uzyskać więcej szczegółów. –

+0

@JeffTucker To jest [wyrażenie regularne zgodne z RFC822] (http://ex-parrot.com/~pdw/Mail-RFC822-Address.html). – user17753

2

najpierw trzeba ograniczyć użytkownikowi wprowadzając niewłaściwych symboli, można to zrobić za pomocą zdarzenia tekstowe KeyPress

Private Sub txtemailid_KeyPress(ByVal sender As System.Object, 
           ByVal e As System.Windows.FormsKeyPressEventArgs) Handles txtemailid.KeyPress 

    Dim ac As String = "@" 
    If e.KeyChar <> ChrW(Keys.Back) Then 
     If Asc(e.KeyChar) < 97 Or Asc(e.KeyChar) > 122 Then 
      If Asc(e.KeyChar) <> 46 And Asc(e.KeyChar) <> 95 Then 
       If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then 
        If ac.IndexOf(e.KeyChar) = -1 Then 
         e.Handled = True 

        Else 

         If txtemailid.Text.Contains("@") And e.KeyChar = "@" Then 
          e.Handled = True 
         End If 

        End If 


       End If 
      End If 
     End If 

    End If 

End Sub 

powyższy kod tylko pozwoli użytkownikowi AZ wejściowego (małe), od 0 do 9 (cyfry), @ ,., _

i po użyciu stwierdzenia prawidłowości zdarzenie kontroli tekstowego do sprawdzania poprawności identyfikatora e-mail za pomocą wyrażenia regularnego

Private Sub txtemailid_Validating(ByVal sender As System.Object, 
            ByVal e As System.ComponentModel.CancelEventArgs) 
    Handles txtemailid.Validating 

    Dim pattern As String = "^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)[email protected][a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$" 


    Dim match As System.Text.RegularExpressions.Match = Regex.Match(txtemailid.Text.Trim(), pattern, RegexOptions.IgnoreCase) 
    If (match.Success) Then 
     MessageBox.Show("Success", "Checking") 
    Else 
     MessageBox.Show("Please enter a valid email id", "Checking") 
     txtemailid.Clear() 
    End If 
End Sub 
2
'----------------------------------------------------------------------- 

'Creater : Rachitha Madusanka 

'http://www.megazoon.com 

'[email protected] 

'[email protected] 

'Web Designer and Software Developer 

'@ http://www.zionx.net16.net 

'----------------------------------------------------------------------- 




Function ValidEmail(ByVal strCheck As String) As Boolean 
    Try 
     Dim bCK As Boolean 
     Dim strDomainType As String 


     Const sInvalidChars As String = "!#$%^&*()=+{}[]|\;:'/?>,< " 
     Dim i As Integer 

     'Check to see if there is a double quote 
     bCK = Not InStr(1, strCheck, Chr(34)) > 0 
     If Not bCK Then GoTo ExitFunction 

     'Check to see if there are consecutive dots 
     bCK = Not InStr(1, strCheck, "..") > 0 
     If Not bCK Then GoTo ExitFunction 

     ' Check for invalid characters. 
     If Len(strCheck) > Len(sInvalidChars) Then 
      For i = 1 To Len(sInvalidChars) 
       If InStr(strCheck, Mid(sInvalidChars, i, 1)) > 0 Then 
        bCK = False 
        GoTo ExitFunction 
       End If 
      Next 
     Else 
      For i = 1 To Len(strCheck) 
       If InStr(sInvalidChars, Mid(strCheck, i, 1)) > 0 Then 
        bCK = False 
        GoTo ExitFunction 
       End If 
      Next 
     End If 

     If InStr(1, strCheck, "@") > 1 Then 'Check for an @ symbol 
      bCK = Len(Left(strCheck, InStr(1, strCheck, "@") - 1)) > 0 
     Else 
      bCK = False 
     End If 
     If Not bCK Then GoTo ExitFunction 

     strCheck = Right(strCheck, Len(strCheck) - InStr(1, strCheck, "@")) 
     bCK = Not InStr(1, strCheck, "@") > 0 'Check to see if there are too many @'s 
     If Not bCK Then GoTo ExitFunction 

     strDomainType = Right(strCheck, Len(strCheck) - InStr(1, strCheck, ".")) 
     bCK = Len(strDomainType) > 0 And InStr(1, strCheck, ".") < Len(strCheck) 
     If Not bCK Then GoTo ExitFunction 

     strCheck = Left(strCheck, Len(strCheck) - Len(strDomainType) - 1) 
     Do Until InStr(1, strCheck, ".") <= 1 
      If Len(strCheck) >= InStr(1, strCheck, ".") Then 
       strCheck = Left(strCheck, Len(strCheck) - (InStr(1, strCheck, ".") - 1)) 
      Else 
       bCK = False 
       GoTo ExitFunction 
      End If 
     Loop 
     If strCheck = "." Or Len(strCheck) = 0 Then bCK = False 

ExitFunction: 
     ValidEmail = bCK 
    Catch ex As ArgumentException 
     Return False 
    End Try 
    Return ValidEmail 
End Function 
2
Public Function ValidateEmail(ByVal strCheck As String) As Boolean 
     Try 
      Dim vEmailAddress As New System.Net.Mail.MailAddress(strCheck) 
     Catch ex As Exception 
      Return False 
     End Try 
     Return True 
    End Function 
+2

Odpowiedź powinna zawierać wyjaśnienie, a nie fragment kodu, aby lepiej zrozumieć – user2720864

+0

Proszę dodać wyjaśnienie do swojej odpowiedzi: – DontVoteMeDown

0

ja testowałem zatwierdzone „odpowiedź” w tym przypadku i tak nie jest wydaje się przestrzegać specyfikacji tego, co faktycznie jest prawidłowym adresem e-mail. Po wielu bólach głowy znalazłem to wyrażenie, które ma o wiele lepszą pracę niż Microsoft.

"(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]" + 
")+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:" + 
"\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(" + 
"?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ " + 
"\t]))*""(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\0" + 
"31]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\" + 
"](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+" + 
"(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:" + 
"(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z" + 
"|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)" + 
"?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\" + 
"r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[" + 
" \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)" + 
"?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t]" + 
")*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[" + 
" \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*" + 
")(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]" + 
")+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)" + 
"*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+" + 
"|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r" + 
"\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:" + 
"\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t" + 
"]))*""(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031" + 
"]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](" + 
"?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?" + 
":(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?" + 
":\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)|(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?" + 
":(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?" + 
"[ \t]))*""(?:(?:\r\n)?[ \t])*)*:(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\"".\[\] " + 
"\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|" + 
"\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>" + 
"@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""" + 
"(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t]" + 
")*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\" + 
""".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?" + 
":[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[" + 
"\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\"".\[\] \000-" + 
"\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(" + 
"?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;" + 
":\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([" + 
"^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\""" + 
".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\" + 
"]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\" + 
"[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\" + 
"r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] " + 
"\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]" + 
"|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\"".\[\] \0" + 
"00-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\" + 
".|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@," + 
";:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?" + 
":[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*" + 
"(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\""." + 
"\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[" + 
"^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]" + 
"]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)(?:,\s*(" + 
"?:(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\" + 
""".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)(?:\.(?:(" + 
"?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[" + 
"\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t" + 
"])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t" + 
"])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?" + 
":\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|" + 
"\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:" + 
"[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\" + 
"]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)" + 
"?[ \t])*(?:@(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""" + 
"()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)" + 
"?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>" + 
"@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[" + 
" \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@," + 
";:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t]" + 
")*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\" + 
""".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?" + 
"(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\""." + 
"\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:" + 
"\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[" + 
"""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])" + 
"*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])" + 
"+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\" + 
".(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z" + 
"|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(" + 
"?:\r\n)?[ \t])*))*)?;\s*)" 

Mam już sformatowany jako ciąg vb za pomocą prostej aplikacji. Szkoda, że ​​przepełnienie stosu jest bardziej zainteresowane byciem "repozytorium kodującym" niż posiadanie kompletnej odpowiedzi na problem.

+5

Szkoda mi biednego programisty, który musi rozwiązać ten problem. –

-1
Public Shared Function ValidEmailAddress(ByVal emailAddress As String, ByRef errorMessage As String) As Boolean 
     If emailAddress.Length = 0 Then 
      errorMessage = "E-mail address is required." 
      Return False 
     End If 
     If emailAddress.IndexOf("@") > -1 Then 
      If (emailAddress.IndexOf(".", emailAddress.IndexOf("@")) > emailAddress.IndexOf("@")) AndAlso emailAddress.Split(".").Length > 0 AndAlso emailAddress.Split(".")(1) <> "" Then 
       errorMessage = "" 
       Return True 
      End If 
     End If 
     errorMessage = "E-mail address must be valid e-mail address format." 
     Return False 
    End Function 
+0

Jest wiele więcej możliwości sprawdzania poprawności poczty e-mail ... –

0

e-maile typu „adres @ localhost” i „[email protected]” są rzeczywiście ważne adresy i można przetestować te poprzez uruchomienie własnego serwera e-mail (zwykle odbywa się poprzez modyfikację pliku hosta, jak również). Dla kompletnego rozwiązania, jednakże:

''' <summary> 
''' METHODS FOR SENDING AND VALIDATING EMAIL 
''' </summary> 
''' <remarks></remarks> 
Public Class email 

    ''' <summary> 
    ''' check if email format is valid 
    ''' </summary> 
    ''' <param name="emailAddress">[required] Email address.</param> 
    ''' <param name="disallowLocalDomain">[optional] Allow headers like "@localhost"?</param> 
    ''' <param name="allowAlerts">[optional] Enable error messages?</param> 
    ''' <returns>Returns true if email is valid and false otherwise.</returns> 
    ''' <remarks></remarks> 
    Public Shared Function isValid(ByVal emailAddress As String, 
            Optional ByVal disallowLocalDomain As Boolean = True, 
            Optional ByVal allowAlerts As Boolean = True 
            ) As Boolean 
     Try 
      Dim mailParts() As String = emailAddress.Split("@") 
      If mailParts.Length <> 2 Then 
       If allowAlerts Then 
        MsgBox("Valid email addresses are formatted [[email protected]]. " & 
          "Your address is missing a header [i.e. ""@domain.tld""].", 
          MsgBoxStyle.Exclamation, "No Header Specified") 
       End If 
       Return False 
      End If 
      If mailParts(mailParts.GetLowerBound(0)) = "" Then 
       If allowAlerts Then 
        MsgBox("Valid email addresses are formatted [[email protected]]. " & 
          "The username portion of the e-mail address you provided (before the @ symbol) is empty.", 
          MsgBoxStyle.Exclamation, "Invalid Email User") 
       End If 
       Return False 
      End If 
      Dim headerParts() As String = mailParts(mailParts.GetUpperBound(0)).Split(".") 
      If disallowLocalDomain AndAlso headerParts.Length < 2 Then 
       If allowAlerts Then 
        MsgBox("Valid email addresses are formatted [[email protected]]. " & 
          "Although addresses formatted like [[email protected]] are valid, " & 
          "only addresses with headers like ""sample.org"", ""sample.com"", and etc. " & 
          "[i.e. @domain.org] are accepted.", 
          MsgBoxStyle.Exclamation, "Invalid Header") 
       End If 
       Return False 
      ElseIf headerParts(headerParts.GetLowerBound(0)) = "" Or 
        headerParts(headerParts.GetUpperBound(0)) = "" Then 
       If allowAlerts Then 
        MsgBox("Valid email addresses are formatted [[email protected]]. " & 
          "Your header """ & mailParts(mailParts.GetUpperBound(0)) & """ is invalid.", 
          MsgBoxStyle.Exclamation, "Invalid Header") 
       End If 
       Return False 
      End If 
      Dim address As MailAddress = New MailAddress(emailAddress) 
     Catch ex As Exception 
      If allowAlerts Then 
       MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Invalid Email Address") 
      End If 
      Return False 
     End Try 
     Return True 
    End Function 

End Class 'email' 
Powiązane problemy