Advertisement
Guest User

ElektroKit public sample - validatemail

a guest
Jun 25th, 2017
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.93 KB | None | 0 0
  1. ' ***********************************************************************
  2. ' Author   : Elektro
  3. ' Modified : 26-April-2017
  4. ' ***********************************************************************
  5.  
  6. #Region " Public Members Summary "
  7.  
  8. #Region " Functions "
  9.  
  10. ' ValidateMail(String) As Boolean
  11. ' ValidateMail(MailAddress) As Boolean
  12.  
  13. #End Region
  14.  
  15. #End Region
  16.  
  17. #Region " Option Statements "
  18.  
  19. Option Strict On
  20. Option Explicit On
  21. Option Infer Off
  22.  
  23. #End Region
  24.  
  25. #Region " Imports "
  26.  
  27. Imports System.Net.Mail
  28.  
  29. 'Imports Elektro.Core.Types
  30.  
  31. #End Region
  32.  
  33. #Region " Mail Util "
  34.  
  35. Namespace Tools
  36.  
  37.     Partial Public NotInheritable Class MailUtil ': Inherits AestheticObject
  38.  
  39. #Region " Public Methods "
  40.  
  41.         ''' ----------------------------------------------------------------------------------------------------
  42.         ''' <summary>
  43.         ''' Validates a mail address.
  44.         ''' </summary>
  45.         ''' ----------------------------------------------------------------------------------------------------
  46.         ''' <example> This is a code example.
  47.         ''' <code>
  48.         ''' Dim isValid As Boolean = ValidateMail("Address@Gmail.com")
  49.         ''' Dim isValid As Boolean = ValidateMail("mailto:Address@Gmail.com")
  50.         ''' </code>
  51.         ''' </example>
  52.         ''' ----------------------------------------------------------------------------------------------------
  53.         ''' <param name="address">
  54.         ''' The mail address.
  55.         ''' </param>
  56.         ''' ----------------------------------------------------------------------------------------------------
  57.         ''' <returns>
  58.         ''' <see langword="True"/> if the mail address is valid, <see langword="False"/> otherwise.
  59.         ''' </returns>
  60.         ''' ----------------------------------------------------------------------------------------------------
  61.         <DebuggerStepThrough>
  62.         Public Shared Function ValidateMail(ByVal address As String) As Boolean
  63.  
  64.             If Not address.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase) Then
  65.                 address = address.Insert(0, "mailto:")
  66.             End If
  67.  
  68.             If Not Uri.IsWellFormedUriString(address, UriKind.Absolute) Then
  69.                 Return False
  70.             End If
  71.  
  72.             Dim result As Uri = Nothing
  73.             If Uri.TryCreate(address, UriKind.Absolute, result) Then
  74.                 Return (result.Scheme = Uri.UriSchemeMailto)
  75.  
  76.             Else
  77.                 Return False
  78.  
  79.             End If
  80.  
  81.         End Function
  82.  
  83.         ''' ----------------------------------------------------------------------------------------------------
  84.         ''' <summary>
  85.         ''' Validates a mail address.
  86.         ''' </summary>
  87.         ''' ----------------------------------------------------------------------------------------------------
  88.         ''' <example> This is a code example.
  89.         ''' <code>
  90.         ''' Dim isValid As Boolean = ValidateMail(New MailAddress("Address@Gmail.com"))
  91.         ''' Dim isValid As Boolean = ValidateMail(New MailAddress("mailto:Address@Gmail.com"))
  92.         ''' </code>
  93.         ''' </example>
  94.         ''' ----------------------------------------------------------------------------------------------------
  95.         ''' <param name="address">
  96.         ''' The mail address.
  97.         ''' </param>
  98.         ''' ----------------------------------------------------------------------------------------------------
  99.         ''' <returns>
  100.         ''' <see langword="True"/> if the mail address is valid, <see langword="False"/> otherwise.
  101.         ''' </returns>
  102.         ''' ----------------------------------------------------------------------------------------------------
  103.         <DebuggerStepThrough>
  104.         Public Shared Function ValidateMail(ByVal address As MailAddress) As Boolean
  105.  
  106.             Return MailUtil.ValidateMail(address.Address)
  107.  
  108.         End Function
  109.  
  110. #End Region
  111.  
  112.     End Class
  113.  
  114. End Namespace
  115.  
  116. #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement