?? sendpanel.java
字號:
//package mailbox;import java.awt.*;import javax.swing.*;import java.awt.event.*;import javax.swing.border.*;import com.sun.mail.smtp.*;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;import java.util.*;import java.io.*;import javax.swing.filechooser.*;import javax.swing.tree.*;/** * Title: MailBox * Description: send and recieve Email * Copyright: Copyright (c) 2002 * Company: 421 * @author xuhao * @version 1.0 */public class SendPanel extends JPanel { waitDialog wait=new waitDialog(); JLabel jLabel1 = new JLabel(); JTextField toField = new JTextField(); JLabel jLabel2 = new JLabel(); JTextField subjectField = new JTextField(); JLabel jLabel3 = new JLabel(); JTextField attachmentField = new JTextField(); JButton dirButton = new JButton(); JTextArea contentArea = new JTextArea(); JButton sendButton = new JButton(); JScrollPane jsp=new JScrollPane(contentArea); JButton saveButton = new JButton(); Border border1; JLabel jLabel4 = new JLabel(); Vector attachVector=new Vector(); chooseFile Jfc=new chooseFile(); RecievePanel recievePanel; public SendPanel(RecievePanel recievePanel) { try { this.recievePanel=recievePanel; jbInit(); } catch(Exception ex) { ex.printStackTrace(); } } void jbInit() throws Exception { this.setLayout(null); border1 = BorderFactory.createCompoundBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.white,Color.white,Color.white),BorderFactory.createEmptyBorder(2,2,2,2)); subjectField.setFont(new java.awt.Font("Monospaced", 0, 14)); subjectField.setBounds(new Rectangle(14, 146, 195, 27)); jLabel1.setFont(new java.awt.Font("Monospaced", 0, 14)); jLabel1.setText("地址:"); jLabel1.setBounds(new Rectangle(13, 6, 68, 28)); this.setLayout(null); toField.setFont(new java.awt.Font("Monospaced", 0, 14)); toField.setBounds(new Rectangle(11, 36, 194, 28)); jLabel2.setFont(new java.awt.Font("Monospaced", 0, 14)); jLabel2.setText(" 主題:"); jLabel2.setBounds(new Rectangle(4, 114, 49, 30)); attachmentField.setFont(new java.awt.Font("Monospaced", 0, 14)); attachmentField.setBounds(new Rectangle(13, 271, 193, 28)); jLabel3.setFont(new java.awt.Font("Monospaced", 0, 14)); jLabel3.setText("附件:"); jLabel3.setBounds(new Rectangle(17, 238, 45, 32)); dirButton.setFont(new java.awt.Font("Monospaced", 0, 14)); dirButton.setBorder(border1); dirButton.setText("瀏覽"); dirButton.setBounds(new Rectangle(14, 304, 68, 27)); dirButton.addActionListener(new dirAction()); contentArea.setFont(new java.awt.Font("Monospaced", 0, 14)); jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jsp.setBounds(new Rectangle(227, 33, 317, 271)); sendButton.setFont(new java.awt.Font("Monospaced", 0, 14)); sendButton.setBorder(border1); sendButton.setText("立即發送"); sendButton.setBounds(new Rectangle(410, 323, 99, 32)); sendButton.addActionListener(new sendAction()); saveButton.setFont(new java.awt.Font("Monospaced", 0, 14)); saveButton.setBorder(border1); saveButton.setText("保存郵件"); saveButton.setBounds(new Rectangle(265, 323, 99, 32)); saveButton.addActionListener(new saveAction()); jLabel4.setFont(new java.awt.Font("Monospaced", 0, 14)); jLabel4.setText("內容:"); jLabel4.setBounds(new Rectangle(229, 14, 68, 19)); this.add(jsp, null); this.add(attachmentField, null); this.add(toField, null); this.add(subjectField, null); this.add(jLabel3, null); this.add(jLabel2, null); this.add(jLabel1, null); this.add(saveButton, null); this.add(sendButton, null); this.add(dirButton, null); this.add(jLabel4, null); } class sendAction implements ActionListener,Serializable { public void actionPerformed(ActionEvent e) {wait.Start(); (new send()).start(); wait.setVisible(true); } } class dirAction implements ActionListener,Serializable { public void actionPerformed(ActionEvent e) { Jfc.showOpenDialog(new JFrame()); } } class saveAction implements ActionListener,Serializable { public void actionPerformed(ActionEvent e) { MyMessage savemessage=new MyMessage(); savemessage.setAddress(toField.getText()); savemessage.setSubject(subjectField.getText()); savemessage.setAttachment(attachmentField.getText()); savemessage.setRecievedDate(new Date()); savemessage.setContent(contentArea.getText()); recievePanel.sendVector.addElement(savemessage); DefaultMutableTreeNode messageNode=new DefaultMutableTreeNode(recievePanel.sendVector.size()+"."+savemessage.getAddress()); recievePanel.sendModel.insertNodeInto(messageNode,recievePanel.sendNode,recievePanel.sendNode.getChildCount()); recievePanel.recieveTree.scrollPathToVisible(new TreePath(messageNode.getPath())); } } class send extends Thread implements Serializable { public void run() { String smtpServer=User.getSmtpServer(); Session mailSession=null; try { Properties props=new Properties(); props.put("mail.smtp.host",smtpServer); props.put("mail.smtp.auth","true"); if(User.getValidateNeeded()) mailSession=Session.getInstance(props,new PopupAuthenticator()); else mailSession=Session.getInstance(props,null); Message msg=new MimeMessage(mailSession); msg.setSentDate(new Date()); msg.setFrom(new InternetAddress(User.getUserName()+"@"+smtpServer.substring(5))); msg.setRecipient(Message.RecipientType.TO,new InternetAddress(toField.getText())); msg.setSubject(subjectField.getText()); if(attachVector.size()>=1) { Multipart mp=new MimeMultipart(); MimeBodyPart mbp=new MimeBodyPart(); mbp.setContent((contentArea.getText()).toString(),"text/plain;charset=Gb2312"); mp.addBodyPart(mbp); for(int i=0;i<attachVector.size();i++) { MimeBodyPart attachMbp=new MimeBodyPart(); FileDataSource fds=new FileDataSource(((File)(attachVector.elementAt(i))).toString()); attachMbp.setDataHandler(new DataHandler(fds)); attachMbp.setFileName(fds.getName()); mp.addBodyPart(attachMbp); } msg.setContent(mp); } else {msg.setContent((contentArea.getText()).toString(),"text/plain;charset=Gb2312"); } Transport.send(msg); toField.setText(""); contentArea.setText(""); subjectField.setText(""); attachmentField.setText(""); attachVector.removeAllElements(); wait.Stop(); wait.setVisible(false); } catch(Exception e) { wait.Stop(); wait.setVisible(false); toField.setText(""); contentArea.setText(""); subjectField.setText(""); attachmentField.setText(""); attachVector.removeAllElements(); JOptionPane.showMessageDialog(null,e.getMessage()); } } } class chooseFile extends JFileChooser implements Serializable { public void approveSelection() { attachVector.addElement(this.getSelectedFile()); if(attachmentField.getText()==null) attachmentField.setText(""); attachmentField.setText(attachmentField.getText()+(Jfc.getSelectedFile()).toString()+";"); getTopLevelAncestor().setVisible(false); } public void cancelSelection() { getTopLevelAncestor().setVisible(false); } }//end}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -