import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class EmailSender
{
public static void main(String[] args) throws Exception
{
// Establish a TCP connection with the mail server.
Socket soc = new Socket("gsmtp183.google.com", 25);
// Create a BufferedReader to read a line at a time.
InputStream is = soc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
// Read greeting from the server.
String response = br.readLine();
System.out.println(response);
if (!response.startsWith("220"))
{
throw new Exception("220 reply not received from server.");
}
// Get a reference to the socket's output stream.
OutputStream os = soc.getOutputStream();
// Send HELO command and get server response.
String command = "HELO Ng\r\n";
System.out.print(command);
os.write(command.getBytes("US-ASCII"));
response = br.readLine();
System.out.println(response);
if (!response.startsWith("250"))
{
throw new Exception("250 reply not received from server.");
}
// Send MAIL FROM command.
String from = "MAIL FROM: <NgSzeYin2005@gmail.com>\r\n";
System.out.print(from);
os.write(from.getBytes("US-ASCII"));
response = br.readLine();
System.out.println(response);
if (!response.startsWith("250"))
{
throw new Exception("250 reply not received from server.");
}
// Send RCPT TO command.
String rcpt = "RCPT TO: <NgSzeYin2005@gmail.com>\r\n";
System.out.print(rcpt);
os.write(rcpt.getBytes("US-ASCII"));
response = br.readLine();
System.out.println(response);
if (!response.startsWith("250"))
{
throw new Exception("250 reply not received from server.");
}
// Send DATA command.
String data = "DATA\r\n";
System.out.print(data);
os.write(data.getBytes("US-ASCII"));
response = br.readLine();
System.out.println(response);
if (!response.startsWith("354"))
{
throw new Exception("354 reply not received from server.");
}
// Send message data.
String subject = "Subject: SMTP Telnet Testing\r\n\r\n";
String name = "Name: Ng Sze Yin\r\n";
String matric = "Matric No.: 105540\r\n";
System.out.print(subject);
os.write(subject.getBytes("US-ASCII"));
System.out.print(name);
os.write(name.getBytes("US-ASCII"));
System.out.print(matric);
os.write(matric.getBytes("US-ASCII"));
// End with line with a single period.
String end = ".\r\n";
System.out.print(end);
os.write(end.getBytes("US-ASCII"));
response = br.readLine();
System.out.println(response);
if (!response.startsWith("250"))
{
throw new Exception("250 reply not received from server.");
}
// Send QUIT command.
String quit = "QUIT\r\n";
System.out.print(quit);
os.write(quit.getBytes("US-ASCII"));
response = br.readLine();
System.out.println(response);
if (!response.startsWith("221"))
{
throw new Exception("221 reply not received from server.");
}
}
}
import java.util.*; // Utilities
import java.awt.*;
import java.awt.event.*; // ActionListener
import javax.swing.*; //GUI
import javax.swing.border.TitledBorder;
public class EmailSenderSys extends JFrame implements ActionListener
{
private JLabel host,name,from,to,subject,msg;
private JButton btnSend,btnReset,btnExit;
private JTextField txtHost,txtFrom,txtTo,txtSub;
private JPanel mail;
private JTextArea msgArea;
private String mailFrom,rcptTo,Subject;
EmailSenderSys() // Constructor
{
super ("Email Sender");
CreateInterface();
setSize (700,520);
setVisible(true);
} // Constructor
public void CreateInterface()
{
Container pane = getContentPane();
pane.setLayout(null);
// Create JLabel
name = new JLabel ("Email Sender");
name.setBounds(265, 10, 600, 25);
name.setFont(new Font("Georgia", Font.BOLD,25));
name.setForeground(Color.blue);
pane.add(name);
// Create Email Sender Panel
mail = new JPanel();
mail.setBounds(10,50,670,142);
mail.setBorder(new TitledBorder("Email"));
mail.setLayout(null);
pane.add(mail);
host = new JLabel ("Local Mail Server :");
host.setBounds(20, 4, 180, 60);
mail.add(host);
txtHost = new JTextField();
txtHost.setBounds(140, 20, 520, 25);
mail.add(txtHost);
from = new JLabel ("From :");
from.setBounds(20, 32, 180, 60);
mail.add(from);
txtFrom = new JTextField();
txtFrom.setBounds(140, 48, 520, 25);
mail.add(txtFrom);
to = new JLabel ("To :");
to.setBounds(20,60, 180, 60);
mail.add(to);
txtTo = new JTextField();
txtTo.setBounds(140,76, 520, 25);
mail.add(txtTo);
subject = new JLabel ("Subject :");
subject.setBounds(20, 86, 180, 60);
mail.add(subject);
txtSub = new JTextField();
txtSub.setBounds(140,104, 520, 25);
mail.add(txtSub);
// Create Message Area
msgArea = new JTextArea();
msgArea.setBounds(10, 170, 670, 250);
msgArea.setBorder(new TitledBorder("Message"));
JScrollPane sP = new JScrollPane(msgArea);
pane.add(sP);
pane.add(msgArea);
//Create Send Button
btnSend = new JButton("Send");
btnSend.setBounds(185,425,100,28);
pane.add(btnSend);
btnSend.addActionListener(this);
//Create Reset Button
btnReset = new JButton("Reset");
btnReset.setBounds(305,425,100,28);
pane.add(btnReset);
btnReset.addActionListener(this);
//Create Exit Button
btnExit = new JButton("Exit");
btnExit.setBounds(425,425,100,28);
pane.add(btnExit);
btnExit.addActionListener(this);
}
public static void main(String []agrs)
{
EmailSenderSys bs = new EmailSenderSys();
bs.setDefaultCloseOperation(EXIT_ON_CLOSE);
}// main class
public void actionPerformed (ActionEvent e)
{
if (e.getSource()== btnSend)
{
}
if(e.getSource()==btnReset)
{
txtHost.setText("");
txtFrom.setText("");
txtTo.setText("");
txtSub.setText("");
msgArea.setText("");
}
if(e.getSource()==btnExit)
{
System.exit(0);
}
}
} //EmailSenderSys Class
Who can teach me how to combine these two class so that the email can be send?