Posting newsgroup messages with NNTP using Indy






2.63/5 (10 votes)
Jul 24, 2005
1 min read

45569

252
In this article, I will demonstrate how to quickly and easily post a message to a newsgroup, including an attachment, using the Indy open source library.
Introduction
In this article, I will demonstrate how to quickly and easily post a message to a newsgroup, including an attachment, using the Indy open source library.
The Code
using (NNTP xNNTP = new NNTP()) {
xNNTP.Connect("msnews.microsoft.com");
try {
Message xMsg = new Message();
xMsg.Subject = "Test Message from Indy";
xMsg.From.Address = "null@nowhere.com";
xMsg.Body.Text = "This is a test message.";
xMsg.NewsGroups.Add("microsoft.test");
new AttachmentFile(xMsg.MessageParts,
System.Environment.CurrentDirectory + @"\..\..\App.ico");
xNNTP.Post(xMsg);
Console.WriteLine("Message posted");
} finally {
xNNTP.Disconnect();
}
}
Running the Demo
The demo simply posts a test message to a test newsgroup on the Microsoft news server. To change it to your news server and newsgroup, simply change the parameters in the demo.
Output
The demo has very little output unless there is an error. To see the results, you should look at the microsoft.test newsgroup using your newsreader. Note that messages may not appear instantly and on the Microsoft server it sometimes takes a few minutes for the message to be visible publicly.
Why Console?
The demo code is a console application. For simple code snippets, I prefer console applications as they are easier to write and to focus on code without the demo being over-shadowed by the interface code. All of the code demonstrated here can of course be used in a WinForms, WebForms, Web services, or other types of applications.
What is Indy?
This demo uses classes from the open source Indy.Sockets library. Indy.Sockets is an open source socket library that supports clients, servers, TCP, UDP, raw sockets, as well as over 100 higher level protocols such as SMTP, POP3, NNTP, HTTP, and many more. Indy.Sockets is available for C#, C++, Delphi, and Visual Basic. NET. Indy runs on Windows, Linux, Microsoft .NET, and Mono.
The downloadable demo includes the Indy assembly so it is ready to run.