Showing posts with label email. Show all posts
Showing posts with label email. Show all posts

Friday, 12 August 2011

SubEthaSMTP: Store email as files before sending out

I have written a simple handler for SubEthaSMTP that will save emails to specific folders before sending them out. First the main function to setup stuff (SMTPServer.java)

You can download it from here. (Note: There are three source class in there, with their Class Name properly highlighted).

What it does is to start an SMTP server (standard SMTP Port) in the workstation/server you are running it on.

You will need to configure SMTPMessageHandlerFactory to set the appropriate smtp host, smtp port, smtp user and smtp password to send the email to.

Whenever a mail is received, it will write the contents of the mail in folder: outbox/{fromemailaddr}/{date}/{time}

It will work for normal mail (HTML/plain) and mail with attachments, but not message/rfc822 type.

Also, because I am not able to read BCC headers from the mail received via SMTP, instead of processing the TO, CC, and BCC in the mail content, I will send out the mail to everyone separately, putting them in the TO field for each email sent.

Please help to test out and comment accordingly. Thanks.

Sunday, 24 July 2011

Apache Camel: Email notification when files are placed in FTP folder

I love getting automated notification when events occur rather than having to perform manual checking. As an example is, instead of having to login to FTP and check whether files are uploaded to a particular FTP folder, it will indeed be much more elegant if an email notification can be sent to me automatically to inform me about that fact.


For this article, you can use the Apache Camel project created from my previous blog article. Also, let's enable Apache Camel logging so we can see debug output for ease of understanding what's going on. Verify if you already have log4j.properties in your project (should be in Other Sources -> src/main/resources -> <default package> folder under your project. If it is there, you might want to skip to the FTP and Email section.
To do so, first, ensure that you add dependency for slf4j-log4j12 for the appropriate version of slf4j you have (mine is 1.6.1). You can follow the instructions in my previous article to add the dependency (if you are like me, using NetBeans 7 and Maven).


Next you need to click on the Files tab in your NetBeans project, go to your project and expand to src\main, right-click and select New -> Folder. Create a folder named 'resources' (without quotes, of course).
After that, right-click on the resources folder you have just created, and select New -> Properties File. When the New Properties File dialog appear, enter log4j as the filename. The Folder should be src\main\resources (if you indeed right-clicked on the resources folder). As per screenshot below:




Enter following info into your log4j.properties file:

log4j.rootLogger=TRACE, out

log4j.logger.org.apache.camel=DEBUG

log4j.appender.out=org.apache.log4j.ConsoleAppender
log4j.appender.out.layout=org.apache.log4j.PatternLayout
log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
The file should be accessible from Projects tab in the following navigation under your Project folder: Other Sources -> src/main/resources -> <default package>


Next, add dependencies for both camel-mail and camel-ftp.When done, amend your routing code to:



        context.addRoutes(new RouteBuilder() {
            public void configure() {
                from("ftp://ftp.myftp.com/test?username=myuser&password=mypass&noop=true")
                    .setBody().simple("Hi, new file uploaded to ftp. Name: ${header.CamelFileName}")
                    .setHeader("subject").simple("Received filename: ${header.CamelFileName}")
                    .to("smtps://smtp.gmail.com?username=development.rants@gmail.com&password=******");
            }
        });

The above example (fictitious account info), will check the test folder in ftp.myftp.com for user: myuser. When found, it will send an email to my gmail account development.rants@gmail.com. The sender is of the same email account. An email will be sent for every file found in the ftp account, so for testing purposes, please ensure that you do not have too many files in this folder.


You may also want to modify the call to Thread.sleep in App.java to a longer time, e.g. 60 seconds, as below:


        Thread.sleep(60000);


Next, build and run the project, and voila! You will start receiving emails when files are uploaded into the ftp folder being watched.


For this article, notice that I am using noop=true parameter for the ftp url. That will means the file will be left in that folder as it is. This is useful if you are running the program a few times and wants to be able to test without needing to re-upload files to the ftp folder.


For more options you may want to refer to the Camel FTP component documentation here.


Enjoy!