I am trying to send a mail from Solaris 5.9 using JAVA code.but when I am trying to do the same.then I am not able to read a line(data) from buffer..I am only able to connect to SMTP server but not able to get the data from buffer.
when I am trying to read the line --
I am trying to read a line but unable to read it--
Use an existing, well-written library like JavaMail or Apache's Commons Mail.
When posting code, consider the following:
- limiting the posted code to the bits actually used (several methods posted are never used)
- use normal Java naming conventions (classes, not local variables, begin with capital letters)
- post only code that will compile (this won't)
- remove superfluous code (what's the purpose of the first several lines of sendEmail()? It just obfuscates any actual issues.)
Joined: Apr 28, 2009
I am sorry about that.I will take care of it from next time onwards.
Yes,you are right and I can use Javamail or comm api but I want to know what is going wrong in this code so that I am not able to read the data and not able to send mail.
Do not use readline() on a stream attached to a socket. Different platforms use different bit patterns for marking the end of a line, using it in network programming can cause all sorts of behaviors, usually causes it to block indefinitely because it is expecting a specific bit pattern to know when it is done. I stopped looking at your code once I saw those readlines, you may have other issues, but this is the big pointy one.
Quite often in network programming what you are writing will interact with a variety of operating systems and servers/clients written in many different programming languages. This requires special care. I recommend going over the Network and IO trails here. Another good resource is Java Network Programming by Elliot Harold, it talks about sockets, IO, threads and other critical topics in detail.
I disagree with the idea to use JavaMail. Nothing teaches you how email works(or whatever else you are wanting to learn) if you do it yourself. It makes you a self-reliant programmer. Of course, I wouldn't advise rolling you own for production use unless you really know what you are doing. Same goes for any protocol, data structure, etc.
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Rusty Shackleford wrote:I disagree with the idea to use JavaMail. Nothing teaches you how email works(or whatever else you are wanting to learn) if you do it yourself. It makes you a self-reliant programmer. Of course, I wouldn't advise rolling you own for production use unless you really know what you are doing. Same goes for any protocol, data structure, etc.
It depends on what your goal is. If you are writing a program just for educational purposes, then I wouldn't have a problem with trying to implement the low-level e-mail protocol yourself. If this is a program for a project for work, then it's silly to implement it yourself when there are well-tested libraries available - trying to implement it yourself in that case will cost you more time and your own implementation will most likely be inferior to that of a well-known and well-tested library.
Joined: Jan 03, 2006
Perhaps before posters recommend an existing library, they find out what the goal is. I see this all too often when people ask for help when implementing protocols or data structures. Learning how to use an existing Collection class isn't nearly as useful as learning how it really works. Anyone can make an API call.
It isn't always the case that "existing and 'well' tested libraries" are better. For example there are cases where writing your own data structures is better then using what is in Collections. But I agree that generally using existing solutions is better and said exactly that in my first post.