Hi,
Here is some vb.net code I wrote based on this c# code.
http://www.developerfusion.co.uk/show/4472/
NNTP class
Imports System.Net.Sockets
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Public Class NNTP
Inherits System.Net.Sockets.TcpClient
Public Event MessageHeaderRecieved(ByVal sender As Object, ByVal e As
NNPTEventArgs)
Dim arNews As New NewsGroupMessageTable
Dim mstrGroupName As String
#Region "Message custom Hashtable"
<Serializable()> _
Private Class NewsGroupMessageTable
Inherits System.Collections.DictionaryBase
Default Public Property Item(ByVal key As [String]) As
NNTP.NewsGroupMessage
Get
Return CType(Dictionary(key), NNTP.NewsGroupMessage)
End Get
Set(ByVal Value As NNTP.NewsGroupMessage)
Dictionary(key) = Value
End Set
End Property
Public ReadOnly Property Keys() As ICollection
Get
Return Dictionary.Keys
End Get
End Property
Public ReadOnly Property Values() As ICollection
Get
Return Dictionary.Values
End Get
End Property
Public Sub Add(ByVal key As [String], ByVal value As
NNTP.NewsGroupMessage)
Dictionary.Add(key, value)
End Sub 'Add
Public Function Contains(ByVal key As [String]) As Boolean
Return Dictionary.Contains(key)
End Function 'Contains
Public Sub Remove(ByVal key As [String])
Dictionary.Remove(key)
End Sub 'Remove
End Class
#End Region
#Region "News group message class"
<Serializable()> _
Public Class NewsGroupMessage
Dim mstrSubject As String
Dim mstrTo As String
Dim mstrFrom As String
Dim mstrBody As String
Dim mdtMessage As Date
Dim intId As Long
Dim strRef As String = "None"
Public Property ID() As Long
Get
Return intId
End Get
Set(ByVal Value As Long)
intId = Value
End Set
End Property
Public Property MessageDate() As Date
Get
Return mdtMessage
End Get
Set(ByVal Value As Date)
mdtMessage = Value
End Set
End Property
Public Property Subject() As String
Get
Return mstrSubject
End Get
Set(ByVal Value As String)
mstrSubject = Value
End Set
End Property
Public Property From() As String
Get
Return mstrFrom
End Get
Set(ByVal Value As String)
mstrFrom = Value
End Set
End Property
Public Property Body() As String
Get
Return mstrBody
End Get
Set(ByVal Value As String)
mstrBody = Value
End Set
End Property
Public Property MessageReference() As String
Get
Return strRef
End Get
Set(ByVal Value As String)
strRef = Value
End Set
End Property
Public Property MessageTo() As String
Get
Return mstrTo
End Get
Set(ByVal Value As String)
mstrTo = Value
End Set
End Property
End Class
#End Region
#Region "NNPT event args"
Public Class NNPTEventArgs
Inherits EventArgs
Private mintMax As Long
Private mintpos As Long
Private mMessage As NewsGroupMessage
Public Sub New(ByVal max As Long, ByVal pos As Long, ByVal message
As NewsGroupMessage)
mintMax = max
mintpos = pos
mMessage = message
End Sub
Public ReadOnly Property Max() As Long
Get
Return mintMax
End Get
End Property
Public ReadOnly Property Pos() As Long
Get
Return mintpos
End Get
End Property
Public ReadOnly Property Message() As NewsGroupMessage
Get
Return mMessage
End Get
End Property
End Class
#End Region
Public Overloads Sub Connect(ByVal server As String)
Dim strResponse As String
Connect(server, 119)
strResponse = Response()
If strResponse.Substring(0, 3) <> "200" Then
Throw New NntpException(Response)
End If
End Sub 'Connect
Public Sub Disconnect()
Dim message As String
Dim strResponse As String
message = "QUIT" + vbCr + vbLf
Write(message)
strResponse = Response()
If strResponse.Substring(0, 3) <> "205" Then
'Throw New NntpException(Response)
End If
Dim strFile As String = String.Format("{0}.ngd",
CurrentNewsGroup.Replace(".", ""))
Dim fs As New FileStream(strFile, FileMode.OpenOrCreate)
'Get a Binary Formatter instance
Dim bf As New BinaryFormatter
'Serialize t
bf.Serialize(fs, arNews)
End Sub 'Disconnect
Public Function GetNewsgroups() As ArrayList
Dim message As String
Dim strResponse As String
Dim retval As New ArrayList
message = "LIST" & vbCr & vbLf
Write(message)
strResponse = Response()
If strResponse.Substring(0, 3) <> "215" Then
Throw New NntpException(Response)
End If
While True
strResponse = Response()
If strResponse = "." + vbCr + vbLf OrElse strResponse = "." +
vbLf Then
Return retval
Else
Dim seps As Char = " "c
Dim values As String() = strResponse.Split(seps)
retval.Add(values(0))
End If
End While
End Function 'GetNewsgroups
Private Function Response() As String
Dim enc As New System.Text.ASCIIEncoding
Dim serverbuff() As Byte = New [Byte](1023) {}
Dim stream As NetworkStream = GetStream()
Dim count As Integer = 0
While True
Dim buff() As Byte = New [Byte](1) {}
Dim bytes As Integer = stream.Read(buff, 0, 1)
If bytes = 1 Then
serverbuff(count) = buff(0)
count += 1
If buff(0) = Asc(vbLf) Then
Exit While
End If
Else
Exit While
End If
End While
Dim retval As String = enc.GetString(serverbuff, 0, count)
'Debug.WriteLine("READ:" + retval)
Return retval
End Function 'Response
Public Property CurrentNewsGroup() As String
Get
Return mstrGroupName
End Get
Set(ByVal Value As String)
mstrGroupName = Value
End Set
End Property
Public Sub GetNewsHeaders()
Dim message As String
Dim strResponse As String = "211"
If CurrentNewsGroup Is Nothing Then
MessageBox.Show("Set news group first")
Return
End If
message = "GROUP " + CurrentNewsGroup + vbCr + vbLf
Write(message)
strResponse = Response()
If strResponse.Substring(0, 3) <> "211" Then
Throw New NntpException(strResponse)
End If
Dim seps As Char = " "c
Dim values As String() = strResponse.Split(seps)
Dim start As Long = Int32.Parse(values(2))
Dim [end] As Long = Int32.Parse(values(3))
If start + 500 < [end] AndAlso [end] > 500 Then
start = [end] - 500
End If
Dim strFile As String = String.Format("{0}.ngd",
CurrentNewsGroup.Replace(".", ""))
If File.Exists(strFile) Then
Dim fs As New FileStream(strFile, FileMode.Open)
'Get a Binary Formatter instance
Dim bf As New BinaryFormatter
'Deserialize c from strFilename2
'Note that the deserialized object must be cast to the proper
type.
arNews = CType(bf.Deserialize(fs), NewsGroupMessageTable)
'Close the file and release resources (avoids GC delays)
fs.Close()
End If
Dim l As Long = 0
For Each c As NewsGroupMessage In arNews.Values
l += 1
RaiseEvent MessageHeaderRecieved(Me, New
NNPTEventArgs(arNews.Count, l, c))
Next
Dim i As Long
For i = [end] To start Step -1
If Not arNews.Contains(i.ToString) Then
message = "HEAD " & i & vbCr & vbLf
Write(message)
strResponse = Response()
If strResponse.Substring(0, 3) <> "423" Then
If strResponse.Substring(0, 3) <> "221" Then
Throw New NntpException(strResponse)
Else
Dim article As New NewsGroupMessage
article.ID = i
Dim intLine As Integer = 0
Dim strMessage As String
While True
strResponse = Response()
If strResponse = "." + vbCr + vbLf Then
Exit While
End If
If strResponse = "." + vbLf Then
Exit While
End If
If strResponse.IndexOf("From:") = 0 Then
article.From = strResponse.Substring(6)
ElseIf strResponse.IndexOf("Subject") = 0 Then
article.Subject = strResponse.Substring(9)
ElseIf strResponse.IndexOf("References:") = 0
Then
article.MessageReference =
strResponse.Substring(12)
ElseIf strResponse.IndexOf("Message-ID:") = 0
Then
strMessage = strResponse.Substring(12)
ElseIf strResponse.IndexOf("Newsgroups:") = 0
Then
article.MessageTo =
strResponse.Substring(12)
ElseIf strResponse.IndexOf("Date:") = 0 Then
Dim intEnd As Integer =
strResponse.IndexOf("-") - 6
If intEnd < 0 Then
intEnd = strResponse.IndexOf("+") - 6
End If
If intEnd < 0 Then
intEnd = strResponse.IndexOf("GMT") - 6
End If
article.MessageDate =
DateTime.Parse(strResponse.Substring(6, intEnd))
End If
intLine += 1
End While
If article.MessageReference = "None" Then
article.MessageReference = strMessage
arNews.Add(article.ID.ToString, article)
Dim z As Long = [end] - start
RaiseEvent MessageHeaderRecieved(Me, New
NNPTEventArgs([end] - start, z - (i - start), article))
End If
End If
End If
Next i
End Sub 'GetNews
Public Function GetNewsArticle(ByVal MessageID As Long) As String
Dim message As String
Dim strResponse As String
If CurrentNewsGroup Is Nothing Then
MessageBox.Show("Set news group first")
Return Nothing
End If
message = "GROUP " + CurrentNewsGroup + vbCr + vbLf
Write(message)
strResponse = Response()
If strResponse.Substring(0, 3) <> "211" Then
Throw New NntpException(strResponse)
End If
message = "BODY " & MessageID.ToString & vbCr & vbLf
Write(message)
Dim article As String
strResponse = Response()
If strResponse.Substring(0, 3) <> "423" Then
If strResponse.Substring(0, 3) <> "222" Then
Throw New NntpException(strResponse)
Else
Dim intLine As Integer = 0
Dim strMessage As String
While True
strResponse = Response()
If strResponse = "." + vbCr + vbLf Then
Exit While
End If
If strResponse = "." + vbLf Then
Exit While
End If
article &= strResponse
End While
DirectCast(arNews.Item(MessageID.ToString),
NewsGroupMessage).Body = article
End If
End If
Return article
End Function 'GetNews
Public Sub Post(ByVal newsgroup As String, ByVal subject As String,
ByVal from As String, ByVal content As String)
Dim message As String
Dim strResponse As String
message = "POST " & newsgroup & vbCr & vbLf
Write(message)
strResponse = Response()
If strResponse.Substring(0, 3) <> "340" Then
Throw New NntpException(Response)
End If
message = "From: " & from & vbCr & vbLf & "Newsgroups: " & newsgroup
& vbCr & vbLf & "Subject: " & subject & vbCr & vbLf & vbCr & vbLf & content
& vbCr & vbLf & "." & vbCr & vbLf
Write(message)
strResponse = Response()
If strResponse.Substring(0, 3) <> "240" Then
Throw New NntpException(Response)
End If
End Sub 'Post
Private Sub Write(ByVal message As String)
Dim en As New System.Text.ASCIIEncoding
Dim WriteBuffer(1023) As Byte
WriteBuffer = en.GetBytes(message)
Dim stream As NetworkStream = GetStream()
stream.Write(WriteBuffer, 0, WriteBuffer.Length)
'Debug.WriteLine("WRITE:" & message)
End Sub 'Write
End Class
Public Class NewsGroupMessageCollection
Inherits System.Collections.CollectionBase
Public Sub Add(ByVal value As NNTP.NewsGroupMessage)
list.Add(value)
End Sub
Public Sub Remove(ByVal Index As Integer)
If Index > Count - 1 Or Index < 0 Then
Throw New Exception("Invalid Index")
Else
list.RemoveAt(Index)
End If
End Sub
Public ReadOnly Property Item(ByVal Index As Integer) As
NNTP.NewsGroupMessage
Get
Return CType(list.Item(Index), NNTP.NewsGroupMessage)
End Get
End Property
End Class
Public Class NntpException
Inherits System.ApplicationException
Public Sub New(ByVal str As String)
MyBase.New(str)
End Sub 'New
End Class 'NntpException
Ken
------------------
"Jim" <re***@groups.please> wrote in message
news:yq*******************@bignews4.bellsouth.net. ..
Have you seen any NNTP classes that I may use or build upon to build a
simple newsreader/downloader?
Is there such a class in the .Net framework that I have overlooked? If
not,
inclusion of RFC documented protocols would certainly be a good idea for a
class hierarchy in the .Net classes.
You could adopt and extend the classes at will. Who knows......maybe even
improve one and get a new RFC implemented.
Any help you could give would be great. (FYI the RFCs for NNTP protocols
are at http://www.ietf.org/rfc/rfc0977.txt?number=977 and the RFC for the
USENET mail standard is at
http://www.ietf.org/rfc/rfc0850.txt?number=850.)
Thanks!