无法解析标头以创建MIME消息

时间:2013-02-24 11:13:38

标签: java apache api email javax.mail

我正在尝试仅基于Apache NNTP API向我自己发送消息,而不是发送到或通过intertubes。涉及的特定类采用NNTP message并尝试将其解析为常规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 - &gt; MIME消息我当然乐于接受建议。我已经放弃了新闻组标题,因为我不明白的原因似乎有问题。基本上,我只是无法弄清楚如何解析这些标题,以便他们创建一个有效的消息发送。

1 个答案:

答案 0 :(得分:1)

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

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

有多个usenet新闻可用于电子邮件网关;你可能想看看使用其中一个而不是自己动手。