【问题标题】:subethasmtp server not printing messages from clientsubethasmtp 服务器不打印来自客户端的消息
【发布时间】:2015-02-25 03:53:42
【问题描述】:

当我运行客户端时,它应该向我的服务器发送一封电子邮件,然后我希望我的电子邮件服务器将电子邮件详细信息(收件人、发件人、端口、消息)打印到控制台。出于某种原因,在运行客户端后,服务器上没有任何明显的变化。

服务器

package example;

import org.subethamail.smtp.server.SMTPServer;

public class EmailServer {

    public static void main(String[] args) {
        MyMessageHandlerFactory myFactory = new MyMessageHandlerFactory();
        SMTPServer smtpServer = new SMTPServer(myFactory);
        smtpServer.setPort(25000);
        smtpServer.start();
    }
}

服务器输出

运行:[main] INFO org.subethamail.smtp.server.SMTPServer - SMTP 服务器 *:25000 开始 [org.subethamail.smtp.server.ServerThread *:25000] 信息 org.subethamail.smtp.server.ServerThread - SMTP 服务器 *:25000 开始

客户

package example;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.subethamail.smtp.client.*;

public class EmailClient {

    public static void main(String[] args) {
        try {
            SMTPClient sc = new SMTPClient();
            sc.close();
            sc.connect("localhost", 25000);
            sc.sendReceive("test");
        } catch (IOException ex) {
            Logger.getLogger(EmailClient.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

客户端输出

运行:构建成功(总时间:0 秒)

版本是 3.1.7 来自https://code.google.com/p/subethasmtp/downloads/list

服务器需要 MyMessageHandlerFactory,我复制自:https://code.google.com/p/subethasmtp/wiki/SimpleExample

【问题讨论】:

    标签: java email smtp jakarta-mail smtpclient


    【解决方案1】:

    好的,让我们检查一下源代码(总是一个好主意),看看会发生什么。

    你发送“测试”通过

    SMTPClient sc;
    sc.sendReceive("test"); // which is actually sent to your SMTPServer as "test\r\n"
    

    现在,考虑到这是一个新的 SMTP 会话(请参阅 RFC5321 了解您一直想知道但又害怕询问此类事情的所有内容)并且“test”不是有效的命令 VERB 在对话的这一点上,您会看到sendReceive() 返回的错误。

    但是由于您忽略了应该返回的SMTPClient.Response#75

    Response resp=SMTPClient.sendReceive()
    

    你错过了两个

    • resp.code(我确定是 500 - Permanent Negative Completion reply / Syntax - 请参阅上面的 RFC)和
    • resp.message 描述您的命令无法执行的原因

    这两个都是从CommandHandler#93返回的。

    【讨论】:

    • 哦,关于您的IOException try/catch,事务不会失败。相反,如果您选择处理它,它会尽职尽责地返回足够的信息来对您的问题进行分类。 :-)
    • 所以,我是这个库的新手,以前从未接触过它。为了让我的服务器打印出来自客户端的电子邮件,您能告诉我需要进行哪些基本的代码级更改吗?
    • 刚刚跟进,您有没有机会查看我之前的评论?
    • @ThreaT - 您需要使用发送消息所需的正确 SMTP 命令调用您的 sendReceive,并且在每个步骤中,从 sc.sendReceive 捕获返回的结果以确保您的命令已被接受.对于典型的 SMTP 会话,您需要按顺序发送“HELO”、“MAIL FROM”、“RCPT TO”和“DATA”命令。请参阅earthinfo.org/example-smtp-conversation 了解更多信息。
    • 据我所知,sendReceive(String msg) 只有这一种方法,在其他任何地方都不会超载。您能否举例说明我如何更改客户端以将正确的数据以代码格式发送到服务器?
    猜你喜欢
    • 1970-01-01
    • 2017-06-18
    • 2015-09-09
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    • 2017-01-09
    相关资源
    最近更新 更多