Well I checked for infinite loop. I did a debug. I found that the code is exiting somewhere in the middle of its execution. Actually i have a String variable, which grows unusually long as the application goes on! It stores the complete HTML code that is going to be rendered in the mail. Could that be a problem?
Infinite loops are rarely the cause of OOMEs, unless the loop is allocating objects and adding them to a collection. Most often, it's simply that your program is using too much memory. It's unlikely that it's the HTML, unless you've got multiple megabytes of it. More likely it's something else.
First, it may be that the program simply needs more memory. Add "-Xmx256m" to the vm arguments in the Eclipse run configuration. If it works, then your program just needed more than the default sized heap. If it still doesn't run, then it's probably something that's going out of control. First try reading the code, or better yet, getting someone else to do it, and look for excess memory usage. If that it's enough, then you can use a memory profiler (YourKit, JProbe, etc) to find out where the memory is going.
The 256MB memory allocation didn't help! I went through the code. I can post the code here. But the thing is it has 5 classes two of them are big!
Looks like I created a MONSTER!!!
Little explanation that i can provide is that, I am adding string values to variable which stores the whole text of an html page starting from "<html>" to "</html>". But when i debugged the code i see in a loop when the values are getting added to the string, the code suddenly comes back to the main class to finally block and exits and throws the exception. Well seeing from that it looks like the string size is becoming bigger than it can handle!