当前位置:编程学习 > JAVA >>

socket实现邮件发送

[java] 
通过java的网络编程特性,完成邮件发送的功能,效果图如下:


首先新建一个maiTest的java项目,


然后新建GBC.java和MailTest.java文件


GBC.java代码如下:

[java] 
package ch02;   www.zzzyk.com
 
import java.awt.*; 
 
 
public class GBC extends GridBagConstraints  

  
   public GBC(int gridx, int gridy) 
   { 
      this.gridx = gridx; 
      this.gridy = gridy; 
   } 
 
 
   public GBC(int gridx, int gridy, int gridwidth, int gridheight) 
   { 
      this.gridx = gridx; 
      this.gridy = gridy; 
      this.gridwidth = gridwidth;  
      this.gridheight = gridheight;  
   } 
 
 
   public GBC setAnchor(int anchor)  
   {  
      this.anchor = anchor;  
      return this; 
   } 
    
 
   public GBC setFill(int fill)  
   {  
      this.fill = fill;  
      return this; 
   } 
 
 
   public GBC setWeight(double weightx, double weighty)  
   {  
      this.weightx = weightx;  
      this.weighty = weighty;  
      return this; 
   } 
 
 
   public GBC setInsets(int distance)  
   {  
      this.insets = new Insets(distance, distance, distance, distance); 
      return this; 
   } 
 
 
   public GBC setInsets(int top, int left, int bottom, int right)  
   {  
      this.insets = new Insets(top, left, bottom, right); 
      return this; 
   } 
 
 
   public GBC setIpad(int ipadx, int ipady)  
   {  
      this.ipadx = ipadx;  
      this.ipady = ipady;  
      return this; 
   } 

MailTest.java的代码如下:
[java] 
package ch02; 
 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import java.net.*; 
import java.io.*; 
import javax.swing.*; 
 
 
public class MailTest 

   public static void main(String[] args) 
   { 
      EventQueue.invokeLater(new Runnable() 
         { 
            public void run() 
            { 
               JFrame frame = new MailTestFrame(); 
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
               frame.setVisible(true); 
            } 
         }); 
   } 

 
 
class MailTestFrame extends JFrame 

   public MailTestFrame() 
   { 
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 
      setTitle("MailTest"); 
 
      setLayout(new GridBagLayout()); 
 
      add(new JLabel("From:"), new GBC(0, 0).setFill(GBC.HORIZONTAL)); 
 
      from = new JTextField(20); 
      add(from, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(100, 0)); 
 
      add(new JLabel("To:"), new GBC(0, 1).setFill(GBC.HORIZONTAL)); 
 
      to = new JTextField(20); 
      add(to, new GBC(1, 1).setFill(GBC.HORIZONTAL).setWeight(100, 0)); 
 
      add(new JLabel("SMTP server:"), new GBC(0, 2).setFill(GBC.HORIZONTAL)); 
 
      smtpServer = new JTextField(20); 
      add(smtpServer, new GBC(1, 2).setFill(GBC.HORIZONTAL).setWeight(100, 0)); 
 
      message = new JTextArea(); 
      add(new JScrollPane(message), new GBC(0, 3, 2, 1).setFill(GBC.BOTH).setWeight(100, 100)); 
 
      comm = new JTextArea(); 
      add(new JScrollPane(comm), new GBC(0, 4, 2, 1).setFill(GBC.BOTH).setWeight(100, 100)); 
 
      JPanel buttonPanel = new JPanel(); 
      add(buttonPanel, new GBC(0, 5, 2, 1)); 
 
      JButton sendButton = new JButton("Send"); 
      buttonPanel.add(sendButton); 
      sendButton.addActionListener(new ActionListener() 
         { 
            public void actionPerformed(ActionEvent event) 
            { 
//               new SwingWorker<Void, Void>() 
//               { 
//                  protected Void doInBackground() throws Exception 
//                  { 
//                     comm.setText(""); 
//                     sendMail(); 
//                   &n

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,