Last active
February 28, 2018 05:41
-
-
Save Baneeishaque/7ec3973635c867cfc66277b279650d55 to your computer and use it in GitHub Desktop.
SMTP Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.net.*; | |
public class SMTPClient | |
{ | |
private boolean run=true; | |
public static void main(String[] args) | |
{ | |
try | |
{ | |
new SMTPClient().connect(); | |
} | |
catch(Exception e) | |
{ | |
} | |
} | |
public void connect() throws Exception | |
{ | |
Socket s=new Socket("localhost",4000); | |
PrintWriter pw=new PrintWriter(s.getOutputStream(),true); | |
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream())); | |
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); | |
new Thread(new Runnable() | |
{ | |
public void run() | |
{ | |
while(run) | |
{ | |
try | |
{ | |
String msg=br.readLine(); | |
if(msg!=null) | |
System.out.println(msg); | |
else run=false; | |
} | |
catch(Exception e) | |
{ | |
} | |
} | |
} | |
}).start(); | |
String id; | |
boolean val; | |
do | |
{ | |
id=bf.readLine(); | |
String emailreg="^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*(\\.[_A-Za-z]{2,})$"; | |
val=id.matches(emailreg); | |
if(!val)System.out.println("Wrong format email-id.please re-enter."); | |
} | |
while(!val); | |
pw.println(id); | |
System.out.println("Always input \n1-Send Mail\n2-Read mail\n3-Logout"); | |
while(true) | |
{ | |
int n=Integer.parseInt(bf.readLine()); | |
pw.println(n); | |
switch(n) | |
{ | |
case 1:System.out.println("Send To:"); | |
pw.println(bf.readLine()); | |
System.out.println("Message:"); | |
pw.println(bf.readLine()); | |
break; | |
case 2:run=false; | |
System.exit(0); | |
break; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.net.*; | |
import java.util.*; | |
public class SMTPServer | |
{ | |
private ArrayList<user>users=new ArrayList<user>(); | |
public static void main(String[] args) | |
{ | |
try | |
{ | |
new SMTPServer().start(); | |
} | |
catch(Exception e) | |
{ | |
} | |
} | |
public void start() throws Exception | |
{ | |
ServerSocket ss=new ServerSocket(4000); | |
Socket s; | |
while(true) | |
{ | |
s=ss.accept(); | |
System.out.println("Client connected.."); | |
new Thread(new SocketHandler(s)).start(); | |
} | |
} | |
public boolean registerId(String id,SocketHandler sh) | |
{ | |
user n; | |
int i; | |
for(i=0;i<users.size();i++) | |
{ | |
n=users.get(i); | |
if(n.id.equals(id)) | |
{ | |
if(n.sh!=null) | |
return false; | |
n.sh=sh; | |
return true; | |
} | |
} | |
user u=new user(); | |
u.id=id; | |
u.sh=sh; | |
users.add(u); | |
return true; | |
} | |
public void sendMessage(String id,String to,String mbody) | |
{ | |
message msg=new message(); | |
msg.from=id; | |
msg.message=mbody; | |
user n; | |
for(int i=0;i<users.size();i++) | |
{ | |
n=users.get(i); | |
if(n.id.equals(to)) | |
n.msgs.add(msg); | |
} | |
user u=new user(); | |
u.id=to; | |
u.msgs.add(msg); | |
users.add(u); | |
} | |
public ArrayList<message>getMessages(String id) | |
{ | |
for(int i=0;i<users.size();i++) | |
{ | |
user n=users.get(i); | |
if(n.id.equals(id)) | |
return n.msgs; | |
} | |
return null; | |
} | |
public void logout(String id) | |
{ | |
for(int i=0;i<users.size();i++) | |
{ | |
user n=users.get(i); | |
if(n.id.equals(id)) | |
n.sh=null; | |
} | |
} | |
class user | |
{ | |
String id; | |
ArrayList<message>msgs=new ArrayList<message>(); | |
SocketHandler sh; | |
} | |
class message | |
{ | |
String from; | |
String message; | |
} | |
class SocketHandler implements Runnable | |
{ | |
public Socket s; | |
private String id; | |
public SocketHandler(Socket s) | |
{ | |
this.s=s; | |
} | |
public void run() | |
{ | |
try | |
{ | |
PrintWriter pw=new PrintWriter(s.getOutputStream(),true); | |
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream())); | |
pw.println("Enter your email-id:"); | |
id=br.readLine(); | |
if(registerId(id,this)) | |
{ | |
pw.println("Hi"+id+",you are successfully logged in."); | |
} | |
else | |
{ | |
pw.println("OOPs!Your id seems to be duplicate.Closing your connection.."); | |
s.close(); | |
return; | |
} | |
while(true) | |
{ | |
System.out.println("Menu\n1)Send Mail\n2)Inbox\n3)Logout"); | |
int opt=Integer.parseInt(br.readLine()); | |
switch(opt) | |
{ | |
case 1 : System.out.println("Enter Reciver Address : "); | |
String to=br.readLine(); | |
System.out.println("Enter Message : "); | |
String body=br.readLine(); | |
sendMessage(id,to,body); | |
pw.println("Mail sent to"+to); | |
break; | |
case 2 : ArrayList<message>al=getMessages(id); | |
if(al==null) | |
{ | |
pw.println(0); | |
break; | |
} | |
for(int i=0;i<al.size();i++) | |
{ | |
message m=al.get(i); | |
pw.println("From."+m.from); | |
pw.println("Message."+m.message); | |
} | |
break; | |
case 3 : pw.println("You are successfully logged out .."); | |
logout(id); | |
s.close(); | |
break; | |
default : break; | |
} | |
} | |
} catch(Exception e){} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment