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.Sock ets
Imports System.IO
Imports System.Runtime. Serialization
Imports System.Runtime. Serialization.F ormatters.Binar y
Public Class NNTP
Inherits System.Net.Sock ets.TcpClient
Public Event MessageHeaderRe cieved(ByVal sender As Object, ByVal e As
NNPTEventArgs)
Dim arNews As New NewsGroupMessag eTable
Dim mstrGroupName As String
#Region "Message custom Hashtable"
<Serializable() > _
Private Class NewsGroupMessag eTable
Inherits System.Collecti ons.DictionaryB ase
Default Public Property Item(ByVal key As [String]) As
NNTP.NewsGroupM essage
Get
Return CType(Dictionar y(key), NNTP.NewsGroupM essage)
End Get
Set(ByVal Value As NNTP.NewsGroupM essage)
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.Valu es
End Get
End Property
Public Sub Add(ByVal key As [String], ByVal value As
NNTP.NewsGroupM essage)
Dictionary.Add( key, value)
End Sub 'Add
Public Function Contains(ByVal key As [String]) As Boolean
Return Dictionary.Cont ains(key)
End Function 'Contains
Public Sub Remove(ByVal key As [String])
Dictionary.Remo ve(key)
End Sub 'Remove
End Class
#End Region
#Region "News group message class"
<Serializable() > _
Public Class NewsGroupMessag e
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 MessageReferenc e() 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 NewsGroupMessag e
Public Sub New(ByVal max As Long, ByVal pos As Long, ByVal message
As NewsGroupMessag e)
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 NewsGroupMessag e
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.Sub string(0, 3) <> "200" Then
Throw New NntpException(R esponse)
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.Sub string(0, 3) <> "205" Then
'Throw New NntpException(R esponse)
End If
Dim strFile As String = String.Format(" {0}.ngd",
CurrentNewsGrou p.Replace(".", ""))
Dim fs As New FileStream(strF ile, FileMode.OpenOr Create)
'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.Sub string(0, 3) <> "215" Then
Throw New NntpException(R esponse)
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.Spl it(seps)
retval.Add(valu es(0))
End If
End While
End Function 'GetNewsgroups
Private Function Response() As String
Dim enc As New System.Text.ASC IIEncoding
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(buf f, 0, 1)
If bytes = 1 Then
serverbuff(coun t) = 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(s erverbuff, 0, count)
'Debug.WriteLin e("READ:" + retval)
Return retval
End Function 'Response
Public Property CurrentNewsGrou p() 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 CurrentNewsGrou p Is Nothing Then
MessageBox.Show ("Set news group first")
Return
End If
message = "GROUP " + CurrentNewsGrou p + vbCr + vbLf
Write(message)
strResponse = Response()
If strResponse.Sub string(0, 3) <> "211" Then
Throw New NntpException(s trResponse)
End If
Dim seps As Char = " "c
Dim values As String() = strResponse.Spl it(seps)
Dim start As Long = Int32.Parse(val ues(2))
Dim [end] As Long = Int32.Parse(val ues(3))
If start + 500 < [end] AndAlso [end] > 500 Then
start = [end] - 500
End If
Dim strFile As String = String.Format(" {0}.ngd",
CurrentNewsGrou p.Replace(".", ""))
If File.Exists(str File) Then
Dim fs As New FileStream(strF ile, 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.Deseri alize(fs), NewsGroupMessag eTable)
'Close the file and release resources (avoids GC delays)
fs.Close()
End If
Dim l As Long = 0
For Each c As NewsGroupMessag e In arNews.Values
l += 1
RaiseEvent MessageHeaderRe cieved(Me, New
NNPTEventArgs(a rNews.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.Sub string(0, 3) <> "423" Then
If strResponse.Sub string(0, 3) <> "221" Then
Throw New NntpException(s trResponse)
Else
Dim article As New NewsGroupMessag e
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.Ind exOf("From:") = 0 Then
article.From = strResponse.Sub string(6)
ElseIf strResponse.Ind exOf("Subject") = 0 Then
article.Subject = strResponse.Sub string(9)
ElseIf strResponse.Ind exOf("Reference s:") = 0
Then
article.Message Reference =
strResponse.Sub string(12)
ElseIf strResponse.Ind exOf("Message-ID:") = 0
Then
strMessage = strResponse.Sub string(12)
ElseIf strResponse.Ind exOf("Newsgroup s:") = 0
Then
article.Message To =
strResponse.Sub string(12)
ElseIf strResponse.Ind exOf("Date:") = 0 Then
Dim intEnd As Integer =
strResponse.Ind exOf("-") - 6
If intEnd < 0 Then
intEnd = strResponse.Ind exOf("+") - 6
End If
If intEnd < 0 Then
intEnd = strResponse.Ind exOf("GMT") - 6
End If
article.Message Date =
DateTime.Parse( strResponse.Sub string(6, intEnd))
End If
intLine += 1
End While
If article.Message Reference = "None" Then
article.Message Reference = strMessage
arNews.Add(arti cle.ID.ToString , article)
Dim z As Long = [end] - start
RaiseEvent MessageHeaderRe cieved(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 CurrentNewsGrou p Is Nothing Then
MessageBox.Show ("Set news group first")
Return Nothing
End If
message = "GROUP " + CurrentNewsGrou p + vbCr + vbLf
Write(message)
strResponse = Response()
If strResponse.Sub string(0, 3) <> "211" Then
Throw New NntpException(s trResponse)
End If
message = "BODY " & MessageID.ToStr ing & vbCr & vbLf
Write(message)
Dim article As String
strResponse = Response()
If strResponse.Sub string(0, 3) <> "423" Then
If strResponse.Sub string(0, 3) <> "222" Then
Throw New NntpException(s trResponse)
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(arNe ws.Item(Message ID.ToString),
NewsGroupMessag e).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.Sub string(0, 3) <> "340" Then
Throw New NntpException(R esponse)
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.Sub string(0, 3) <> "240" Then
Throw New NntpException(R esponse)
End If
End Sub 'Post
Private Sub Write(ByVal message As String)
Dim en As New System.Text.ASC IIEncoding
Dim WriteBuffer(102 3) As Byte
WriteBuffer = en.GetBytes(mes sage)
Dim stream As NetworkStream = GetStream()
stream.Write(Wr iteBuffer, 0, WriteBuffer.Len gth)
'Debug.WriteLin e("WRITE:" & message)
End Sub 'Write
End Class
Public Class NewsGroupMessag eCollection
Inherits System.Collecti ons.CollectionB ase
Public Sub Add(ByVal value As NNTP.NewsGroupM essage)
list.Add(value)
End Sub
Public Sub Remove(ByVal Index As Integer)
If Index > Count - 1 Or Index < 0 Then
Throw New Exception("Inva lid Index")
Else
list.RemoveAt(I ndex)
End If
End Sub
Public ReadOnly Property Item(ByVal Index As Integer) As
NNTP.NewsGroupM essage
Get
Return CType(list.Item (Index), NNTP.NewsGroupM essage)
End Get
End Property
End Class
Public Class NntpException
Inherits System.Applicat ionException
Public Sub New(ByVal str As String)
MyBase.New(str)
End Sub 'New
End Class 'NntpException
Ken
------------------
"Jim" <re***@groups.p lease> wrote in message
news:yq******** ***********@big news4.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......mayb e 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!