public static void main(String[] args) { mailSendFrame mailSendFrame = new mailSendFrame(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = mailSendFrame.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } mailSendFrame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); mailSendFrame.setVisible(true); mailSendFrame.show(); } |
//组件实例变量的定义 Panel panelMain = new Panel(); Panel panelUp = new Panel(); Panel panel3 = new Panel(); Panel panel4 = new Panel(); Panel panel6 = new Panel(); Panel panel7 = new Panel(); TextField txtServer = new TextField(); TextField txtTo = new TextField(); TextField txtFrom = new TextField(); TextField txtSubject = new TextField(); Panel panel8 = new Panel(); Label lblFile = new Label(); Button cmdBrowse = new Button(); Panel panelDown = new Panel(); TextArea txtMail = new TextArea(); Panel panel10 = new Panel(); Button cmdSend = new Button(); Button cmdExit = new Button(); ....... ....... panelMain.add(panelUp, null); panelUp.add(panel3, null); panel3.add(new Label("发信服务器:"), null); panel3.add(txtServer, null); panelUp.add(panel4, null); panel4.add(new Label("收件人:"), null); panel4.add(txtTo, null); panelUp.add(panel6, null); panelUp.add(panel7, null); panel7.add(new Label("主题:"), null); panel7.add(txtSubject, null); panel6.add(new Label("发件人:"), null); panel6.add(txtFrom, null); panelUp.add(panel8, null); panel8.add(new Label("附件: "), null); panel8.add(lblFile, null); panel8.add(cmdBrowse, null); panelMain.add(panelDown, null); panelDown.add(txtMail, BorderLayout.CENTER); panelDown.add(panel10, BorderLayout.SOUTH); panel10.add(cmdSend, null); panel10.add(cmdExit, null); panelDown.add(new Label(" "), BorderLayout.EAST); panelDown.add(new Label(" "), BorderLayout.WEST); ........ ........ |
private FileDialog openFileDialog= new FileDialog(this,"打开文件",FileDialog.LOAD); public mailSendFrame() { try { Init(); } catch(Exception e) { e.printStackTrace(); } } ...... ...... |
cmdSend.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { cmdSend_actionPerformed(e); } } |
void cmdSend_actionPerformed(ActionEvent e) { mailSender.setFrom(txtFrom.getText().trim()); mailSender.setTo(txtTo.getText().trim()); mailSender.addHeader("Subject",txtSubject.getText().trim()) ; mailSender.addData(txtMail.getText()) ; if(!lblFile.getText().trim().equals("") ) mailSender.addAttachment(lblFile.getText().trim()); mailSender.open(txtServer.getText().trim(),25); mailSender.transmit(); mailSender.close(); } |
cmdExit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { cmdExit_actionPerformed(e); } } this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent e) { this_windowClosing(e); } } |
void cmdExit_actionPerformed(ActionEvent e) { System.exit(0); } void this_windowClosing(WindowEvent e) { System.exit(0); } |
public int open(String serverName, int port){ try{ mailSocket = new Socket(serverName, port); send = new PrintWriter(mailSocket.getOutputStream(), true); recv = new BufferedReader(new InputStreamReader(mailSocket.getInputStream())); String s1 = recv.readLine(); char c = s1.charAt(0); if((c == '4') | (c == '5')) return 0; } catch(Exception e){ return 0; } return 1; } |
public int transmit(){ boolean flag = true; //发送HELO 命令 if(domain.length() != 0){ int i = sendString("HELO " + domain); if(i != 1) return 0; } //发送MAIL FROM 命令(发件人) if(from.length() != 0){ int j = sendString("MAIL FROM:" + from); if(j != 1) return 0; } //发送RCPT TO 命令(收件人) if(to.length() != 0){ int k = sendString("RCPT TO:" + to); if(k != 1) return 0; } //发送邮件正文(DATA 命令) if(sendString("DATA") != 1) return 0; //发送邮件头信息 for(int l = 0; l < x_set.size(); l += 2){ String s = (String)x_set.elementAt(l); send.println(s + ": " + x_set.elementAt(l + 1)); } //发送时间及相关内容格式说明 if(x_set.indexOf("Date") < 0) send.println("Date: " + (new Date()).toString()); ........ ........ |
private int sendString(String s){ String s1 = ""; try{ send.println(s); s1 = recv.readLine(); } catch(Exception e){ System.out.print(s1); return 0; } if(s1.length() == 0) return 0; char c = s1.charAt(0); return !((c == '4') | (c == '5')) ? 1 : 0; } |
public int close(){ int i = 0; try{ i += sendString("QUIT"); mailSocket.close(); } catch(Exception e){ return 0; } return i == 0 ? 1 : 0; } |
import java.awt.*; import java.awt.event.*; public class mailSendFrame extends Frame { smtpMail mailSender=new smtpMail(); Panel panelMain = new Panel(); Panel panelUp = new Panel(); Panel panel3 = new Panel(); Panel panel4 = new Panel(); Panel panel6 = new Panel(); Panel panel7 = new Panel(); TextField txtServer = new TextField(); TextField txtTo = new TextField(); TextField txtFrom = new TextField(); TextField txtSubject = new TextField(); Panel panel8 = new Panel(); Label lblFile = new Label(); Button cmdBrowse = new Button(); Panel panelDown = new Panel(); TextArea txtMail = new TextArea(); Panel panel10 = new Panel(); Button cmdSend = new Button(); Button cmdExit = new Button(); private FileDialog openFileDialog = new FileDialog(this,"打开文件",FileDialog.LOAD); public mailSendFrame() { try { Init(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { mailSendFrame mailSendFrame = new mailSendFrame(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = mailSendFrame.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } mailSendFrame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); mailSendFrame.setVisible(true); mailSendFrame.show(); } private void Init() throws Exception { this.setLayout(new BorderLayout()); panelMain.setLayout(new GridLayout(2,1)); panelUp.setLayout(new GridLayout(6,1)); panel3.setLayout(new FlowLayout()); this.setVisible(true); ....... ....... //smtpMail.java 的源代码 import java.io.*; import java.net.Socket; import java.util.*; public class smtpMail{ private boolean sendConf=false; public static final int OK = 1; public static final int ERROR = 0; private static final String TEXT = "1"; private static final String TFILE = "2"; private static final String BFILE = "3"; private static final String CPR = "Java 1.0"; private static final String MAILER = "X-Mailer"; private static final int BUFFER_SIZE = 48; private String DELIMETER; private String SEPARATOR; private static final int HOW_LONG = 6; private static final char SMTP_ERROR_CODE1 = 52; private static final char SMTP_ERROR_CODE2 = 53; private static final int fillchar = 61; private static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; private Socket mailSocket; private BufferedReader recv; private PrintWriter send; private String from; private String to; private String domain; private Vector x_set; private Vector body; private Vector attach; public smtpMail(){ DELIMETER = ""; SEPARATOR = ""; mailSocket = null; recv = null; send = null; from = ""; to = ""; domain = ""; x_set = new Vector(); body = new Vector(); attach = new Vector(); DELIMETER = getId(); SEPARATOR = System.getProperty("file.separator"); } ......... ......... |