﻿Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Net.Mail

Public Class sms
    Private Const SMTP_SERVER As String = "mail.dailytip.net"
    Private Const SMTP_USERNAME As String = "affan@dailytip.net"
    Private Const SMTP_PASSWORD As String = "231960"
    Private Const SMTP_PORT As String = "587" 'leave it as string!
    Private Const DEFAULT_FROM_EMAIL As String = "smsapplication@gmail.com"

    Private _ListOfNumbers As New Dictionary(Of String, String)
    Private _ListOfProviders As New Dictionary(Of String, String)
    Private _phonenumber As String
    Private _FromEmail As String
#Region "Properties"


    Public ReadOnly Property FromEmail() As String
        Get
            Return _FromEmail
        End Get
    End Property

    Private ReadOnly Property PhoneNumber() As String
        Get
            Return _phonenumber
        End Get
    End Property
  
    Public ReadOnly Property ListOfProviders() As Dictionary(Of String, String)
        Get
            Return _ListOfProviders
        End Get

    End Property
    Public ReadOnly Property ListOfNumbers() As Dictionary(Of String, String)
        Get
            Return _ListOfNumbers
        End Get

    End Property

    Private _message As String
    Private Property Message() As String
        Get
            Return _message
        End Get
        Set(ByVal value As String)
            _message = value
        End Set
    End Property


    Private _subject As String
    Private Property Subject() As String
        Get
            Return _subject
        End Get
        Set(ByVal value As String)
            _subject = value
        End Set
    End Property
#End Region


    Public Sub New()
        LoadProviders()
        Message = String.Empty
        Subject = String.Empty
        _FromEmail = "smsapplication@gmail.com"
    End Sub

    Public Sub AddSingle(ByVal number As String, ByVal provider As String)

        _phonenumber = number
        If CheckNumber() Then
            If DoesProviderExist(provider) Then
                _ListOfNumbers.Add(PhoneNumber, provider)
            End If
        End If
    End Sub

    Public Sub AddMessage(ByVal mess As String)
        If String.IsNullOrEmpty(mess) Then
            Message = String.Empty
        Else
            Message = mess
        End If
    End Sub

    Public Sub AddSubject(ByVal subj As String)
        If String.IsNullOrEmpty(subj) Then
            Subject = String.Empty
        Else
            Subject = subj
        End If
    End Sub

    Public Sub AddFromEmail(ByVal e As String)
        If String.IsNullOrEmpty(e) Then
            _FromEmail = DEFAULT_FROM_EMAIL
        Else
            Dim r As New Regex("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$")
            If Not r.IsMatch(e) Then
                _FromEmail = DEFAULT_FROM_EMAIL
                Console.WriteLine("Email is not in the right format, switching to default")
            End If
            _FromEmail = e
        End If
    End Sub

    Public Sub Delete(ByVal number As String)
        _ListOfNumbers.Remove(number)
    End Sub

    Public Sub SendAll()
        Dim fullEmail As String = String.Empty
        For Each num In ListOfNumbers
            Dim pro As String = String.Empty
            If ListOfProviders.TryGetValue(num.Value, pro) Then
                Dim temp As String = pro.Replace("number", num.Key)
                SendEmail(temp)
            Else
                Console.WriteLine("Send failed")
            End If
        Next
    End Sub

    Private Sub SendEmail(ByVal email As String)
        'send your email here.
        Console.WriteLine(String.Empty)
        Console.WriteLine("Sending Message to: " & PhoneNumber)

        'construct the email
        Dim em As New NetEmail(SMTP_SERVER, SMTP_USERNAME, SMTP_PASSWORD, SMTP_PORT)
        em.AddEmail(email)
        em.AddMessage(Message)
        em.EnableSSL = False
        em.isHTML = False
        em.Send(FromEmail, Subject)




    End Sub

    'this method parses the number, and removes any dashses.
    Private Function CheckNumber() As Boolean

        'remove one or zero.
        If PhoneNumber.StartsWith(0) Or PhoneNumber.StartsWith(1) Then
            _phonenumber = PhoneNumber.Remove(0, 1)
        End If

        'display message
        Console.WriteLine("")
        Console.WriteLine("-------------------------------------------")
        Console.WriteLine("Verifying Number")
        'remove all dashes and check for length
        _phonenumber = PhoneNumber.Replace("-", String.Empty)
        ' Check a (USA style) telephone number
        '1-800-201-1929 (true)
        '800-201-1929 (true)
        '201-1929 (false)
        Dim myRegex As New Regex( _
        "^1?\s*-?\s*(\d{3}|\(\s*\d{3}\s*\))\s*-?\s*\d{3}\s*-?\s*\d{4}$")
        If Not myRegex.IsMatch(PhoneNumber) Then
            Console.WriteLine("Please enter a valid number")
            Return False
        End If

        'display message
        Console.WriteLine("Number Verified:  " & PhoneNumber)


        Return True
    End Function
    Private Function DoesProviderExist(ByVal name As String) As Boolean

        If ListOfProviders.ContainsKey(name.Trim) Then
            Console.WriteLine("")
            Console.WriteLine("Carrier Found: " & name)
            Console.WriteLine("-------------------------------------------")

            Return True
        Else
            Console.WriteLine("")
            Console.WriteLine("We could not find the carrier you specified")
            Console.WriteLine("-------------------------------------------")
            Return False
        End If
    End Function


    Private Sub LoadProviders()
        'Dim reader As New IO.StreamReader(FilePath) ' create new streamreader
        'Do Until reader.EndOfStream
        '    Dim line As String = reader.ReadLine 'read single line
        '    Dim proArray() As String = line.Split(",") 'split the line
        '    ListOfProviders.Add(proArray(0).Trim, proArray(1).Trim) 'store both parts into dictionary
        'Loop
        'reader.Close()
        With _ListOfProviders
            .Add("711Speakout", "number@cingularme.com")
            .Add("Alaska", "number@msg.acsalaska.com")
            .Add("Alltel", "number@message.alltel.com")
            .Add("AT&T", "number@mms.att.net")
            .Add("Bell", "number@txt.bell.ca")
            .Add("Cingular", "number@cingularme.com")
            .Add("Cricket", "number@mms.mycricket.com")
            .Add("Fido", "number@fido.ca")
            .Add("ITCompany", "number@itcompany.com.au")
            .Add("Meteor", "number@sms.mymeteor.ie")
            .Add("MetroPCS", "number@mymetropcs.com")
            .Add("MTS", "number@text.mtsmobility.com")
            .Add("Rogers", "number@pcs.rogers.com")
            .Add("Sasktel", "number@sms.sasktel.com")
            .Add("SMSGlobal", "number@sms.smsglobal.com.au")
            .Add("Sprint", "number@messaging.sprintpcs.com")
            .Add("T-Mobile", "number@tmomail.net")
            .Add("Telus", "number@msg.telus.com")
            .Add("Verizon", "number@vtext.com")
            .Add("VirginCanada", "number@vmobile.ca")
            .Add("VirginUSA", "number@vmobl.com")
        End With

    End Sub

End Class


Public Class NetEmail

    Private _SMTPAddress As String
    Private _username As String
    Private _Password As String
    Private _message As String
    Private _senderemail As String
    Private MailEntity As MailMessage

    Private Property SMTPAddress() As String
        Get
            Return _SMTPAddress
        End Get
        Set(ByVal value As String)
            _SMTPAddress = value
        End Set
    End Property

    Private Property UserName() As String
        Get
            Return _username
        End Get
        Set(ByVal value As String)
            _username = value
        End Set
    End Property


    Private Property Password() As String
        Get
            Return _Password
        End Get
        Set(ByVal value As String)
            _Password = value
        End Set
    End Property


    Private _port As String
    Private Property Port() As String
        Get
            Return _port
        End Get
        Set(ByVal value As String)
            _port = value
        End Set
    End Property


    Private _enableSSL As Boolean
    Public Property EnableSSL() As Boolean
        Get
            Return _enableSSL
        End Get
        Set(ByVal value As Boolean)
            _enableSSL = value
        End Set
    End Property


    Private _isHTML As Boolean
    Public Property isHTML() As Boolean
        Get
            Return _isHTML
        End Get
        Set(ByVal value As Boolean)
            _isHTML = value
        End Set
    End Property


    Private _timeout As Integer
    Public Property Timeout() As Integer
        Get
            Return _timeout
        End Get
        Set(ByVal value As Integer)
            _timeout = value
        End Set
    End Property

    Public Sub New(ByVal smtp As String, ByVal user As String, ByVal pass As String, ByVal iPort As String)
        SMTPAddress = smtp.Trim
        UserName = user.Trim
        Password = pass.Trim
        isHTML = True
        If iPort = 0 Then
            Port = String.Empty
        Else
            Port = iPort.Trim
        End If

        MailEntity = New MailMessage
        EnableSSL = False
        Timeout = 5000
    End Sub

    Public Sub AddEmail(ByVal mail As String)
        MailEntity.To.Add(New MailAddress(mail))
    End Sub

    Public Sub AddMessage(ByVal mess As String)
        MailEntity.Body = mess
    End Sub

    Public Sub AttachFile(ByVal filename As String)
        MailEntity.Attachments.Add(New Attachment(filename))
    End Sub

    Public Function Send(ByVal from As String, ByVal subject As String)

       
        Try

            MailEntity.From = New MailAddress(from)
            MailEntity.Subject = subject
            MailEntity.Priority = MailPriority.Normal

            ' Send the mail message
            Dim mSmtpClient As New SmtpClient(SMTPAddress)
            If Not String.IsNullOrEmpty(Port) Then
                mSmtpClient.Port = Port
            End If
            mSmtpClient.Credentials = New System.Net.NetworkCredential(UserName, Password)
            mSmtpClient.EnableSsl = EnableSSL
            mSmtpClient.Timeout = Timeout

            mSmtpClient.Send(MailEntity)

            Console.WriteLine("Message sent!")
            Console.WriteLine("")
            Return True
        Catch ex As SmtpException
            Console.WriteLine(ex.Message)
            Console.WriteLine(ex.StatusCode)

            Return False
        Catch ex As Exception
            ' MessageBox.Show("this is executed")
            Console.WriteLine(ex.Message)

            Return False

        End Try

    End Function

End Class

