java mail例子(2)

//本类是mailSender的ui

package org.mingyuan.mail;

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.SwingUtilities;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

public class MailSenderUI {

         /**

         * @param args

         * @throws UnsupportedLookAndFeelException

         * @throws IllegalAccessException

         * @throws InstantiationException

         * @throws ClassNotFoundException

         */

         public static void main(String[] args) throws ClassNotFoundException,

                            InstantiationException, IllegalAccessException,

                            UnsupportedLookAndFeelException {

                   // TODO Auto-generated method stub

                   UIManager

                                     .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

                   MailSenderFrame frame = new MailSenderFrame();

                   SwingUtilities.updateComponentTreeUI(frame);

         }

}

class MailSenderFrame extends JFrame {

         public MailSenderFrame() {

                   this.setTitle("Mail sender");

                   this.setSize(300, 200);

                   this.setLocation(400, 300);

                   this.setAlwaysOnTop(true);

                   this.setDefaultCloseOperation(EXIT_ON_CLOSE);

                   this.setVisible(true);

                   this.add(new MailSenderPanel());

                   // this.setResizable(false);

                   this.pack();

         }

}

class MailSenderPanel extends JPanel {

         private JButton sendButton = new JButton("发送");

         private JButton cancelButton = new JButton("取消");

         private JLabel userNameLabel = new JLabel("用户名");

         private JLabel passwordLabel = new JLabel("口令");

         private JLabel receiverLabel = new JLabel("收件人");

         private JLabel subjectLabel = new JLabel("主题");

         private JLabel contentLabel = new JLabel("邮件内容");

         private JTextField userNameField = new JTextField(20);

         private JPasswordField passwordField = new JPasswordField(20);

         private JTextField receiverField = new JTextField(20);

         private JTextField subjectField = new JTextField(20);

         private JTextArea contentArea = new JTextArea(4, 6);

         public MailSenderPanel() {

                   this.setVisible(true);

                   this.setLayout(new BorderLayout());

                   JPanel panel1 = new JPanel(new GridLayout(3, 4));

                   panel1.add(userNameLabel);

                   panel1.add(userNameField);

                   panel1.add(passwordLabel);

                   panel1.add(passwordField);

                   panel1.add(receiverLabel);

                   panel1.add(receiverField);

                   panel1.add(new JLabel());

                   panel1.add(new JLabel());

                   panel1.add(subjectLabel);

                   panel1.add(subjectField);

                   panel1.add(new JLabel());

                   panel1.add(new JLabel());

                   this.add(panel1, BorderLayout.NORTH);

                   JPanel panel2 = new JPanel(new BorderLayout());

                   panel2.add(contentLabel, BorderLayout.WEST);

                   contentArea.setWrapStyleWord(true);

                   contentArea.setColumns(8);

                   contentArea.setRows(8);

                   JScrollPane pane = new JScrollPane(contentArea);

                   panel2.add(pane, BorderLayout.CENTER);

                   this.add(panel2, BorderLayout.CENTER);

                   JPanel panel3 = new JPanel(new GridLayout(1, 4));

                   panel3.add(new JLabel());

                   panel3.add(sendButton);

                   panel3.add(cancelButton);

                   panel3.add(new JLabel());

                   this.add(panel3, BorderLayout.SOUTH);

                   sendButton.addActionListener(new ActionListener() {

                            @Override

                            public void actionPerformed(ActionEvent arg0) {

                                     // TODO Auto-generated method stub

                                     MailSender mail = new MailSender(

                                                        userNameField.getText().trim(),

                                                        passwordField.getText(),

                                                        receiverField.getText().trim(), contentArea.getText(),

                                                        subjectField.getText().trim());

                                     mail.test();

                                     if(mail.send()){

                                               JOptionPane.showMessageDialog(MailSenderPanel.this,"发送成功!","发送成功",JOptionPane.INFORMATION_MESSAGE, null);

                                     }else{

                                               JOptionPane.showMessageDialog(MailSenderPanel.this,"发送失败!","发送错误",JOptionPane.ERROR_MESSAGE, null);

                                     }

                            }

                   });

                   cancelButton.addActionListener(new ActionListener() {

                            @Override

                            public void actionPerformed(ActionEvent e) {

                                     // TODO Auto-generated method stub

                                     userNameField.setText("");

                                     passwordField.setText("");

                                     receiverField.setText("");

                                     subjectField.setText("");

                                     contentArea.setText("");

                            }

                   });

         }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值