/* 1.merak郵件軟件安裝 2包 3圖 項目改進方向 添加附件 改成桌面exe作特別的客戶端 進行接受郵件的開發 網頁版本練習 */ package jgh; import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.border.Border; import java.awt.Toolkit; public class MailServer extends JFrame implements ActionListener{ private static String sendProtocol = "smtp"; private static String sendHost = "localhost"; private static Session session=null; private static JTextField [] jtxs = new JTextField[3]; private static JLabel[] Jlbs = new JLabel[5]; private static JLabel bglab; private static JButton[] Jbus = new JButton[2]; private static JTextArea jta; private static ImageIcon sendImg,outImg,bgImg; public MailServer(){ init(); } //發送圖形界面 public void sendView(){ this.setLayout(null); this.setTitle("郵件發送服務"); this.setBounds((GetSize.Swidth-650)/2, (GetSize.Sheight-400)/2, 650, 400); bglab = new JLabel(); bglab.setBounds(0, 0, 650, 400); sendImg = new ImageIcon("p_w_picpaths/send.png"); outImg = new ImageIcon("p_w_picpaths/out.png"); bgImg = new ImageIcon("p_w_picpaths/bg.jpg"); bglab.setIcon(bgImg); bglab.setOpaque(false); for(int i =0;i<3;i++){ jtxs[i] = new JTextField(); jtxs[i].setBounds(200, 40+i*45, 350, 35); jtxs[i].setBorder(BorderFactory.createEmptyBorder()); } for(int i =0;i<5;i++){ Jlbs[i] = new JLabel(); Jlbs[i].setForeground(Color.WHITE); } Jbus[0] = new JButton(); Jbus[0].addActionListener(this); Jbus[0].setActionCommand("send"); Jbus[0].setIcon(sendImg); Jbus[1] = new JButton(); Jbus[1].addActionListener(this); Jbus[1].setActionCommand("out"); Jbus[1].setIcon(outImg); Jbus[0].setBounds(240, 290, 50, 60); Jbus[1].setBounds(430, 290, 50, 60); Jbus[0].setOpaque(false); Jbus[0].setBackground(Color.lightGray); Jbus[0].setBorder(BorderFactory.createEmptyBorder()); Jbus[1].setBorder(BorderFactory.createEmptyBorder()); Jbus[1].setOpaque(false); Jbus[1].setBackground(Color.lightGray); Jlbs[1].setBounds(100, 50, 100, 20); Jlbs[1].setText("收件人地址:"); Jlbs[2].setBounds(100, 92, 100, 20); Jlbs[2].setText("發件人地址:"); Jlbs[3].setBounds(100, 140, 100, 20); Jlbs[3].setText("主 題:"); Jlbs[4].setBounds(100, 185, 100, 20); Jlbs[4].setText("正 文:"); jta = new JTextArea(); jta.setBounds(200, 180, 350, 90); bglab.add(jtxs[0]); bglab.add(jtxs[1]); bglab.add(jtxs[2]); bglab.add(Jlbs[0]); bglab.add(Jlbs[1]); bglab.add(Jlbs[2]); bglab.add(Jlbs[3]); bglab.add(Jlbs[4]); bglab.add(Jbus[0]); bglab.add(Jbus[1]); bglab.add(jta); this.add(bglab); this.setVisible(true); this.setResizable(false); } //初始化方法,用於建立Session對象 public static void init(){ Properties props = new Properties();//建立屬性對象 props.put("mail.transport.protocol",sendProtocol);//指定郵件傳輸協議 props.put("mail.smtp.class", "com.sun.mail.smtp.SMTPTransport");//指定傳輸協議使用的類 props.put("mail.smtp.host", sendHost);//指定發送主機郵件的主機 session = Session.getDefaultInstance(props); } //發送郵件 public void sendMessage(String formAddr,String toAddr,String title,String text) throws Exception{ Message msg = new MimeMessage(session); InternetAddress[] toAddrs = InternetAddress.parse(toAddr, false);//建立就收放的InternetAddress對象 msg.setRecipients(Message.RecipientType.TO,toAddrs);//指定接收方 msg.setSentDate(new Date()); msg.setSubject(title); msg.setFrom(new InternetAddress(formAddr)); msg.setText(text); Transport.send(msg); JOptionPane.showMessageDialog(null, "郵件發送出成功!"); } public static void main(String[] args) { new MailServer().sendView(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub JButton jb = (JButton) e.getSource(); if(jb.getActionCommand().equals("send")){ String formAddr = null; try { formAddr = new String(jtxs[1].getText().getBytes("GBK"),"UTF-8"); } catch (UnsupportedEncodingException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } String toAddr = jtxs[0].getText(); String title = jtxs[2].getText(); String text = jta.getText(); try { sendMessage(formAddr,toAddr,title,text); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }else{ this.dispose(); } } } class GetSize { public static int Swidth; public static int Sheight; static{ Swidth = Toolkit.getDefaultToolkit().getScreenSize().width; Sheight = Toolkit.getDefaultToolkit().getScreenSize().height; } } /* mail to me w22z@qq.com you wile gain the jar and merak */