?? sendmailapplet.java
字號:
package mailapplet;import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;import java.io.*;import java.util.*;public class sendMailApplet extends Applet { private boolean isStandalone = false; private Label label1 = new Label(); private TextField textField1 = new TextField(); private Label label2 = new Label(); private TextField textField2 = new TextField(); private Label label3 = new Label(); private TextField textField3 = new TextField(); private Label label4 = new Label(); private TextField textField4 = new TextField(); private Label label5 = new Label(); private TextArea textArea1 = new TextArea(); private Button button1 = new Button(); private Button button2 = new Button(); //Get a parameter value public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } //Construct the applet public sendMailApplet() { } //Initialize the applet public void init() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { label1.setText("郵件服務器地址"); label1.setBounds(new Rectangle(32, 27, 87, 27)); this.setLayout(null); textField1.setBounds(new Rectangle(122, 25, 162, 24)); label2.setText("發件人地址"); label2.setBounds(new Rectangle(31, 67, 88, 31)); textField2.setBounds(new Rectangle(122, 68, 163, 25)); label3.setText("收件人地址"); label3.setBounds(new Rectangle(31, 108, 88, 31)); textField3.setBounds(new Rectangle(123, 107, 163, 26)); label4.setText("主題"); label4.setBounds(new Rectangle(33, 152, 59, 30)); textField4.setBounds(new Rectangle(123, 148, 162, 25)); label5.setText("正文"); label5.setBounds(new Rectangle(32, 254, 42, 27)); textArea1.setBounds(new Rectangle(86, 194, 257, 187)); button1.setLabel("發送"); button1.setBounds(new Rectangle(102, 408, 76, 27)); button1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button1_actionPerformed(e); } }); button2.setLabel("重寫"); button2.setBounds(new Rectangle(192, 407, 80, 27)); button2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button2_actionPerformed(e); } }); this.add(textField1, null); this.add(label1, null); this.add(label2, null); this.add(textField2, null); this.add(textField3, null); this.add(label3, null); this.add(textField4, null); this.add(label4, null); this.add(label5, null); this.add(textArea1, null); this.add(button1, null); this.add(button2, null); } //Start the applet public void start() { } //Stop the applet public void stop() { } //Destroy the applet public void destroy() { } //Get Applet information public String getAppletInfo() { return "Applet Information"; } //Get parameter info public String[][] getParameterInfo() { return null; } //Main method public static void main(String[] args) { sendMailApplet applet = new sendMailApplet(); applet.isStandalone = true; Frame frame; frame = new Frame() { protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } public synchronized void setTitle(String title) { super.setTitle(title); enableEvents(AWTEvent.WINDOW_EVENT_MASK); } }; frame.setTitle("Applet Frame"); frame.add(applet, BorderLayout.CENTER); applet.init(); applet.start(); frame.setSize(400,320); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); frame.setVisible(true); } void button1_actionPerformed(ActionEvent e) { String mail_host = textField1.getText(); String from = textField2.getText(); String to = textField3.getText(); String subject = textField4.getText(); String body = textArea1.getText(); if ((from==null)||(from.trim().length()==0)) //若發件人輸入框為空則提示錯誤 { JOptionPane msg = new JOptionPane(); JOptionPane.showMessageDialog(this, "發件人地址不能為空!", "地址不能為空!", 0); } else if ((to==null)||(to.trim().length()==0)) //若收件人輸入框為空則提示錯誤 { JOptionPane msg = new JOptionPane(); JOptionPane.showMessageDialog(this, "收件人地址不能為空!", "地址不能為空!", 0); } else { try { Properties props = new Properties(); props.put("mail.host", mail_host); //設置郵件服務器的名稱 props.put("mail.transport.protocol", "smtp"); //設置郵件傳輸協議為pop3 Session sendMailSession; boolean sessionDebug = false; sendMailSession = Session.getDefaultInstance(props, null); //產生一個發送郵件的Session實例 sendMailSession.setDebug(sessionDebug); Message newMessage = new MimeMessage(sendMailSession); //產生一個Message類的實例用來設置郵件內容 newMessage.setFrom(new InternetAddress(from)); //設置發信人的地址 InternetAddress[] address = {new InternetAddress(to)}; newMessage.setRecipients(Message.RecipientType.TO, address); //設置收信人地址 newMessage.setSubject(subject); //設置信件主題 newMessage.setSentDate(new Date()); //設置信件發送日期 newMessage.setText(body); //設置信件正文內容 Transport.send(newMessage); //發送信件 JOptionPane msg = new JOptionPane(); JOptionPane.showMessageDialog(this, "發送郵件成功!", "發送郵件成功!", 1); textField1.setText(""); textField2.setText(""); textField3.setText(""); textField4.setText(""); textArea1.setText(""); } catch(Exception e1) { JOptionPane msg = new JOptionPane(); //捕捉異常情況 JOptionPane.showMessageDialog(this, "不能發送郵件!", "不能發送郵件!", 2); e1.printStackTrace(); } } } void button2_actionPerformed(ActionEvent e) { //用戶點擊“重寫”按鈕產生的動作 textField1.setText(""); textField2.setText(""); textField3.setText(""); textField4.setText(""); textArea1.setText(""); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -