Alek Modi

Alek Modi
Follow You Heart!

Friday, September 9, 2011

#3. Socket Prgrmming | MailClient Source Code.


//MailClient.java
//User Interface is being developed here!

 import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
/*MY INFO  */
/**
 * @author Alek
 * amodi@clunet.edu
 *
 * Using Cal Lutheran Local Mailserver and port number
 * this app can send and process SMT Protocol.
 *
 */
//Use this- smtpauth.callutheran.edu

/*
 * Developed on - Sep 08, 2011
 *
 * WireShark Lab work - MailClient
 */


public class MailClient extends Frame
{
private static final long serialVersionUID = 1L;
private Button JButtonSend = new Button("Send");
private Button JButtonClear = new Button("Clear");
private Button JButtonQuit = new Button("Quit");  
private Label Slb = new Label("Local mailserver:");
private TextField Sfield = new TextField(" ", 40);  
private Label FLabel = new Label("From:");  
private TextField FField = new TextField("", 40);
private Label toLabel = new Label("To:");
private TextField toField = new TextField("", 40);
private Label subjectLabel = new Label("Subject:");
private TextField subjectField = new TextField("", 40);
private Label messageLabel = new Label("Message:");  
private TextArea messageText = new TextArea(10, 40);  

public MailClient()
{  
super("Java Mailclient - By Alek Modi");  
// Organize the layout    
Panel serverPanel = new Panel(new BorderLayout());
Panel fromPanel = new Panel(new BorderLayout());
Panel toPanel = new Panel(new BorderLayout());  
Panel subjectPanel = new Panel(new BorderLayout());
Panel messagePanel = new Panel(new BorderLayout());  
serverPanel.add(Slb, BorderLayout.WEST);  
serverPanel.add(Sfield, BorderLayout.CENTER);
fromPanel.add(FLabel, BorderLayout.WEST);  
fromPanel.add(FField, BorderLayout.CENTER);  
toPanel.add(toLabel, BorderLayout.WEST);  
toPanel.add(toField, BorderLayout.CENTER);
subjectPanel.add(subjectLabel, BorderLayout.WEST);
subjectPanel.add(subjectField, BorderLayout.CENTER);
messagePanel.add(messageLabel, BorderLayout.NORTH);
messagePanel.add(messageText, BorderLayout.CENTER);
Panel fieldPanel = new Panel(new GridLayout(0, 1));
fieldPanel.add(serverPanel);
fieldPanel.add(fromPanel);  
fieldPanel.add(toPanel);  
fieldPanel.add(subjectPanel);
Panel buttonPanel = new Panel(new GridLayout(1, 0));
JButtonSend.addActionListener(new SendListener());  
JButtonClear.addActionListener(new ClearListener());
JButtonQuit.addActionListener(new QuitListener());  
buttonPanel.add(JButtonSend);  
buttonPanel.add(JButtonClear);  
buttonPanel.add(JButtonQuit);
add(fieldPanel, BorderLayout.NORTH);  
add(messagePanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);  

pack();
setVisible(true);
}


static public void main(String argv[])
{  
new MailClient();

}    
//SendListener  
class SendListener implements ActionListener
{  
public void actionPerformed(ActionEvent event)
{    
System.out.println("Sending mail");
if ((Sfield.getText()).equals(""))
{      
Component frame = null;  
JOptionPane.showMessageDialog(frame,  
"Please provide the local mailserver.",        
"Error",        
JOptionPane.ERROR_MESSAGE);
return;      
}            
if((FField.getText()).equals(""))
{      
Component frame = null;
JOptionPane.showMessageDialog(frame,  
"Please provide the sender's email.",
"Error",        
JOptionPane.ERROR_MESSAGE);  
return;      
}      
if((toField.getText()).equals(""))
{        
Component frame = null;  
JOptionPane.showMessageDialog(frame,  
"Please provide the recipient's email.",  
"Error",        
JOptionPane.ERROR_MESSAGE);  
return;        
}            

Message mailMessage = new Message(FField.getText(),
toField.getText(),  
subjectField.getText(),
messageText.getText());      
if(!mailMessage.isValid())
{      
return;
}            
Envelope envelope = null;
try
{    
envelope = new Envelope(mailMessage,  
Sfield.getText());
}
catch (UnknownHostException e)
{            
return;    
}      
try
{          
SMTPConnection connection = new SMTPConnection(envelope);
connection.send(envelope);  
connection.close();    
}
catch (IOException error)
{      
System.out.println("Sending failed: " + error);  
Component frame = null;
JOptionPane.showMessageDialog(frame,
"Error",    
"Error",    
JOptionPane.ERROR_MESSAGE);
return;  
}    
System.out.println("Mail sent succesfully!");  
Component frame = null;
JOptionPane.showMessageDialog(frame,  
" succesful!",    
"",    
JOptionPane.INFORMATION_MESSAGE);
}  
}  
    class ClearListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {    
    System.out.println("Clearing fields");
    FField.setText("");      
    toField.setText("");      
    subjectField.setText("");  
    messageText.setText("");  
    }  
    }      
    class QuitListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {      
    System.exit(0);
    }  
    }
}

No comments:

Post a Comment