⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendmailapplet.java

📁 Java网络编程实例 第10章实例.rar
💻 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 + -