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?

When using client and server architecture you need to declare client IP address which could be (127.1.1.1) and server IP address which could (12345) depends.... at the beggining of ur program as you did to your sockets... Get back to you morro

When using client and server architecture you need to declare client IP address which could be (127.1.1.1) and server IP address which could (12345) depends.... at the beggining of ur program as you did to your sockets... Get back to you morro

Thanks for your reply. :)
I mean how to combine this interface and EmailSender so that when I click the send button it will automatic determine and send the email?

You can't at this point.

The "EmailSender" class has no methods to interact with and is just a series of commands in main(). You'll have to re-write "EmailSender" to be a class with methods to set the parameters for the email and a send() method to send it.

The interface would then pass the info collected to the appropriate methods on the EmailSender and the "Send" button listener would invoke the EmailSender.send() message.

You have a bit of work ahead of you before they can be "combined".

You can't at this point.

The "EmailSender" class has no methods to interact with and is just a series of commands in main(). You'll have to re-write "EmailSender" to be a class with methods to set the parameters for the email and a send() method to send it.

The interface would then pass the info collected to the appropriate methods on the EmailSender and the "Send" button listener would invoke the EmailSender.send() message.

You have a bit of work ahead of you before they can be "combined".

Sorry, not so understand what you means...
I just a beginner of Java.
Can you write a sample for me? Sorry and thanks for reply. :)

If you are a beginner in Java and you cannot understand this thing how can you expect write something as complex as an emaiSender combined with GUI. Even I don't dare to try this.
What he told you is that inside the main there are commands that are executed one after the other. You need to run those commands at you gui. But you cannot use the way they are. You separate them inside methods and the arguments will be given from the gui.

If you are a beginner in Java and you cannot understand this thing how can you expect write something as complex as an emaiSender combined with GUI. Even I don't dare to try this.
What he told you is that inside the main there are commands that are executed one after the other. You need to run those commands at you gui. But you cannot use the way they are. You separate them inside methods and the arguments will be given from the gui.

Erm...
If combine is difficult, then you know how to link its? Means from class EmailSender link to the Interface? Will be easier?

If you cannot understand the answers on this then what you are attempting is beyond you. You need to start from the beginning with some basic Java tutorials such as those listed in the Read Me post "Starting Java" at the top of the forum.

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

Sorry, this just my interface.
No time to study wor... Bacause this project wanna pass up already. :P

My EmailSender was finish, can send the email already.
May I know how to add a attachment? Who can teach me the coding?

Nobody can help me for the attachment?

Nobody can help me for the attachment?

You're trying to add an attachment? When you post, look below the text box and you'll see "Additional Options", then a little bit below that you'll see "Atach Files". Click on the "Manage Attachments" button.

Also, please use code tags and formatting. It makes your code much easier to read.

[code=JAVA] // paste your code here

[/code]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.