佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1056|回复: 9

JAVA高手请进来一下

[复制链接]
发表于 12-4-2008 02:05 AM | 显示全部楼层 |阅读模式
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
   


这是我的interface.

[ 本帖最后由 KIRA4 于 12-4-2008 02:11 AM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 12-4-2008 02:05 AM | 显示全部楼层
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 ABC\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: <abc@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: <abc@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: abc\r\n";
      String matric = "Matric No.: 123456\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.");
      }
   }
}

这是我的email sender。
想请问各位大大,怎样才能把这两个class combine起来,或者把class link起来,让user按send button时能够把信息发送出去?



[ 本帖最后由 KIRA4 于 12-4-2008 02:13 AM 编辑 ]
回复

使用道具 举报

发表于 12-4-2008 12:12 PM | 显示全部楼层
Java不是有nested class吗?好像:

class OuterClass {
    ...
    class NestedClass {
        ...
    }
}

将EmailSender放进 JFrame class 里面,然后instantiate一个 new class来用,可以吗?
回复

使用道具 举报

发表于 12-4-2008 12:15 PM | 显示全部楼层
  1. public interface Mailer {
  2.     public int send(String[] to);
  3. }
复制代码

  1. ......
  2. private Mailer mailer;
  3. private String[] to;

  4. btnSend.addActionListener(new ActionListener(){
  5.     public void actionPerformed(ActionEvent e){
  6.         int status = mailer.send(to);
  7.     }
  8. });


  9. ......
复制代码


把你的Mailer的code 重写,implement一下上面的Mailer Interface。
回复

使用道具 举报

 楼主| 发表于 12-4-2008 12:56 PM | 显示全部楼层
原帖由 yhchan 于 12-4-2008 12:12 PM 发表
Java不是有nested class吗?好像:

class OuterClass {
    ...
    class NestedClass {
        ...
    }
}

将EmailSender放进 JFrame class 里面,然后instantiate一个 new class来用,可以吗?

不是要把send function的那些coding放进interface的send button里面吗?
我不是很懂要怎样放...
回复

使用道具 举报

 楼主| 发表于 12-4-2008 12:58 PM | 显示全部楼层
原帖由 苦瓜汤 于 12-4-2008 12:15 PM 发表
public interface Mailer {
    public int send(String[] to);
}

......
private Mailer mailer;
private String[] to;

btnSend.addActionListener(new ActionListener(){
    public void actionPerfor ...

send function可以直接放进这个method里面?
回复

使用道具 举报

Follow Us
发表于 12-4-2008 01:08 PM | 显示全部楼层
我的意思是。。。

现在你的EmailSender,是一个Commmand Prompt Application,对不?你可以将这个class变成一个general的class,然后将send email的coding,放在一个public method里面(比如 sendEmail())。

然后,将这个class放进 Interface的class那里。当click button时,在action里面,create一个EmailSender的class,然后call sendEmail() method。这样就是Object oriented的原意吧,让不同的物体(object)来interact。

Idea是这样,但coding我不懂,毕业后就没用了。你研究一下看看,或向其他大大求救
回复

使用道具 举报

发表于 12-4-2008 02:01 PM | 显示全部楼层
原帖由 KIRA4 于 12-4-2008 12:58 PM 发表

send function可以直接放进这个method里面?


再给一个更直接的例子,你消化一下,然后试试看再把code post上来:
  1. public class MyMailer implements Mailer {
  2.     ......
  3.     public MyMailer(String server, int port){
  4.        ......
  5.     }
  6.     public int send(String[] to){
  7.        for(String s : to)
  8.           System.out.println("Sending mail to "+s+"...");

  9.        return 200;
  10.     }
  11. }

  12. public class MyFrame extends JFrame {
  13.     ..... //省略
  14.     private Mailer mailer;
  15.     private JButton btnSend;

  16.     public MyFrame(Mailer mailer){
  17.        super();
  18.        this.mailer = mailer;
  19.        btnSend = new JButton("Send");
  20.        btnSend.addActionListener(new ActionListener(){
  21.              public void actionPerformed(ActionEvent e){
  22.                    String[] to = new String[]{"bill@microsoft.com","larry@google.com","sergey@google.com"};
  23.                    int status = mailer.send(to);
  24.              }
  25.         });
  26.         .....      
  27.     }
  28.     .......

  29.     //main
  30.     public static void main(String[] args){
  31.        Mailer mailer = new MyMailer("smtp.example.com",25);
  32.        new MyFrame(mailer).setVisible(true);
  33.     }
  34. }
复制代码
回复

使用道具 举报


ADVERTISEMENT

发表于 12-4-2008 08:20 PM | 显示全部楼层
原帖由 KIRA4 于 12-4-2008 02:05 AM 发表
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class EmailSender
{
   public static void main(String[] args) throws Exception
   {
      // Establis ...



if (e.getSource() == btnSend) {
           //加这个
            try {
                EmailSender.send();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }

然后将 public static void main(String[] args) throws Exception
换成
public void send()  throws Exception {
回复

使用道具 举报

发表于 27-4-2008 02:59 PM | 显示全部楼层
借帖一问。。。
请问要怎样在这email sender里加个attachment?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 29-12-2025 03:10 AM , Processed in 0.115179 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表