SubethaSmtp工作示例

时间:2014-04-01 06:34:13

标签: java email gmail inbox

您能告诉我如何使用SubethaSmtp库吗?我只想从Gmail收件箱中检索邮件并在控制台窗口中显示它们或其中一个。

我研究了大部分API文档,但是我无法将各个部分放在一起以使事情正常运行。

你能告诉我一个有效的例子吗?

我编写了这段代码来构建一个grails应用程序。您可能会发现一些不良的代码习惯,但它对于示例应用程序来说很有用。

以下是src / groovy文件夹中的代码:

class MessageHandlerFactoryImpl implements MessageHandlerFactory {

    @Override
    MessageHandler create(MessageContext ctx) {
        return new MessageHandlerImpl(ctx)
    }
}

class MessageHandlerImpl implements MessageHandler {

    MessageContext context

    MessageHandlerImpl(MessageContext context) {
        this.context = context
    }

    @Override
    void from(String from) {
        println "FROM: ${from}"
    }

    @Override
    void recipient(String recipient) {
        println "RECIPIENT: ${recipient}"

    }

    @Override
    void data(InputStream data) {
        println "DATA"
        println "-------------------"

        BufferedReader reader = new BufferedReader(new InputStreamReader(data))
        StringBuilder builder = new StringBuilder()
        String line
        while ((line = reader.readLine()) != null) {
            builder.append(line + "\n")
        }
        println builder.toString()
    }

    @Override
    void done() {
        println "DONE"

    }
}

class SimpleMessageListenerImpl implements SimpleMessageListener {
    @Override
    boolean accept(String from, String recipient) {
        println "accept: ${from} \n>> ${recipient}"
        return false
    }

    @Override
    void deliver(String from, String recipient, InputStream data) {
        try {
            println "deliver: ${from} \n>> ${recipient} \n>>> ${data.read()}"
        } catch (TooMuchDataException e) {
            println "TooMuchDataException: ${e.message}"
        } catch (IOException e) {
            println "IOException: ${e.message}"
        }
    }
}

class UsernamePasswordValidatorImpl implements UsernamePasswordValidator {
    @Override
    void login(String username, String password) {
        try {

            println "LOGIN:::::::"
        }   catch(LoginFailedException e) {
            println "LoginFailedException: ${e.message}"
        }
    }
}

这是我的控制器代码。

class SubethaController {

    SMTPServer server
    def index() {

        MessageHandlerFactoryImpl factory = new MessageHandlerFactoryImpl()
        server = new SMTPServer(factory)
        server.hostName = "imap.gmail.com"
        server.port = 993
        server.authenticationHandlerFactory = new EasyAuthenticationHandlerFactory(new UsernamePasswordValidatorImpl())
        server.start()
    }

    def stop() {
        server?.stop()
    }

    Wiser wiser
    def wiser() {
        server = new SMTPServer(new SimpleMessageListenerAdapter(new SimpleMessageListenerImpl()))
        server.start()
        wiser = new Wiser()
        wiser.setPort(25001)
        wiser.start()

        for (WiserMessage message : wiser.getMessages())
        {
            String eSender = message.getEnvelopeSender()
            String eReceiver = message.getEnvelopeReceiver()

            println ">>>>>>>message.getMimeMessage ${message.getMimeMessage()}"
        }
    }
    def wiserS() {
        wiser?.stop()
    }


}

感谢。

1 个答案:

答案 0 :(得分:4)

Okey ......我找到了答案......代码编写得很好,工作正常。我只是不知道如何在端口上向收听 smtp服务器发送消息。我刚刚使用 telnet 程序并将电子邮件发送到 localhost 上运行的 smtp服务器。现在,我将创建 DNS映射,使其在Internet上运行。

感谢Nicolás表达您的兴趣。