{"id":13479,"date":"2014-05-14T14:45:51","date_gmt":"2014-05-14T09:15:51","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=13479"},"modified":"2016-12-19T14:54:40","modified_gmt":"2016-12-19T09:24:40","slug":"receive-email-using-subethasmtp-the-local-smtp-server","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/receive-email-using-subethasmtp-the-local-smtp-server\/","title":{"rendered":"Receive Email using SubEthaSMTP (The local SMTP server)."},"content":{"rendered":"<blockquote><p>In my Grails app, I came across a requirement of setting up an SMTP server for receiving mails and extracting their content. After exploring a lot I found SubEthaSMTP to be the most suitable. <a href=\"http:\/\/code.google.com\/p\/subethasmtp\/\">SubEthaSMTP<\/a> lets your application receive an SMTP mail very easily. <\/p><\/blockquote>\n<p style=\"margin-bottom: 10px\">To setup an SMTP server in your application, You need to follow the steps as follows:<\/p>\n<p style=\"margin-bottom: 10px\">1) Add dependency for the latest (at the time of writing it was 3.1.7) SubEthaSMTP library in the BuildConfig.groovy file.<\/p>\n<p>[shell]dependencies {<br \/>\n        runtime &quot;org.subethamail:subethasmtp:3.1.7&quot;<br \/>\n}<br \/>\n[\/shell]<\/p>\n<p style=\"margin-bottom: 10px\">2) After adding the library you need to implement two Interfaces, namely MessageHandlerFactory and MessageHandler.<\/p>\n<p>The MessageHandlerFactory is instantiated for every message to be exchanged in an SMTP conversation which (MessageHandlerFactory) in turn instantiates a new instance of MessageHandler.<\/p>\n<p>You can implement the <strong>MessageHandlerFactory<\/strong> as follows:<\/p>\n<p>[java]<br \/>\nclass MessageHandlerFactoryImpl implements MessageHandlerFactory {<\/p>\n<p> \t@Override<br \/>\n \tMessageHandler create(MessageContext ctx) {<br \/>\n        \treturn new MessageHandlerImpl(ctx)<br \/>\n    \t}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p style=\"margin-bottom: 10px\">And you can implement the <strong>MessageHandler<\/strong> as follows:<\/p>\n<p>[java]<br \/>\n@Log4j<br \/>\nclass MessageHandlerImpl implements MessageHandler {<br \/>\n    MessageContext context<\/p>\n<p>    MessageHandlerImpl() {}<\/p>\n<p>    MessageHandlerImpl(MessageContext context) {<br \/>\n        this.context = context<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    void from(String from) {<br \/>\n        log.info &quot;FROM: ${from}&quot;<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    void recipient(String recipient) {<br \/>\n        log.info &quot;RECIPIENT: ${recipient}&quot;<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    void data(InputStream data) {<br \/>\n        log.info &quot;DATA&quot;<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    void done() {<br \/>\n        log.info &quot;DONE&quot;<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>The <em>from()<\/em>, <em>recipient()<\/em> and <em>data()<\/em> methods will be called during each stage of SMTP. Multiple MessageHandler are created if multiple messages are delivered with a single SMTP session.<\/p>\n<p style=\"margin-bottom: 10px\">3) Run the SmtpServer on your app&#8217;s start up.<\/p>\n<p>[java]<br \/>\n SMTPServer smtpServer  = new SMTPServer(new MessageHandlerFactoryImpl())<\/p>\n<p> smtpServer.hostName=&lt;your hostname&gt;<br \/>\n smtpServer.port =&lt;some port&gt;<br \/>\n smtpServer.start()<br \/>\n[\/java]\n<\/ul>\n<p style=\"margin-bottom: 10px\">After getting your SmtpServer instance running the next thing you need to do is to send email to your app. For this you can use <strong>Telnet<\/strong> program or you can configure a messenger like <strong>Thunderbird<\/strong> or <strong>Kmail<\/strong> to send mail on locally running SMTP server which is <strong>localhost<\/strong>.<\/p>\n<p>The Telnet program can be used to send emails, as shown below (Note: My SMTP server is running on port 25000):<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/05\/Telnet1.png\"><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/05\/Telnet1.png\" alt=\"\" width=\"600\" class=\"alignnone size-full\" \/><\/p>\n<p style=\"margin-top: 10px\">\n<p>Voilla! it worked.<\/p>\n<p>That&#8217;s all you need to run local SMTP server in your app!<\/p>\n<p>The next milestone is to read content of mail<\/p>\n<p>Most of the email client send a multipart message. A <strong>MultipartMessage<\/strong> is a simple <strong>Message<\/strong> object where the <em>Content-Type<\/em> is set to <em>multipart<\/em> and the content body carries a reference to a Multipart object.<\/p>\n<p>\n You can easily retrieve content from multipart content-typed message using MimeMessage instance. For this you need to include <a href=\"http:\/\/mvnrepository.com\/artifact\/com.sun.mail\/dsn\/1.4.5\">JavaMail API Dsn<\/a> library. You can extract body part from multipart message with MultipartReport instance. You can do this as follows:\n<\/p>\n<p>[java]<br \/>\nSession session = Session.getDefaultInstance(new Properties())<\/p>\n<p>MimeMessage message = new MimeMessage(session, data)<\/p>\n<p>if (obj instanceof MimeMultipart) {<br \/>\n    MultipartReport multipartReport = new MultipartReport(message.dataHandler.dataSource);<br \/>\n    def parsedText = multipartReport.textBodyPart.inputStream<br \/>\n    log.info &quot;Body content : ${parsedText}&quot;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>So guys, this is how you can implement your own locally running SMTP server with very less effort!<\/p>\n<blockquote>\n<p>Akash Sethi<br \/>\nSoftware Engineer<br \/>\nIntelliGrape Software Pvt. Ltd.<br \/>\nakash@intelligrape.com<br \/>\nMy LinkedIn<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>In my Grails app, I came across a requirement of setting up an SMTP server for receiving mails and extracting their content. After exploring a lot I found SubEthaSMTP to be the most suitable. SubEthaSMTP lets your application receive an SMTP mail very easily. To setup an SMTP server in your application, You need to [&hellip;]<\/p>\n","protected":false},"author":67,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":97},"categories":[7],"tags":[1426,4840,4844,1423,1424,1434,1433],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/13479"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/67"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=13479"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/13479\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=13479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=13479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=13479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}