SubethaSmtp 是否将电子邮件存储在某处?

问题描述 投票:0回答:2

我使用 SubethaSmtp 库作为电子邮件服务器,到目前为止,我已经能够运行该服务器并通过发送电子邮件来测试它。电子邮件信息打印在输出中。据我所知,smtp协议是用来发送电子邮件的。而IMAP协议用于接收电子邮件。我的问题是,SubethaSmtp 是否将电子邮件存储在某个地方(例如数据库或文件)?一般来说,我是否需要 SubethaSmtp 服务器以外的服务器来接收电子邮件?这两个协议之间有什么关系?

我的代码是用以下两个Java类编写的:

基本SMTP服务器类:

package com.sojoodi;

import org.subethamail.smtp.server.SMTPServer;


public class BasicSMTPServer {
    public static void main(String[] args) {
        MyMessageHandlerFactory myFactory = new MyMessageHandlerFactory();
        SMTPServer smtpServer = new SMTPServer(myFactory);

        smtpServer.setPort(25000);

        smtpServer.start();

        System.out.println("smtpServer = " + smtpServer);
        System.out.println("HostName = " + smtpServer.getHostName());
    }
}

和 MyMessageHandlerFactory 类:

package com.sojoodi;

import org.subethamail.smtp.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MyMessageHandlerFactory implements MessageHandlerFactory {
    public MessageHandler create(MessageContext ctx) {
        return new Handler(ctx);
    }

    class Handler implements MessageHandler {
        MessageContext ctx;

        public Handler(MessageContext ctx) {
            this.ctx = ctx;
        }

        public void from(String from) throws RejectException {
            System.out.println("FROM:"+from);
        }

        public void recipient(String recipient) throws RejectException {
            System.out.println("RECIPIENT:"+recipient);
        }

        public void data(InputStream data) throws IOException {
            System.out.println("MAIL DATA");
            System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
            System.out.println(this.convertStreamToString(data));
            System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
        }

        public void done() {
            System.out.println("Finished");
        }

        public String convertStreamToString(InputStream is) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();

            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return sb.toString();
        }

    }
}
smtp imap
2个回答
0
投票

也许您可以检查使用 SubEtha SMTP 和 Spring Boot 接收电子邮件帖子,对于有关存储的问题,您可以检查相关代码存储库中的问题部分,因为有一个与您相同的问题


0
投票

您应该创建 SMTPServer 实例并将工厂传递给它,例如:

SMTPServer smtp = SMTPServer
    .port(config.getPort())
    .hostName(config.getHostname())
    .messageHandlerFactory(new MyMessageHandlerFactory())
    .build();

smtp.start();
© www.soinside.com 2019 - 2024. All rights reserved.