|
查看: 1057|回复: 9
|
JAVA高手请进来一下
[复制链接]
|
|
|
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 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 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
|
显示全部楼层
- public interface Mailer {
- public int send(String[] to);
- }
复制代码
- ......
- private Mailer mailer;
- private String[] to;
- btnSend.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- int status = mailer.send(to);
- }
- });
- ......
复制代码
把你的Mailer的code 重写,implement一下上面的Mailer Interface。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 12-4-2008 12:56 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 12-4-2008 12:58 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 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
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 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?  |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|