aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes SMTP java program...  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "SMTP java program...  " Watch "SMTP java program...  " New topic
Author

SMTP java program...

James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Hi all!

I wrote the below code as a homework exercise. I am to create a Java program that interacts with an SMTP server to send e-mail messages. I've gotten it to work, up to the RCPT TO: <email@addresshere.com> portion. The program will parrot the command to me, but I can't get the server to respond; it does not print the 'destination OK' message on the console, as it should (the String response = br.readline(), then System.out.println(response) should parrot back the server response). Can anyone point me in the right direction? I'm almost there, just have to get past this sticking point. Thanks in advance!

-Patrick


[ January 31, 2008: Message edited by: Patrick Brooks ]

Hello. My name is Inigo Montoya. You killed my father. Prepare to die.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8866
    
    8

Using BufferedReader with sockets is problematic. Different operating systems have different end-of-line characters. If you are on a Windows computer talking to a Unix computer, your program will wait forever for the Windows end-of-line "/n/r" when all the Unix computer will send is "/n".
For more information, see: Don't println to a socket


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Hi Joe. I tried using /r/n on all of the commands (forgot to put it in all of them), but with the same result... the program works fine up to the rcpt to: point, then deadlocks. I'm using 'write' instead of 'println', though, just thought I'd point that out, since the article spoke of 'println'. New, (slightly) modified code:

Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8866
    
    8

Originally posted by Patrick Brooks:
[QB]Hi Joe. I tried using /r/n on all of the commands (forgot to put it in all of them), but with the same result... the program works fine up to the rcpt to: point, then deadlocks.


So you are actually getting a response from HELO and FROM? I should read more carefully.
Check your HELO command.
There's also a line where you print one thing to the screen, but send something else to the socket. That can't be good!

Originally posted by Patrick Brooks:
[QB]
I'm using 'write' instead of 'println', though, just thought I'd point that out, since the article spoke of 'println'.

The problem is reversed in your case. You problem is reading from a socket and the example is given as if the problem were with writing. The same principal applies. Both BufferedReader and PrintWriter are poor choices for cross-platform communication because the use platform-specific EOL characters.
[ January 30, 2008: Message edited by: Joe Ess ]
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Originally posted by Joe Ess:

The problem is reversed in your case. You problem is reading from a socket and the example is given as if the problem were with writing. The same principal applies. Both BufferedReader and PrintWriter are poor choices for cross-platform communication because the use platform-specific EOL characters.

[ January 30, 2008: Message edited by: Joe Ess ]


Hmmm, OK, what is a good substitute for BufferedReader, then? Sorry, I'm new to network programming. I caught the HELO error and the command error you pointed out, thanks, but still having the same problem.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41511
    
  53
I think you can still use BufferedReader, just not its readLine method. Of course, handling complete lines is one of the main attraction of Readers, so you may want to work with InputStream directly instead.


Ping & DNS - my free Android networking tools app
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Originally posted by Ulf Dittmer:
I think you can still use BufferedReader, just not its readLine method. Of course, handling complete lines is one of the main attraction of Readers, so you may want to work with InputStream directly instead.


So, do you think that the BufferedReader is what is causing my problem? Strange that it works fine for the 'mail from' portion, but not for the 'rcpt to' portion. I tried with /r/n, with /n, and with just /r, but to no avail. Thanks!

-Patrick
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8866
    
    8

Originally posted by Patrick Brooks:

So, do you think that the BufferedReader is what is causing my problem?


No. I'll say it again:

Check your HELO command.
There's also a line where you print one thing to the screen, but send something else to the socket. That can't be good!
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Well, I didn't post my fully revised code... Code below, I did allow for a user-inputted HELO domain, and I did fix the System.out.println and os.write inconsistency, but still having the same problem, at rcpt to:. I get a deadlock, then it eventually times out.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8866
    
    8

Your code above with the changes I stated worked for me. Let's see your current code.
Just a nitpick, deadlock refers to a specific situation, usually between two or more threads or processes. I/O blocks.
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Originally posted by Joe Ess:
Your code above with the changes I stated worked for me. Let's see your current code.
Just a nitpick, deadlock refers to a specific situation, usually between two or more threads or processes. I/O blocks.


Oops, I forgot to post it... here it is:

Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8866
    
    8

All your commands end with a particular string except one. . .
[ January 31, 2008: Message edited by: Joe Ess ]
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Yeah, that seems to have done it. I left out /r/n earlier because it was causing a 503 error, but it seems to work now, don't know why the change. Anyways, thanks for the help!

-Patrick
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: SMTP java program...