aspose file tools*
The moose likes Java in General and the fly likes Not able to send a mail using JAVA code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Not able to send a mail using JAVA code" Watch "Not able to send a mail using JAVA code" New topic
Author

Not able to send a mail using JAVA code

smart gupta
Greenhorn

Joined: Apr 28, 2009
Posts: 4
Hi All,

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

LineIn = In.readLine();
System.out.println("LineIn="+LineIn);





Please help me to resolve it.

Thanks in advance...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Hello "smart gupta", welcome to JavaRanch.

Please check your private messages (click "My Private Messages" at the top right of the page) for an administrative matter from JavaRanch.

The standard API for sending e-mail from Java is JavaMail.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.)
smart gupta
Greenhorn

Joined: Apr 28, 2009
Posts: 4
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.

Please help me to understand it.

Thanks in advance ...
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

I would suggest you take Jesper's mail seriously. His recommendations are not optional. It is expected you comply to the recommendations before you post again.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

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.
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to send a mail using JAVA code
 
Similar Threads
Email servlet
SHA1 digest error for javax/mail/MessagingException
Transport.send problem
Email from Servlet??
SMTP java program...