主要是想熟悉一下jigloo插件的使用,所以做了一个发邮件的小程序,其实调用的都是人家的东西,我们自己只做了最简单的傻瓜操作,每思及此,感觉要学的东西还很多。
本人很懒,搞swing那套东西很不给力,所以就用jigloo插件帮我布个局,代码跟图形界面的编辑其实是完全独立的,有了代码可以直接运行无需jigloo插件。
另外需要下载两个jar文件:mail.jar和activation.jar,这是java发邮件所必须的类库
下载不到可以去http://download.csdn.net/source/2676485我上传的
(做这么龊下载还要分?好吧,我发邮件你总行了把)
直接上码,大家一看就都明白了,Resources类里面的图,大家随便放几个图进去吧
---------------------无架构无意识无优化无高端无河蟹昏哥线-------------------------------------
主界面MailSender.java
import java.awt.BorderLayout; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JSeparator; import javax.swing.JTabbedPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.WindowConstants; import javax.swing.SwingUtilities; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.swing.BorderFactory; import javax.swing.JFileChooser; import java.io.*; /** * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class MailSender extends javax.swing.JFrame { { //Set Look & Feel try { javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch(Exception e) { e.printStackTrace(); } } public LoginDialog dlg; //登录对话框 public static Font fontOne; private static String attachPath;//附件路径 private static String mailAddr;//发件人邮箱地址 private static String mailPwd;//发件人邮箱密码 public boolean loginCheck;//是否已登录 static Resources resources = new Resources(); private JLabel fromLabel;//显示发件人 private JLabel jLabel2; private JButton loginJB; private JLabel attachLabel; private JLabel stateLabel;//邮件状态 private boolean hasAttach;//是否有附件 private JButton attachJB;//添加附件 private JLabel recieverLabel; private JLabel tittleLabel; private JTextField recieverJT;//@前面的邮箱地址 private JTextField smtpJT;//@后面的邮箱地址 private JTextField tittleJT;//主题 private JTextArea textJTA;//内容 private JButton sendJB; private JSeparator jSeparator1;//分割线 private JLabel jLabel1; /** * Auto-generated main method to display this JFrame */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { MailSender inst = new MailSender(); inst.setLocationRelativeTo(null); inst.setBounds(400, 200, 420, 400); inst.setVisible(true); } }); } public MailSender() { super(); setBounds(0, 0, 400, 400); //设置窗口位置和大小 fontOne = new Font("null", Font.BOLD, 15); loginCheck = false;//未登录 dlg = new LoginDialog(this); dlg.setVisible(false); hasAttach = false;//没有附件 initGUI(); } private void initGUI() { try { setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); setTitle("/u90ae/u4ef6"); getContentPane().setLayout(null); setIconImage(resources.mail_send.getImage()); { recieverLabel = new JLabel(); recieverLabel.setFont(fontOne); getContentPane().add(recieverLabel); recieverLabel.setText("/u6536/u4ef6/u4eba/u5730/u5740:"); recieverLabel.setBounds(10, 12, 92, 15); } { tittleLabel = new JLabel(); tittleLabel.setFont(fontOne); getContentPane().add(tittleLabel); tittleLabel.setText("/u4e3b/u9898:"); tittleLabel.setBounds(10, 39, 69, 15); } { recieverJT = new JTextField(); getContentPane().add(recieverJT); recieverJT.setBounds(101, 9, 149, 22); } { jLabel1 = new JLabel(); getContentPane().add(jLabel1); jLabel1.setFont(fontOne); jLabel1.setText("@"); jLabel1.setBounds(255, 12, 37, 15); } { smtpJT = new JTextField(); getContentPane().add(smtpJT); smtpJT.setBounds(299, 9, 87, 22); } { tittleJT = new JTextField(); getContentPane().add(tittleJT); tittleJT.setBounds(101, 36, 285, 22); } { jSeparator1 = new JSeparator(); getContentPane().add(jSeparator1); jSeparator1.setBounds(7, 66, 368, 10); } { textJTA = new JTextArea(); getContentPane().add(textJTA); textJTA.setBounds(12, 82, 374, 179); } { sendJB = new JButton(); getContentPane().add(sendJB); sendJB.setText("/u53d1/u9001"); sendJB.setBounds(12, 321, 75, 30); sendJB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { sendJBActionPerformed(evt); // TODO Auto-generated catch block } }); } { stateLabel = new JLabel(); getContentPane().add(stateLabel); stateLabel.setBounds(90, 327, 70, 24); } { attachJB = new JButton(); getContentPane().add(attachJB); attachJB.setText("/u9644/u4ef6"); attachJB.setBounds(156, 321, 75, 30); attachJB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { attachJBActionPerformed(evt); } }); attachJB.setIcon(resources.add); } { attachLabel = new JLabel(); getContentPane().add(attachLabel); attachLabel.setBounds(239, 315, 136, 45); } { loginJB = new JButton(); getContentPane().add(loginJB); loginJB.setIcon(resources.login); loginJB.setText("/u767b/u5f55"); loginJB.setBounds(12, 267, 89, 48); loginJB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { loginJBActionPerformed(evt); } }); } { jLabel2 = new JLabel(); getContentPane().add(jLabel2); jLabel2.setFont(resources.fontOne); jLabel2.setText("/u53d1/u4ef6/u4eba:"); jLabel2.setBounds(124, 282, 75, 18); } { fromLabel = new JLabel(); getContentPane().add(fromLabel); fromLabel.setBounds(214, 282, 161, 21); } pack(); pack(); } catch (Exception e) { e.printStackTrace(); } } /* * 点击登录按钮 */ private void loginJBActionPerformed(ActionEvent evt) { //System.out.println("loginJB.actionPerformed, event="+evt); //TODO add your code for loginJB.actionPerformed if(!loginCheck) { dlg.setVisible(true); } else { dlg.setVisible(false); } } /* * 点击发送按钮 */ private void sendJBActionPerformed(ActionEvent evt) { // System.out.println("sendJB.actionPerformed, event="+evt); // TODO add your code for sendJB.actionPerformed // stateLabel.setText("邮件发送中..."); if (loginCheck) { //已登录 if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(null, "确认发送邮件吗?", "邮件", JOptionPane.YES_NO_OPTION)) { try { String to = recieverJT.getText().trim() + "@" + smtpJT.getText().trim();//获得收件人地址 System.out.println(to); String subject = tittleJT.getText(); String body = textJTA.getText(); Properties props = System.getProperties(); // 设置SMTP邮件服务器: String smtpAddr = smtpAddressConfirm(smtpJT.getText()); if ("not include" == smtpAddr) { JOptionPane.showMessageDialog(null, "无法解析邮箱地址!"); jLabel1.setIcon(resources.error); } System.out.println(smtpAddr); props.put("mail.smtp.host", smtpAddr); props.setProperty("mail.smtp.port", "25"); // SMTP服务器需要验证: props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.auth", "true"); // 传入用户名和口令: Session session = Session.getDefaultInstance(props, new PasswordAuthenticator(mailAddr, mailPwd)); session.setDebug(true); // 创建新邮件: Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(mailAddr)); msg.setRecipients(Message.RecipientType.TO, InternetAddress .parse(to, false)); msg.setSubject(subject); msg.setText(body); msg.setSentDate(new Date()); // 发送: if (hasAttach) { MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.attachFile("sss"); Multipart multi = new MimeMultipart(); FileDataSource fds = new FileDataSource(attachPath); mbp1.setDataHandler(new DataHandler(fds)); mbp1.setFileName(attachLabel.getText()); multi.addBodyPart(mbp1); msg.setContent(multi); } msg.saveChanges(); Transport.send(msg); System.out.println("mail sended!"); stateLabel.setText("发送成功!"); sendJB.setIcon(resources.ok); setIconImage(resources.mail_sended.getImage()); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "发送邮件失败!"); System.err.println("邮件发送失败的原因是:" + ex.getMessage()); System.err.println("具体的错误原因"); ex.printStackTrace(System.err); } } } else { JOptionPane.showMessageDialog(null, "请先登录!"); } } private String smtpAddressConfirm(String smtpAddr) { if (smtpAddr.contains(".com")) { if (smtpAddr.contains("163")) { smtpAddr = "smtp.163.com"; } else if (smtpAddr.contains("qq")) { smtpAddr = "smtp.qq.com"; } else { return "not include"; } } else { return "not include"; } return smtpAddr; } /* * 添加附件 */ private void attachJBActionPerformed(ActionEvent evt) { //System.out.println("attachJB.actionPerformed, event="+evt); //TODO add your code for attachJB.actionPerformed JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setDialogTitle("打开文件夹"); chooser.setCurrentDirectory(new File("E:/新建文件夹/my coding tip")); chooser.setMultiSelectionEnabled(true);//支持多个附件 int returnVal = chooser.showOpenDialog(this); if(returnVal == JFileChooser.APPROVE_OPTION) { attachPath = chooser.getSelectedFile().getAbsolutePath(); attachLabel.setIcon(resources.attach); attachLabel.setText(chooser.getSelectedFile().getName()); } this.setIconImage(resources.mail_attachment.getImage()); this.hasAttach = true; //有附件 } public void setLoginCheck(boolean loginCheck) { this.loginCheck = loginCheck; } public static void setMailAddr(String mailAddr) { MailSender.mailAddr = mailAddr; } public static void setMailPwd(String mailPwd) { MailSender.mailPwd = mailPwd; } public JLabel getFromLabel() { return fromLabel; } public boolean isLoginCheck() { return loginCheck; } } // //资源类 其他类中用到的图标,字体等 class Resources { public static ImageIcon mail_send; public static ImageIcon mail_sended; public static ImageIcon mail_attachment; public static ImageIcon login; public static ImageIcon ok; public static ImageIcon error; public static ImageIcon add; public static ImageIcon attach; public static ImageIcon logon; public static ImageIcon logon1; public static ImageIcon logout; public static ImageIcon logout1; public static Font fontOne; public Resources() { mail_send = new ImageIcon("images/mail_send.gif"); mail_sended = new ImageIcon("images/mail_sended.gif"); mail_attachment = new ImageIcon("images/mail_attachment.gif"); login = new ImageIcon("images/login.jpg"); ok = new ImageIcon("images/ok.png"); error = new ImageIcon("images/error.gif"); add = new ImageIcon("images/add.gif"); attach = new ImageIcon("images/attach.gif"); logon = new ImageIcon("images/logon.gif"); logon1 = new ImageIcon("images/logon1.gif"); logout = new ImageIcon("images/logout.gif"); logout1 = new ImageIcon("images/logout1.gif"); fontOne = new Font("null", Font.BOLD, 20); } }
登录的对话框界面LoginDialog.java
import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingUtilities; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JButton; /** * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class LoginDialog extends javax.swing.JDialog { { //Set Look & Feel try { javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch(Exception e) { e.printStackTrace(); } } public static MailSender ms; public static Resources rs; public static Font font; private static String userAddr; private static String userPwd; private JLabel pwdLabel; private JTextField userJT; private JButton logoutJB; private JButton logonJB; private JPasswordField pwdJP; private JLabel userLabel; /** * Auto-generated main method to display this JDialog */ /* public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { //JFrame frame = new JFrame(); //LoginDialog inst = new LoginDialog(ms); //inst.setVisible(true); } }); }*/ public LoginDialog(MailSender frame) { super(frame); ms = frame; userAddr = ""; userPwd = ""; font = new Font("null", Font.BOLD, 20); initGUI(); } private void initGUI() { try { { this.setTitle("/u8d26/u53f7/u767b/u5f55"); getContentPane().setLayout(null); } this.setBounds(450,250,300,200); this.setVisible(true); { userLabel = new JLabel(); getContentPane().add(userLabel); userLabel.setFont(font); userLabel.setText("/u90ae/u7bb1/u5730/u5740:"); userLabel.setBounds(17, 12, 97, 26); } { pwdLabel = new JLabel(); getContentPane().add(pwdLabel); pwdLabel.setFont(font); pwdLabel.setText("/u5bc6/u7801:"); pwdLabel.setBounds(17, 54, 97, 22); } { userJT = new JTextField(); getContentPane().add(userJT); userJT.setText(userAddr); userJT.setBounds(126, 13, 160, 30); } { pwdJP = new JPasswordField(); pwdJP.setText(userPwd); getContentPane().add(pwdJP); pwdJP.setBounds(126, 49, 160, 30); } { logonJB = new JButton(); getContentPane().add(logonJB); logonJB.setBounds(48, 96, 78, 32); logonJB.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent evt) { logonJBMouseReleased(evt); } public void mousePressed(MouseEvent evt) { logonJBMousePressed(evt); } public void mouseExited(MouseEvent evt) { logonJBMouseExited(evt); } public void mouseEntered(MouseEvent evt) { logonJBMouseEntered(evt); } }); logonJB.setIcon(rs.logon); } { logoutJB = new JButton(); getContentPane().add(logoutJB); logoutJB.setIcon(rs.logout); logoutJB.setText("/u6ce8/u9500"); logoutJB.setBounds(160, 96, 78, 32); logoutJB.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { logoutJBMouseClicked(evt); } public void mouseExited(MouseEvent evt) { logoutJBMouseExited(evt); } public void mouseEntered(MouseEvent evt) { logoutJBMouseEntered(evt); } }); } } catch (Exception e) { e.printStackTrace(); } } /* * 登录按钮鼠标事件 */ private void logonJBMouseEntered(MouseEvent evt) { //System.out.println("logonJB.mouseEntered, event="+evt); //TODO add your code for logonJB.mouseEntered this.logonJB.setIcon(rs.logon1); } private void logonJBMouseExited(MouseEvent evt) { //System.out.println("logonJB.mouseExited, event="+evt); //TODO add your code for logonJB.mouseExited this.logonJB.setIcon(rs.logon); } private void logonJBMousePressed(MouseEvent evt) { //System.out.println("logonJB.mousePressed, event="+evt); //TODO add your code for logonJB.mousePressed this.logonJB.setIcon(rs.logon); } private void logonJBMouseReleased(MouseEvent evt) { //System.out.println("logonJB.mouseReleased, event="+evt); //TODO add your code for logonJB.mouseReleased String s = this.userJT.getText(); if(s.contains(".com") && s.contains("@")) { this.logonJB.setIcon(rs.logon1); ms.getFromLabel().setText(this.userJT.getText().trim()); ms.setMailAddr(this.userJT.getText().trim()); ms.setMailPwd(this.pwdJP.getText()); ms.setLoginCheck(true); this.setVisible(false); } else { JOptionPane.showMessageDialog(null, "邮箱格式不合法!"); } } /* * 注销按钮鼠标事件 */ private void logoutJBMouseEntered(MouseEvent evt) { //System.out.println("logoutJB.mouseEntered, event="+evt); //TODO add your code for logoutJB.mouseEntered this.logoutJB.setIcon(rs.logout1); } private void logoutJBMouseExited(MouseEvent evt) { //System.out.println("logoutJB.mouseExited, event="+evt); //TODO add your code for logoutJB.mouseExited this.logoutJB.setIcon(rs.logout); } private void logoutJBMouseClicked(MouseEvent evt) { //System.out.println("logoutJB.mouseClicked, event="+evt); //TODO add your code for logoutJB.mouseClicked this.logoutJB.setIcon(rs.logout1); this.userAddr = ""; this.userPwd = ""; this.userJT.setText(""); this.pwdJP.setText(""); ms.getFromLabel().setText(""); } }
有图有真相: