Two Laptop Bag*
The moose likes Java in General and the fly likes SMTP server does not deliver mail to user's mailbox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "SMTP server does not deliver mail to user Watch "SMTP server does not deliver mail to user New topic
Author

SMTP server does not deliver mail to user's mailbox

M M Islam Chisty
Greenhorn

Joined: Aug 31, 2003
Posts: 17
Hi,

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, my developed programm is treating like SMTP Server (2). But I want it to behave like SMTP Server (1). 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
 
Don't get me started about those stupid light bulbs.
 
subject: SMTP server does not deliver mail to user's mailbox
 
Similar Threads
SMTP not working perfectly
Singleton/Static in Multi threading scenario
JavaMail will not let me to authenticate myself
Error while running a simple java mail application
how to mail send in servlets or jsp?