aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes SMTP not working perfectly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "SMTP not working perfectly" Watch "SMTP not working perfectly" New topic
Author

SMTP not working perfectly

M M Islam Chisty
Greenhorn

Joined: Aug 31, 2003
Posts: 17
Hello Friends,

I just developed a very simple SMTP server (just a silly attempt of mine.. ha ha) which listens for connections at port 25 but it does not seem to deliver the email to the targeted remote user's mailbox. Consider the following situation:


Client
|
|
|
\|/

SMTP Server(1)
[Accepts mail from Clients and delivers to the actual mail server]
|
|
|
\|/

SMTP Server(2)
[Actual mail server where mails are received, stored, fetched. The DNS server might be?]


Considering the above diagram, it seems to me that my developed programm is treating much like SMTP Server (2). But I want it to behave like SMTP Server (1). (Just check the code). After listening requests from email clients, I just want it to deliver the recived mails to the targeted user's mailbox.

Here is my code (very simple indeed). You can just compile/test it. Can you suggest anything?


The Source Code:
----------------


package com.test.email;

import java.io.*;
import java.net.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.activation.*;

public class SmtpServer
extends Thread {


private static final String MAIL_FROM = "MAIL FROM:";
private static final String RCPT_TO = "RCPT TO:";
private static final String DATA = "DATA";
private static final String OK = "250 OK";
private static final String BANNER = "220 Hello from smtpserver in a class";
private static final String HELO = "HELO";
private static final String QUIT = "QUIT";

private static final String SUBJECT = "Subject";
private static final String END_OF_MESSAGE = ".";
private static final String SEND_TERMINAL_AND_MAILBOX = "SEND";

public static void main(String[] args) throws Exception {
new SmtpServer().start();
}

private ServerSocket serverSocket;

public SmtpServer() {
try {
serverSocket = new ServerSocket(25);
System.out.println("Server started on port 25");
}
catch (IOException e) {
e.printStackTrace();
}
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}

public void run() {
while (true) {
try {
Socket socket = serverSocket.accept();
System.out.println(" #### host name is: " +
socket.getInetAddress().getHostName());
HandlerThread ht = new HandlerThread(socket);
ht.start();
}
catch (IOException e) {
System.err.println(e.toString());
}
}
}

private void jbInit() throws Exception {
}

class HandlerThread
extends Thread {

private Socket socket;

HandlerThread(Socket socket) {
this.socket = socket;
}

public void run() {
try {
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(inputStream));
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);


printWriter.println(BANNER);
boolean sessionCompleted = false;
InternetAddress from = null;
InternetAddress to = null;

String subject = "";
String body = "";
MimeMessage mimeMessage = null;

while (!sessionCompleted) {
String line = bufferedReader.readLine();

if (line.startsWith(MAIL_FROM)) {
String fromString = line.substring(MAIL_FROM.length(), line.length());
from = new InternetAddress(fromString);
printWriter.println(OK);
printWriter.flush();
}

else if (line.startsWith(RCPT_TO)) {
String toString = line.substring(RCPT_TO.length(), line.length());
to = new InternetAddress(toString);
printWriter.println(OK);
printWriter.flush();
}

else if (line.startsWith(SUBJECT)) {
subject = line.substring(SUBJECT.length(), line.length());
System.out.println(" subject line: " + line);
printWriter.println(OK);
printWriter.flush();
}

else if (line.startsWith(DATA)) {
printWriter.println("Code 354 is a reply to the DATA command. \n" +
"After getting this, start sending the body of the mail message, ending with a '\r\n.\r\n.' \n" +
"\n354 Send data now. Terminate with a . on a line alone");
System.out.println("Got a message!");
String temp = "";

printWriter.println(OK);
printWriter.flush();
}

else if (line.startsWith(QUIT)) {
System.out.println(" quit line: " + line);
sessionCompleted = true;
printWriter.println(OK);
printWriter.flush();
}

else {
System.out.println("\t" + line);
printWriter.println(OK);
printWriter.flush();
}
}
}
catch (IOException e) {
e.printStackTrace();
}
catch (AddressException e) {
e.printStackTrace();
}
catch (MessagingException e) {
e.printStackTrace();
}
catch (RuntimeException e) {
e.printStackTrace();
}
}
}

}


With Thanks,
.. Chisty


M. M. Islam Chisty<br />Sr. J2EE Engineer<br />M&H Informatics<br />Ph. 8802-7121293<br />Dhaka, Bangladesh
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Please don't post the same question to multiple forums. I deleted the other copy of the message in "Advanced Java".

Glancing at the program, I see where it receives messages, and then it just ignores what it received. So you want to store them, somehow. "How" depends on what you mean by "the user's mailbox." Would the user get their mail in an mbox formt file (as on many UNIX systems)? Or via POP or IMAP? There's no single standard.

If it's the first, then you just need to Google for "mbox format", learn the file format, and save the messages in the appropriate directory on your system (often /var/spool/mail) under the user's email ID. If it's another UNIX format, then you just have to learn about that other format.

If it's POP or IMAP, then you need to other find or write a server for the appropriate protocol, and have your SMTP server save the data in whatever format that server wants. There are many different possibilities, including simply using a database or other internal format peculiar to your pair of homemade servers.


[Jess in Action][AskingGoodQuestions]
M M Islam Chisty
Greenhorn

Joined: Aug 31, 2003
Posts: 17
Hi,

Here are the replies:

Originally posted by Ernest Friedman-Hill:
Please don't post the same question to multiple forums. I deleted the other copy of the message in "Advanced Java".


Ans. Sorry about that. There are two reasons for this:

1. First of all, I was not sure about which forum to post this issue. I was not certain about whether it sould be categorized as a 'Socket related forum' or, a 'Advanced Java' related forum.

2. I did not get the replies by posting it in 'Advanced Java' forum. That's why I re-posted it in the Sockets forum but forgot to reove it from the 'Advanced Java' forum.


Glancing at the program, I see where it receives messages, and then it just ignores what it received. So you want to store them, somehow. "How" depends on what you mean by "the user's mailbox." Would the user get their mail in an mbox formt file (as on many UNIX systems)? Or via POP or IMAP? There's no single standard.


If it's the first, then you just need to Google for "mbox format", learn the file format, and save the messages in the appropriate directory on your system (often /var/spool/mail) under the user's email ID. If it's another UNIX format, then you just have to learn about that other format.

If it's POP or IMAP, then you need to other find or write a server for the appropriate protocol, and have your SMTP server save the data in whatever format that server wants. There are many different possibilities, including simply using a database or other internal format peculiar to your pair of homemade servers.


Ans. The sample server example (I know it is peculiar as you said... but I don't mind) I provided is just an example. When it runs, it receives requests from a client (say, Outlook or, Eudura) and sends responses to the mail client. But it actually does not deliver the mail to the user menioned in the "TO" field. It is not the mbox format issue... maybe, some DNS related issue needs to be considered.


Any suggestion?

With Thanks,
... Chisty
[ June 05, 2005: Message edited by: M M Islam Chisty ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by M M Islam Chisty:
But it actually does not deliver the mail to the user menioned in the "TO" field. It is not the mbox format issue... maybe, some DNS related issue needs to be considered.


All I see here is code that reads commands from an SMTP client, and sends back "OK" regardless of what it receives. (The server actually doesn't read any message text associated with the DATA command, by the way; it just ignores those in the same way it would ignore invalid commands. These aren't actually valid SMTP responses: there's supposed to be a success or failure response code, and you're not displaying those. For example, here's what a correct MAIL FROM looks like, and a valid server response:

MAIL FROM: ejfried@javaranch.com
250 2.1.0 ejfried@javaranch.com... Sender ok

In any case, this server doesn't do anything except respond "OK" to the client. It just ignores and discards everything it reads. Are you saying you actually believe it should be delivering messages somehow, as is? This has nothing to do with DNS, nor indeed, at this level, with mbox format or any other format. This server just doesn't do anything!

If you want the server to deliver the messages it receives, then it will need to read all the data from the DATA command, save this data and the "from" and "to" information, and then do something with all this information. I suggested saving it to mbox or some other format; another option is to forward it to another SMTP server (i.e., turn around and become a client to the appropriate remote server.) Finding the appropriate remote server is a project in itself.
M M Islam Chisty
Greenhorn

Joined: Aug 31, 2003
Posts: 17
Ok. Thanks. I'll try your suggested way and let you know.



Originally posted by Ernest Friedman-Hill:


All I see here is code that reads commands from an SMTP client, and sends back "OK" regardless of what it receives. (The server actually doesn't read any message text associated with the DATA command, by the way; it just ignores those in the same way it would ignore invalid commands. These aren't actually valid SMTP responses: there's supposed to be a success or failure response code, and you're not displaying those. For example, here's what a correct MAIL FROM looks like, and a valid server response:

MAIL FROM: ejfried@javaranch.com
250 2.1.0 ejfried@javaranch.com... Sender ok

In any case, this server doesn't do anything except respond "OK" to the client. It just ignores and discards everything it reads. Are you saying you actually believe it should be delivering messages somehow, as is? This has nothing to do with DNS, nor indeed, at this level, with mbox format or any other format. This server just doesn't do anything!

If you want the server to deliver the messages it receives, then it will need to read all the data from the DATA command, save this data and the "from" and "to" information, and then do something with all this information. I suggested saving it to mbox or some other format; another option is to forward it to another SMTP server (i.e., turn around and become a client to the appropriate remote server.) Finding the appropriate remote server is a project in itself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SMTP not working perfectly