commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thufir Hawat <hawat.thu...@gmail.com>
Subject massage the message into a Message (NNTP->MIME)
Date Mon, 25 Feb 2013 10:05:34 GMT
On Sun, 24 Feb 2013 23:06:39 +0100, Thomas Neidhart wrote:


> btw. there exists the commons-email component to send mime messages in a
> more convenient way.

While it does help, it still seems to bring up the same type of 
MessagingException and merely moves the problem around:

compile:
run:
200 Leafnode NNTP Daemon, version 1.11.8 running at localhost (my fqdn: 
dur.bounceme.net)
GROUP comp.lang.java.help
211 47 3 49 comp.lang.java.help group selected
HEAD 3
221 3 <7e60dce5-09d7-4cee-bbc1-137207f03dd0@googlegroups.com> article 
retrieved - head follows
BODY 3
222 3 <7e60dce5-09d7-4cee-bbc1-137207f03dd0@googlegroups.com> article 
retrieved - body follows
Exception in thread "main" javax.mail.MessagingException: 
comp.lang.java.help is not an InternetAddress
	at com.sun.mail.smtp.SMTPTransport.sendMessage
(SMTPTransport.java:1085)
	at net.bounceme.dur.nntp.ArticleReader.<init>
(ArticleReader.java:44)
	at net.bounceme.dur.nntp.Driver.<init>(Driver.java:13)
	at net.bounceme.dur.nntp.Driver.main(Driver.java:17)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)


The problem being that the String representation of headers returned by 
String headers = read(client.retrieveArticleHeader(i));
seems fundamentally incompatible with any sort of javax.mail.Message, or 
anything related.  there must be a way to turn that String of headers 
into legitimate headers for a Message, but how?

I'm quite sure that it's possible to massage the message, if you'll 
forgive me for abusing that quote, into a Message of one variety or 
another.  The question is, how?  My ArticleReader.java to follow:

package net.bounceme.dur.nntp;

import java.io.BufferedReader;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.logging.Logger;
import javax.mail.Message.RecipientType;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import org.apache.commons.mail.util.MimeMessageUtils;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.nntp.NNTPClient;
import org.apache.commons.net.nntp.NewsgroupInfo;

public final class ArticleReader {

    private final static Logger LOG = Logger.getLogger
(ArticleReader.class.getName());

    public ArticleReader(Properties p) throws Exception {
        NNTPClient client = new NNTPClient();
        client.addProtocolCommandListener(new PrintCommandListener(new 
PrintWriter(System.out), true));
        client.connect(p.getProperty("host"));
        NewsgroupInfo newsgroupInfo = new NewsgroupInfo();
        client.selectNewsgroup(p.getProperty("newsgroup"), newsgroupInfo);

        Session session = Session.getDefaultInstance(p);
        String protocol = p.getProperty("protocol");
        String host = p.getProperty("host");
        int port = Integer.valueOf(p.getProperty("port"));
        String username = p.getProperty("username");
        String password = p.getProperty("password");
        Transport transport = session.getTransport(protocol);
        transport.connect(host, port, username, password);

        for (long i = newsgroupInfo.getFirstArticleLong(); i < 
newsgroupInfo.getLastArticleLong(); i++) {
            String headers = read(client.retrieveArticleHeader(i));
            String body = read(client.retrieveArticleBody(i));

            String sb = headers + body;
            MimeMessage message = MimeMessageUtils.createMimeMessage
(session, sb);
            message.setRecipients(RecipientType.TO, p.getProperty
("recipient"));

            transport.sendMessage(message, message.getAllRecipients());
        }
    }

    public String read(BufferedReader br) throws Exception {
        StringBuilder lines = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            lines.append(line).append("\n");
        }
        br.close();
        return new String(lines);
    }
}



Any and all suggestions appreciated.


thanks,

Thufir


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message