gpt4 book ai didi

java - 无法解析 header 来创建 MIME 消息

转载 作者:行者123 更新时间:2023-12-01 14:49:09 25 4
gpt4 key购买 nike

基于Apache NNTP API,我试图只向自己发送消息,而不是通过内管发送消息。 。涉及的特定类采用 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 --> MIME 消息,我当然愿意接受建议。我已经放弃了新闻组标题,因为由于我不明白的原因,这似乎有问题。基本上,我只是不知道如何解析这些 header ,以便它们创建要发送的有效消息。

最佳答案

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

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

有多个可用的新闻组新闻到电子邮件网关;您可能想考虑使用其中之一,而不是自己开发。

关于java - 无法解析 header 来创建 MIME 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15051188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com