How do I use the JavaMail API to send newsgroups messages?

John Zukowski

You need to get an NNTP provider, like Knife's. You must manually create an instance of their NNTP Transport object to send the message as well as specify the newsgroup to post to.

The following program does just that.

import javax.mail.*;
import javax.mail.internet.*;

public class PutNewsExample {
  public static void main (String args[]) throws Exception {
    String host = args[0];
    String from = args[1];
    String newsgroup = args[2];
    String username = args[3];
    String password = args[4];

    // Get session
    Session session = Session.getInstance(
      System.getProperties(), null);

    // Define message
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.setSubject("Test Test - Ignore");
    message.setText("Will this work?");
    message.addHeader("Newsgroups", newsgroup);

    // Define transport
    Transport transport = 
      new dog.mail.nntp.NNTPTransport(session, 
        new URLName("news:" + newsgroup));

    transport.connect(host, username, password);
    transport.sendMessage(message, message.getAllRecipients());