我一直在查看示例 JavaMail 发送和几个邮件服务器之间的网络聊天,并发现它会产生一些额外的聊天。代码示例也可以在互联网上找到
package example;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Sender {
public static void main(String[] args) {
Properties props = new Properties();
//props.put("mail.smtp.host", "localhost"); // local james
props.put("mail.smtp.port", "25");
props.put("mail.smtp.from", "misterunsub@localhost");
props.put("mail.smtp.sendpartial", "false");
props.put("mail.smtp.ehlo", "false");
props.put("mail.debug", "true");
Session session = Session.getInstance(props,null);
try {
Message message = new MimeMessage(session);
//message.setFrom(new InternetAddress("misterunsub@localhost"));
message.addHeader("List-Unsubscribe", "<mailto:list-manager@localhost?subject=Unsubscribe>");
message.setSubject("Fancy mail from Unsub");
message.setText("Dear Mail Crawler,"
+ "\n\nNo spam to my email, please!");
message.saveChanges();
InternetAddress[] alice = InternetAddress.parse("alice@localhost");
Transport t = session.getTransport("smtp");
t.connect();
t.sendMessage(message, alice);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
我已经在我的本地主机上设置了 James 来接收流量。(注意 james 也使用 JavaMail 发送/转发邮件,但问题与 JavaMail 有关),James 将发送委托给 JavaMail。因此也表现出问题。但上面的示例足以证明我所看到的。
流量在wireshark中看起来像这样
>> HELO localhost
<< 250 localhost Hello localhost (127.0.0.1 [127.0.0.1])
>> MAIL FROM:<misterunsub@localhost>
<< 250 2.1.0 Sender <misterunsub@localhost> OK
>> RCPT TO:<alice@localhost>
<< 250 2.1.5 Recipient <alice@localhost> OK
>> RSET
<< 250 2.0.0 OK
>> RSET
<< 250 2.0.0 OK
>> MAIL FROM:<misterunsub@localhost>
<< 250 2.1.0 Sender <misterunsub@localhost> OK
>> RCPT TO:<alice@localhost>
<< 250 2.1.5 Recipient <alice@localhost> OK
>> DATA
<< 354 ok send data ending with <CRLF>.<CRLF>
>> this is data
>> .
<< 250 2.6.0 Message recieved
>> QUIT
<< 221 2.0.0 localhost Service closing transmission channel
通信的奇怪之处在于第一个“MAIL FROM:”、“RCPT TO:”、“RSET”和“RSET”,尤其是 RSET RSET,它会导致大量开销。
有人知道如何避免这种情况吗?有人可以确认这种行为吗?
更新 问题似乎与 Windows 上的 vpn 使用或环回/本地主机通信有关。Linux 根本没有这个问题。比尔香农建议使用防病毒或防火墙,很可能就是这样。因为防病毒软件确实注意到了流量。
感谢比尔的及时回复。