Files
2017-03-08 11:21:27 +01:00

373 lines
13 KiB
VB.net

Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports System.Net.Mail
Imports DevExpress.Xpo
Public Class SISPOP3Base
' variables for pop3 class
Public pop3host As [String]
Public port As Integer
Public user As [String]
Public pwd As [String]
Public command As [String]
Public w_TcpClient As TcpClient
Public w_NetStream As NetworkStream
Public w_ReadStream As StreamReader
Public bData As Byte()
'for the data, tat we'll recive
Public Function DoConnect(pop3host As [String], port As Integer, user As [String], pwd As [String]) As String
' create POP3 connection
w_TcpClient = New TcpClient(pop3host, port)
Try
' initialization
w_NetStream = w_TcpClient.GetStream()
w_ReadStream = New StreamReader(w_TcpClient.GetStream())
w_ReadStream.ReadLine()
' send login
command = "USER " & user & vbCr & vbLf
bData = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray())
w_NetStream.Write(bData, 0, bData.Length)
w_ReadStream.ReadLine()
' send pwd
command = "PASS " & pwd & vbCr & vbLf
bData = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray())
w_NetStream.Write(bData, 0, bData.Length)
w_ReadStream.ReadLine()
Catch err As InvalidOperationException
Return ("Error: " & err.ToString())
End Try
Return "+OK"
End Function
Public Function GetStat() As String
' Send STAT command to get number of mail and total size
command = "STAT" & vbCr & vbLf
bData = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray())
w_NetStream.Write(bData, 0, bData.Length)
Return w_ReadStream.ReadLine()
End Function
Public Function GetList() As String
' Send LIST command with no parametrs to get all information
Dim sTemp As String
' For saving 'list' results
Dim sList As String = ""
command = "LIST" & vbCr & vbLf
bData = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray())
w_NetStream.Write(bData, 0, bData.Length)
sTemp = w_ReadStream.ReadLine()
If sTemp(0) <> "-"c Then
' errors begins with '-'
While sTemp <> "."
'saving data to string while not found '.'
sList += sTemp & vbCr & vbLf
sTemp = w_ReadStream.ReadLine()
End While
Else
Return sTemp
End If
Return sList
End Function
Public Function GetList(num As Integer) As String
' Send LIST command with number of a letter
command = "LIST " & num & vbCr & vbLf
bData = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray())
w_NetStream.Write(bData, 0, bData.Length)
Return w_ReadStream.ReadLine()
End Function
Public Function Retr(num As Integer) As String
Dim sTemp As String
Dim sBody As String = ""
Try
command = "RETR " & num & vbCr & vbLf
bData = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray())
w_NetStream.Write(bData, 0, bData.Length)
sTemp = w_ReadStream.ReadLine()
If sTemp(0) <> "-"c Then
'errors begins with -
While sTemp <> "."
' . - is the end of the server response
sBody += sTemp & vbCr & vbLf
sTemp = w_ReadStream.ReadLine()
End While
Else
Return sTemp
End If
Catch err As InvalidOperationException
Return ("Error: " & err.ToString())
End Try
Return sBody
End Function
Public Function Dele(num As Integer) As String
' Send DELE command to delete message with specified number
command = "DELE " & num & vbCr & vbLf
bData = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray())
w_NetStream.Write(bData, 0, bData.Length)
Return w_ReadStream.ReadLine()
End Function
Public Function Rset() As String
' Send RSET command to unmark all deleteting messages
command = "RSET" & vbCr & vbLf
bData = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray())
w_NetStream.Write(bData, 0, bData.Length)
Return w_ReadStream.ReadLine()
End Function
Public Function Quit() As String
' Send QUIT
command = "QUIT" & vbCr & vbLf
bData = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray())
w_NetStream.Write(bData, 0, bData.Length)
Dim tmp As [String] = w_ReadStream.ReadLine()
w_NetStream.Close()
w_ReadStream.Close()
Return tmp
End Function
Public Function GetTop(num As Integer) As String
Dim sTemp As String
Dim [sTop] As String = ""
Try
' retrieve mail with number mail parameter
command = "TOP " & num & " n" & vbCr & vbLf
bData = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray())
w_NetStream.Write(bData, 0, bData.Length)
sTemp = w_ReadStream.ReadLine()
If sTemp(0) <> "-"c Then
While sTemp <> "."
[sTop] += sTemp & vbCr & vbLf
sTemp = w_ReadStream.ReadLine()
End While
Else
Return sTemp
End If
Catch err As InvalidOperationException
Return ("Error: " & err.ToString())
End Try
Return [sTop]
End Function
Public Function GetTop(num_mess As Integer, num_strok As Integer) As String
Dim sTemp As String
Dim [sTop] As String = ""
Try
' retrieve mail with number mail parameter
command = "TOP " & num_mess & " " & num_strok & vbCr & vbLf
bData = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray())
w_NetStream.Write(bData, 0, bData.Length)
sTemp = w_ReadStream.ReadLine()
If sTemp(0) <> "-"c Then
While sTemp <> "."
[sTop] += sTemp & vbCr & vbLf
sTemp = w_ReadStream.ReadLine()
End While
Else
Return sTemp
End If
Catch err As InvalidOperationException
Return ("Error: " & err.ToString())
End Try
Return [sTop]
End Function
Public Function GetUidl() As String
Dim sTemp As String
Dim sUidl As String = ""
command = "UIDL" & vbCr & vbLf
bData = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray())
w_NetStream.Write(bData, 0, bData.Length)
sTemp = w_ReadStream.ReadLine()
If sTemp(0) <> "-"c Then
' errors begins with '-'
While sTemp <> "."
'saving data to string while not found '.'
sUidl += sTemp & vbCr & vbLf
sTemp = w_ReadStream.ReadLine()
End While
Else
Return sTemp
End If
Return sUidl
End Function
Public Function GetUidl(num As Integer) As String
command = "UIDL " & num & vbCr & vbLf
bData = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray())
w_NetStream.Write(bData, 0, bData.Length)
Return w_ReadStream.ReadLine()
End Function
Public Function GetNoop() As String
' Send NOOP command to check if we are connected
command = "NOOP" & vbCr & vbLf
bData = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray())
w_NetStream.Write(bData, 0, bData.Length)
Return w_ReadStream.ReadLine()
End Function
End Class
Public Class SISPOP3Message
Public Function GetFrom(messTop As String) As String
messTop = messTop.Remove(0, (messTop.IndexOf(vbCr & vbLf & "From:") + 7))
messTop = messTop.Remove(messTop.IndexOf(ControlChars.Cr), ((messTop.Length - messTop.IndexOf(ControlChars.Cr)) - 1))
Return messTop
End Function
Public Function GetDate(messTop As String) As String
messTop = messTop.Remove(0, (messTop.IndexOf(vbCr & vbLf & "Date:") + 7))
messTop = messTop.Remove(messTop.IndexOf(ControlChars.Cr), (messTop.Length - messTop.IndexOf(ControlChars.Cr)))
Return messTop
End Function
Public Function GetMessID(messTop As String) As String
messTop = messTop.Remove(0, (messTop.IndexOf(vbCr & vbLf & "Message-ID: ") + 13))
messTop = messTop.Remove(messTop.IndexOf(ControlChars.Cr), (messTop.Length - messTop.IndexOf(ControlChars.Cr)))
Return messTop
End Function
Public Function GetTo(messTop As String) As String
messTop = messTop.Remove(0, (messTop.IndexOf(vbCr & vbLf & "To:") + 6))
messTop = messTop.Remove(messTop.IndexOf(ControlChars.Cr), (messTop.Length - messTop.IndexOf(ControlChars.Cr)))
Return messTop
End Function
Public Function GetSubject(messTop As String) As String
messTop = messTop.Remove(0, (messTop.IndexOf(vbCr & vbLf & "Subject:") + 10))
messTop = messTop.Remove(messTop.IndexOf(ControlChars.Cr), (messTop.Length - messTop.IndexOf(ControlChars.Cr)))
Return messTop
End Function
Public Function GetBody(AllMessage As String) As String
AllMessage = AllMessage.Remove(0, (AllMessage.IndexOf(vbCr & vbLf & vbCr & vbLf)))
Return AllMessage
End Function
End Class
'Public Class SISSendMail
' ReadOnly Property ContactsTo As XPCollection(Of Contacts)
' Get
' Return New xpcoll
' End Get
' End Property
' Private Function SendMail(_Host As String, _Port As Long, _FromAddress As String, _Subject As String, _Body As String) As String
' Try
' Dim smtp As New SmtpClient()
' Dim message As New MailMessage()
' smtp.Host = _Host
' smtp.Port = _Port
' smtp.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials
' 'smtp.Credentials = new System.Net.NetworkCredential("username", "password");
' message.Subject = _Subject
' message.Body = _Body
' message.From = New MailAddress(_FromAddress)
' message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
' message.Priority = MailPriority.Normal
' ' Make the list of recipients.
' If [To].Count > 0 Then
' For Each item As Contact In [To]
' message.[To].Add(New MailAddress(item.Email, item.FullName))
' Next
' End If
' If CC.Count > 0 Then
' For Each item As Contact In CC
' message.CC.Add(New MailAddress(item.Email, item.FullName))
' Next
' End If
' If Bcc.Count > 0 Then
' For Each item As Contact In Bcc
' message.Bcc.Add(New MailAddress(item.Email, item.FullName))
' Next
' End If
' ' Create the file attachment (from the File property) for this e-mail message.
' If File IsNot Nothing Then
' Dim tempFileName As String = Oid.ToString()
' Using fileStream As New FileStream(tempFileName, FileMode.OpenOrCreate)
' File.SaveToStream(fileStream)
' fileStream.Position = 0
' ' Create attachment by using existing fileStream.
' Dim data As New Attachment(fileStream, System.Net.Mime.MediaTypeNames.Application.Octet)
' ' Add time stamp information for the file.
' Dim disposition As System.Net.Mime.ContentDisposition = data.ContentDisposition
' disposition.FileName = File.FileName
' disposition.Size = fileStream.Length
' disposition.CreationDate = System.IO.File.GetCreationTime(tempFileName)
' disposition.ModificationDate = System.IO.File.GetLastWriteTime(tempFileName)
' disposition.ReadDate = System.IO.File.GetLastAccessTime(tempFileName)
' ' Add the attachment to this message.
' message.Attachments.Add(data)
' ' Send the message.
' smtp.Send(message)
' data.Dispose()
' ' Delete temp file.
' If System.IO.File.Exists(tempFileName) Then
' System.IO.File.Delete(tempFileName)
' End If
' End Using
' End If
' Catch E As SmtpException
' Return "Mail send failed with message: " + E.Message
' End Try
' Return "Mail was send successfully"
' End Function
'End Class
'Dim pop3 As POP3class
'pop3 = New POP3class()
'pop3.DoConnect("your.mail.server", 110, "username", "password")
'pop3.GetStat()
'' and if we have mail:
'Dim msg As MessageClass
'msg = New MessageClass()
'Dim sMessageTop As String = msg.GetTop(1)
''OK, message is well, lets download it.
'Dim sAllMessage As String = msg.Retr(1)
'msg.GetBody(sAllMessage)