Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SMTP Authentication

Status
Not open for further replies.

astrodestino

IS-IT--Management
Feb 19, 2005
179
AR
Hi!
I am working on an application that has to work on windows CE and part of this application must send an email but I found that I cannot authenticate. I found a class that can authenticate thru smtp but i wont authenticate thru pop.
Does anyone knows how to authenticate thru pop?
I dont know anything about email and that kind of stuff and I'm stuck here.
Any help would be appreciated.
Here is the class code that I found:

Code:
Imports System
Imports System.Net
Imports System.Text

Public Class Mailer

    Public Enum MailFormat
        Html ''As 
        Text ''As 
    End Enum
    Private _err As System.Exception
    Private Smtp As SmtpServer

    Public Function SendMail(ByVal from As String, ByVal to2 As String, ByVal subject As String, ByVal message As String, ByVal serverName As String) As Boolean
        Return SendMail(from, to2, subject, message, MailFormat.Html, String.Empty, String.Empty, serverName, 25)
    End Function

    Public Function SendMail(ByVal from As String, ByVal to2 As String, ByVal subject As String, ByVal message As String, ByVal AUTH_Name As String, ByVal AUTH_Password As String, ByVal serverName As String) As Boolean
        Return SendMail(from, to2, subject, message, MailFormat.Html, AUTH_Name, AUTH_Password, serverName, 25)
    End Function

    Public Function SendMail(ByVal from As String, ByVal to2 As String, ByVal subject As String, ByVal message As String, ByVal format2 As MailFormat, ByVal AUTH_Name As String, ByVal AUTH_Password As String, ByVal serverName As String, ByVal port As Integer) As Boolean
        Smtp = New SmtpServer
        If Not Connect(serverName, port) Then
            Return False
        End If
        If Not ((AUTH_Name.Length + AUTH_Password.Length) = 0) Then
            If Not AuthLogin(AUTH_Name, AUTH_Password) Then
                Return False
            End If
        End If
        If Not SendMessage(from, to2, subject, message, format2) Then
            Return False
        End If
        If Not Disconnect() Then
            Return False
        End If
        Return True
    End Function

    Private Function Connect(ByVal server As String, ByVal port As Integer) As Boolean
        If Smtp.Connect(server, port) Then
            Try
                If Not Smtp.ResponseOK("220") Then
                    Throw New Exception("Error trying to connect to server " + server)
                End If
                SendCommand("helo " + server, "250")
                Return True
            Catch ex As Exception
                _err = ex
                Return False
            End Try
        End If
        _err = Smtp.Err
        Return False
    End Function

    Private Function Disconnect() As Boolean
        If Smtp.DisConnect Then
            Return True
        End If
        _err = Smtp.Err
        Return False
    End Function

    Private Function AuthLogin(ByVal usrName As String, ByVal password As String) As Boolean
        Try
            SendCommand("auth login", "334 VXNlcm5hbWU6")
            SendCommand(ToBase64(usrName), "334 UGFzc3dvcmQ6")
            SendCommand(ToBase64(password), "235")
            Return True
        Catch ex As Exception
            _err = ex
            Return False
        End Try
    End Function

    Private Function ToBase64(ByVal data As String) As String
        Dim Encoder As System.Text.ASCIIEncoding = New System.Text.ASCIIEncoding
        Return Convert.ToBase64String(Encoder.GetBytes(data))
    End Function

    Private Function SendMessage(ByVal from As String, ByVal to2 As String, ByVal subject As String, ByVal message As String, ByVal format As MailFormat) As Boolean
        Try
            SendCommand("MAIL FROM: " + from, "250")
            SendCommand("RCPT TO: " + to2, "250")
            SendCommand("DATA", "354")
            Smtp.SendData("From: " + from + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
            Smtp.SendData("To: " + to2 + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
            Smtp.SendData("Subject: " + subject + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
            If format = MailFormat.Html Then
                Smtp.SendData("Content-Type: text/html; charset=""ISO-8859-1"" " & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
            End If
            Smtp.SendData("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
            If format = MailFormat.Html Then
                Smtp.SendData(HTMLEncode(message) + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
            Else
                Smtp.SendData(message + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
            End If
            SendCommand(".", "250")
            Return True
        Catch ex As Exception
            _err = ex
            Return False
        End Try
    End Function

    Private Sub SendCommand(ByVal commmand As String, ByVal OK_Response As String)
        If Not Smtp.SendCommand(commmand) Then
            Throw Smtp.Err
        End If
        If Not Smtp.ResponseOK(OK_Response) Then
            Throw Smtp.Err
        End If
    End Sub

    Public ReadOnly Property Err() As System.Exception
        Get
            Return _err
        End Get
    End Property

    Private Function HTMLEncode(ByVal data As String) As String
        Dim sb As StringBuilder = New StringBuilder(data)
        sb.Replace("á", "á")
        sb.Replace("é", "é")
        sb.Replace("í", "í")
        sb.Replace("ó", "ó")
        sb.Replace("ú", "ú")
        sb.Replace("ñ", "ñ")
        Return sb.ToString
    End Function
End Class

Friend Class SmtpServer
    Private _err As System.Exception
    Private TheClient As System.Net.Sockets.TcpClient
    Private TheStream As System.Net.Sockets.NetworkStream
    Private TheReader As System.IO.StreamReader
    Private TheWriter As System.IO.StreamWriter
    Private DataWriter As System.IO.StreamWriter

    Public Sub New()
        TheClient = New System.Net.Sockets.TcpClient
    End Sub

    Public Function Connect(ByVal ServerName As String, ByVal Port As Integer) As Boolean
        Try
            TheClient.Connect(ServerName, Port)
            TheStream = TheClient.GetStream
            TheReader = New System.IO.StreamReader(TheStream)
            TheWriter = New System.IO.StreamWriter(TheStream)
            DataWriter = New System.IO.StreamWriter(TheStream)
            TheWriter.AutoFlush = True
            DataWriter.AutoFlush = True
            Return True
        Catch e As System.Exception
            _err = New System.Exception("Connect Error", e)
            Return False
        End Try
    End Function

    Public Function SendCommand(ByVal cmd As String) As Boolean
        Try
            TheWriter.WriteLine(cmd)
            Return True
        Catch e As Exception
            _err = New System.Exception("Send data error", e)
            Return False
        End Try
    End Function

    Public Function ResponseOK(ByVal resp As String) As Boolean
        Try
            Dim Data As String = Nothing
            Dim i As Integer = 0
            While Data Is Nothing
                System.Threading.Thread.Sleep(25 * i)
                Data = TheReader.ReadLine
                If System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1) = 4 Then
                    Throw New Exception("Timeout reached waiting server response.")
                End If
            End While
            If Not Data.StartsWith(resp) Then
                Throw New Exception("Invalid command response:" & Microsoft.VisualBasic.Chr(10) & "" + Data)
            End If
            Return True
        Catch e As System.Exception
            _err = New Exception(e.Message, e)
            Return False
        End Try
    End Function

    Public Function SendData(ByVal data As String) As Boolean
        Try
            DataWriter.Write(data)
            Return True
        Catch e As System.Exception
            _err = New System.Exception("Send data error", e)
            Return False
        End Try
    End Function

    Public Function DisConnect() As Boolean
        Try
            TheReader.Close()
            TheReader = Nothing
            TheWriter.Close()
            TheWriter = Nothing
            DataWriter.Close()
            DataWriter = Nothing
            TheStream.Close()
            TheStream = Nothing
            TheClient.Close()
            Return True
        Catch e As System.Exception
            _err = New System.Exception("Disconnect Error", e)
            Return False
        End Try
    End Function

    Public ReadOnly Property Err() As System.Exception
        Get
            Return _err
        End Get
    End Property
End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top