Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SMTP java program...

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Joe Ess
Bartender
Pie
Posts: 9265
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9265
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9265
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9265
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9265
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All your commands end with a particular string except one. . .
[ January 31, 2008: Message edited by: Joe Ess ]
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic