Posting a message to a newsgroup in Java

There is an undocumented Sun package called sun.net.nntp. Following
example demonstrates how to post a message to alt.test using your newsserver.
Get an instance of NewsGroupInfo by calling getGroup on the NntpClient. If
it returns null, posting is not allowed.
Setup to post a message. It returns a stream to which the article should be written.
The article must have a properly formed RFC850 header and end-of-lines must by
sent as rn. The Article must end with rn.

Beware:
Why Developers Should Not Write Programs That Call ‘sun’ Packages

import sun.net.nntp.*;
import java.io.*;
 
public class Main {
   public static void main(String[] args) throws IOException {
      String server = "news.uni-stuttgart.de";
 
      NntpClient nntp = new NntpClient(server);
      NewsgroupInfo ni = nntp.getGroup("alt.test");
 
      PrintStream p = nntp.startPost();
      if (p != null) { 
         p.println("From: test@esus.com");
         p.println("Newsgroups: alt.test");
         p.println("Subject: Post a msg to newsgroup using Java");
         p.println("");
         p.println("http://www.esus.com");
         p.print("rn");
         if (nntp.finishPost())
            System.out.println("Ok.");
         else
            System.out.println("Error occurred posting msg.");
      }
      else {
         System.out.println("Posting not allowed!");
      }
      nntp.closeServer();
   }
}