0

Can you please tell me how to use SubethaSmtp library? I just want to retrieve the mails from my Gmail inbox and display them or one of them in console window.

I studied most of the API doc but I'm not being able to put the pieces together to get the things working.

Can you please tell me about a working example?

I wrote this code to build a grails application. You may find some bad code habits but it's okey for a sample application.

Here is the code in src/groovy folder :

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}"
        }
    }
}

And here is my controller code.

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()
    }


}

Thanks.

Andy
  • 49,085
  • 60
  • 166
  • 233
Manvendra SK
  • 802
  • 1
  • 10
  • 20
  • 1
    Google.com: subethasmtp example – Nico Apr 01 '14 at 06:35
  • Yes I searched Google a lot. The simpleexample that comes with library is not very useful. Can you please specifically tell me which example or doc? – Manvendra SK Apr 01 '14 at 06:43
  • Nope. What have you tried so far? I'm not wasting my time doing your work if you can't even put in any effort. And when I say 'effort' I mean actual code, even if it is wrong. – Nico Apr 01 '14 at 06:47
  • :) Actually I did. Just didn't paste the code here. Okey just let me push the code to a public git repository, and I'll provide the link here. Please wait. Thanks to show your interest. – Manvendra SK Apr 01 '14 at 06:52
  • Posting links to code is not allowed: http://meta.stackexchange.com/questions/125997/something-in-my-web-site-or-project-doesnt-work-can-i-just-paste-a-link-to-it – Nico Apr 01 '14 at 06:53
  • Hey Nicolás, I've updated the question with the code. Now can you please help me? – Manvendra SK Apr 01 '14 at 07:08

1 Answers1

5

Okey... I found the answer... The code is well written and is working fine. I just didn't know how to send messages to listening smtp server on the port. I just used telnet program and sent emails to the smtp server running on localhost. Now I will create DNS mapping to make it work on the Internet.

Thanks Nicolás for showing your interest.

Manvendra SK
  • 802
  • 1
  • 10
  • 20