0

我试图根据Apache NNTP API仅向自己发送消息,而不是发送到或通过 intertubes 。涉及的特定类接受NNTP 消息并尝试将其解析为常规 MIME 类型消息,如下所示:

package net.bounceme.dur.nntp;

import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MessageSender {

    private final static Logger LOG = Logger.getLogger(MessageSender.class.getName());
    private String header;
    private String body;
    private Properties p;
    private Session session;
    private MimeMessage message;

    private MessageSender() {
    }

    public MessageSender(Properties p, String... s) throws Exception {
        header = s[0];
        body = s[1];
        this.p = p;
        populate();
    }

    private void populate() throws Exception {
        String lines[] = header.split("\\n");
        session = Session.getDefaultInstance(p, null);
        message = new MimeMessage(session);
        LOG.fine("\n\n\n\nnew message************\n\n\n\n");

        for (String s : lines) {
            if (!s.contains("comp.lang.java.help")) {
                message.addHeaderLine(s);
            }
        }
        message.setContent(message, body);
        String recipient = p.getProperty("recipient");
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(recipient));
        try {
            send();
        } catch (javax.mail.internet.ParseException e) {
            LOG.warning(e.toString());
        } catch (com.sun.mail.smtp.SMTPAddressFailedException e) {
            LOG.warning(e.toString());
            List<Address> addresses = Arrays.asList(message.getAllRecipients());
            for (Address a : addresses) {
                LOG.info(a.toString());
            }
        }
    }

    private void send() throws Exception {
        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);
        LOG.log(Level.FINE, "{0}{1}{2}{3}{4}", new Object[]{protocol, host, port, username, password});
        Enumeration enumOfHeaders = message.getAllHeaders();
        while (enumOfHeaders.hasMoreElements()) {
            Header h = (Header) enumOfHeaders.nextElement();
            LOG.log(Level.FINE, "\n\n\nHEADER\n{0}\n{1}", new Object[]{h.getName(), h.getValue()});
        }
        transport.connect(host, port, username, password);
        transport.sendMessage(message, message.getAllRecipients());
    }
}

但是我遇到了标题问题:

init:
Deleting: /home/thufir/NetBeansProjects/apache_nntp/build/built-jar.properties
deps-jar:
Updating property file: /home/thufir/NetBeansProjects/apache_nntp/build/built-jar.properties
Compiling 1 source file to /home/thufir/NetBeansProjects/apache_nntp/build/classes
compile:
run:
200 Leafnode NNTP Daemon, version 1.11.8 running at localhost (my fqdn: dur.bounceme.net)
GROUP comp.lang.java.help
211 35 3 37 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
Feb 24, 2013 3:05:04 AM net.bounceme.dur.nntp.MessageSender populate
WARNING: javax.mail.internet.ParseException: Expected '/', got wrote
HEAD 4
221 4 <kfpdt3$5g9$1@dont-email.me> article retrieved - head follows
BODY 4
222 4 <kfpdt3$5g9$1@dont-email.me> article retrieved - body follows
Exception in thread "main" javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 450 4.1.8 <markspace@nospam.nospam>: Sender address rejected: Domain not found

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1863)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1119)
    at net.bounceme.dur.nntp.MessageSender.send(MessageSender.java:80)
    at net.bounceme.dur.nntp.MessageSender.populate(MessageSender.java:54)
    at net.bounceme.dur.nntp.MessageSender.<init>(MessageSender.java:35)
    at net.bounceme.dur.nntp.ArticleReader.<init>(ArticleReader.java:28)
    at net.bounceme.dur.nntp.Driver.<init>(Driver.java:13)
    at net.bounceme.dur.nntp.Driver.main(Driver.java:17)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 450 4.1.8 <markspace@nospam.nospam>: Sender address rejected: Domain not found

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1730)
    ... 7 more
Java Result: 1
BUILD SUCCESSFUL (total time: 6 seconds)

现在,我很感激发件人没有域。尽管如此,我还是想继续发送消息。我需要更改发件人吗?我想尽可能少地弄脏标题。

如果有更好的方法来转换 NNTP --> MIME Message 我当然愿意接受建议。由于我不明白的原因,我已经删除了新闻组标题,因为这似乎有问题。基本上,我只是不知道如何解析这些标头,以便它们创建要发送的有效消息。

4

1 回答 1

1

是的,您需要修复发件人信息。问题不是(相当)您的代码。您的 MTA 拒绝该电子邮件。Sendmail 通常被配置为不允许出现无效的“发件人”地址。在这种情况下,它告诉您“nospam.nospam”的 DNS 查找失败。

您的信封应该反映类似 news2mail@myhost.org 的发件人,而不是尝试使用 usenet 文章海报的地址。

有多个可用的电子邮件网关新闻网新闻;您可能想考虑使用其中一个而不是自己滚动。

于 2013-03-06T09:24:34.160 回答