import java.awt.*; import java.io.*; import java.util.Properties; //import java.security.MessageDigest; import java.awt.event.*; import javax.swing.*; public final class SendMail extends JFrame { //采用继承JFrame方式设计窗体,调用更加方便 /** * serialVersionUID是一个标志,解决不同的版本之间的串行话问题 */ private static final long serialVersionUID = 1L; /** * @param args */ boolean frameSizeAdjusted = false; JLabel JLabel1=new JLabel(); JLabel JLabel2=new JLabel(); JLabel JLabel3=new JLabel(); JLabel JLabel4=new JLabel(); JLabel JLabel5=new JLabel(); JTextField _pwd=new JTextField(); JTextField _from=new JTextField(); JTextField _to=new JTextField(); JTextField _subject=new JTextField(); JTextField _smtp=new JTextField(); JScrollPane _scrollPane=new JScrollPane(); JScrollPane _scrollPane2=new JScrollPane(); JTextArea _body=new JTextArea(); JButton Send=new JButton(); JButton Cancel=new JButton(); JList _output=new JList(); DefaultListModel _model=new DefaultListModel(); BufferedReader _in; PrintWriter _out; public SendMail() { setTitle("SendMail Power by Hector"); Container panel=getContentPane(); panel.setLayout(null); setSize(780,340); setVisible(false); JLabel1.setText("From:"); JLabel2.setText("Password:"); JLabel3.setText("To:"); JLabel4.setText("Subject:"); JLabel5.setText("SMTP Sever:"); panel.add(JLabel1); panel.add(JLabel2); panel.add(JLabel3); panel.add(JLabel4); panel.add(JLabel5); JLabel1.setBounds(12, 12, 36, 12); JLabel2.setBounds(12, 48, 84, 12); JLabel3.setBounds(12, 84, 48, 12); JLabel4.setBounds(12, 120, 84, 12); JLabel5.setBounds(12, 156, 84, 12); panel.add(_from); _from.setBounds(96, 12, 300, 24); panel.add(_pwd); _pwd.setBounds(96, 48, 300, 24); panel.add(_to); _to.setBounds(96, 84, 300, 24); panel.add(_subject); _subject.setBounds(96, 120, 300, 24); panel.add(_smtp); _smtp.setBounds(96, 156, 300, 24); panel.add(_scrollPane2); _scrollPane2.setBounds(12,192,384,108); _body.setText("Enter your message here."); _scrollPane2.getViewport().add(_body); _body.setBounds(0, 0, 381, 105); Send.setText("Send"); Send.setBounds(60, 312, 132, 24); panel.add(Send); Cancel.setText("Cancel"); Cancel.setBounds(216, 312, 120, 24); panel.add(Cancel); _scrollPane.setBounds(408, 12, 348, 288); panel.add(_scrollPane); _output.setBounds(408, 12, 345, 285); panel.add(_output); //按钮加入监听器,具体实现在监听器里面判断事件源 SymAction lSymAction=new SymAction(); Send.addActionListener(lSymAction); Cancel.addActionListener(lSymAction); _output.setModel(_model); _model.addElement("Sever out displayed here:"); _scrollPane.getViewport().setView(_output); _scrollPane2.getViewport().setView(_body); } public void setVisible(boolean b) { if(b) setLocation(50,50); super.setVisible(b); } @SuppressWarnings("deprecation") public static void main(String[] args) { // TODO Auto-generated method stub SendMail mymail=new SendMail(); mymail.show(); //(new SendMail()).show(); } public void addNotify()//窗口调整函数而已 { Dimension size=getSize(); super.addNotify(); if(frameSizeAdjusted) return; frameSizeAdjusted = true; Insets insets = getInsets(); JMenuBar menuBar = getRootPane().getJMenuBar(); int menuBarHeight = 0; if( menuBar != null) menuBarHeight = menuBar.getPreferredSize().height; setSize(insets.left + insets.right + size.width,insets.top + insets.bottom +size.height+menuBarHeight); } class SymAction implements ActionListener //通过接口实现监听类 { public void actionPerformed(ActionEvent event) { Object object = event.getSource(); //获取事件发生源,进行不同操作 if(object == Send) Send_actionPerformed(event); else if( object == Cancel) Cancel_actionPerformed(event); } } protected void send(String s) throws IOException //异常由调用者抛出 { if(s!=null) { _model.addElement("C:"+s); _out.print(s); //在侧边栏输出发送信息 _out.flush(); //强制缓冲区输出 } String line = _in.readLine(); if(line!=null) { _model.addElement("S:"+line); //输出服务器返回信息 } else { _model.addElement("error"); } } void Send_actionPerformed(ActionEvent event) { try { java.net.Socket s=new java.net.Socket(_smtp.getText(),25); //创建套接字,SMTP默认25端口 _out=new PrintWriter(s.getOutputStream()); //实例化输入输出流 _in=new BufferedReader(new InputStreamReader(s.getInputStream())); send(null); send("HELO " + java.net.InetAddress.getLocalHost().getHostName()+"\r\n");//noly /*用户验证,发送base64加密的用户名密码*/ send("AUTH LOGIN"+"\r\n");//only send((new sun.misc.BASE64Encoder()).encode(_from.getText().getBytes())+"\r\n");//only send((new sun.misc.BASE64Encoder()).encode( _pwd.getText().getBytes())+"\r\n");//only send("MAIL FROM: " + _from.getText()+"\r\n");//only send("RCPT TO: " + _to.getText()+"\r\n"); send("DATA"+"\r\n"); /*输出邮件信息,注意这里内容可以伪造*/ _out.print("cc:" + "这里是抄送内容"+"\r\n"); _out.print("From:" + "来自邮箱"+"\r\n"); _out.print("to:" + "发送的邮件地"+"\r\n"); _out.print("date:" + "时间"+"\r\n"); _out.print("Subject:" + _subject.getText()+"\r\n"); /******************************** * 邮件信息发送完毕,开始发送正文。 * 运用MIME(Multipurpose Internet Mail Extensions,多功能Internet 邮件扩充服务)实现发送各种格式的邮件。 * 个人时间有限,此实例暂时只加入发送html文件。 ****************************** */ _out.print("X-Mailer:Hector's mail\r\n" //先发送信息头 +"MIMI-Version:1.0\r\n"+"Content-Type:multipart/mixed;boundary=\"#BOUNDARY#\"\r\n\r\n" +"Content-Transfer-Encoding:7bit\r\n\r\n" +"This is a multi-part message in MIME format\r\n\r\n" +"--#BOUNDARY#\r\n" //一个分段标志而已,便于分段发送各种数据 +"Content-Type: text/html;charset=gb2312\r\n" +"Content-Transfer-Encoding:printable\r\n\r\n" +_body.getText()+"\r\n" //html正文 ); send("\r\n"+"."+"\r\n"); send("quit"+"\r\n"); s.close(); } catch(Exception e) { _model.addElement("Error" + e); //输出获取的异常 } } void Cancel_actionPerformed(ActionEvent event) { System.exit(0); //正常退出而已 } }