JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 程式分享區 » Networking  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Re:[分享]簡單的SMTP客戶端 [Re:caterpillar]
weaker

小屁屁∼



發文: 379
積分: 4
於 2004-03-01 18:30 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
使用 javax.mail 套件也來寫一個簡單的 client SMTP
不過畫面滿差的 XD
支援多人發信,個收件人以 "," 分隔(圖中為錯誤示範)
並且也支援 attachment.html 語法
不過沒有去加上斷行的設定 :p
以下為 code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//: MailTest.java
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.activation.*;
 
public class MailTest extends JFrame implements ActionListener {
  
  JFileChooser fileChooser = null;
  JTextField attachmentName = null;
  JTextField mailServer = null;
  JTextField mailFrom = null;
  JTextField mailTo = null;
  JTextField mailSubject = null;
  JTextArea msg = null;
  
  public MailTest() {
    super("MailTest");
    setMailForm();
    setMessageArea();
    setAttachmentFiled();
    
  }
  
  public void setMailForm() {
    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints cons = new GridBagConstraints();
    JLabel labelforServer = new JLabel("MailServer : ");
    mailServer = new JTextField(50);
    JLabel labelforFrom = new JLabel("From : ");
    mailFrom = new JTextField(50);
    JLabel labelforTo = new JLabel("To : ");
    mailTo = new JTextField(50);        
    JButton sendButton = new JButton("Send");
    sendButton.addActionListener(this);
    JLabel labelforSubject = new JLabel("Subject : ");
    mailSubject = new JTextField(50);    
    Insets inset = new Insets(2,2,2,2);
    cons.insets = inset;
    cons.gridx = 0; cons.gridy = 0;
    panel.add(labelforServer,cons);
    cons.gridx = 1; cons.gridwidth = 2;
    panel.add(mailServer,cons);
    cons.gridx = 0; cons.gridy = 1;
    panel.add(labelforFrom,cons);
    cons.gridx = 1;
    panel.add(mailFrom,cons);
    cons.gridx = 0; cons.gridy = 2;
    panel.add(labelforTo,cons);
    cons.gridx = 1;
    panel.add(mailTo,cons);    
    cons.gridx = 0; cons.gridy = 3;
    panel.add(sendButton,cons);    
    cons.gridx = 0; cons.gridy = 4;
    panel.add(labelforSubject,cons);
    cons.gridx = 1;
    panel.add(mailSubject,cons);              
    getContentPane().add(panel,BorderLayout.NORTH);    
  }
  
  public void setMessageArea() {
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.setBorder(new TitledBorder(
                          new LineBorder(Color.black),"Content"));
    msg = new JTextArea();
    JScrollPane js = new JScrollPane(msg);
    panel.add(js,BorderLayout.CENTER);
    getContentPane().add(panel,BorderLayout.CENTER);
  }
  
  public void setAttachmentFiled() {
    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints cons = new GridBagConstraints();
    JLabel labelforAttachment = new JLabel("Attachment : ");
    attachmentName = new JTextField(50);
    JButton openFile = new JButton("Attachment");
    openFile.addActionListener(this);
    Insets inset = new Insets(2,2,2,2);
    cons.insets = inset;
    cons.gridx = 0; cons.gridy = 0;
    panel.add(labelforAttachment,cons);
    cons.gridx = 1; cons.gridwidth = 2;
    cons.fill = GridBagConstraints.BOTH;
    panel.add(attachmentName,cons);
    cons.gridx = 3; cons.gridwidth = 1;
    cons.fill = GridBagConstraints.NONE;
    panel.add(openFile,cons);
    getContentPane().add(panel,BorderLayout.SOUTH);
    
  }
  
  public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Attachment")) {
      fileChooser = new JFileChooser();
      fileChooser.addActionListener(this);
      int choose = fileChooser.showOpenDialog(this);
      if (choose == JFileChooser.APPROVE_OPTION) {
        attachmentName.setText(fileChooser.getSelectedFile().toString());
      }
    }
    
    if (e.getActionCommand().equals("Send")) {
      new Thread() {
        public void run() {
          try {
            Properties props = System.getProperties();
            String host = mailServer.getText();
            String from = mailFrom.getText();
            String to = mailTo.getText();
            String contentMsg = msg.getText();
            props.put("mail.smtp.host",host);
            Session mailSession = Session.getDefaultInstance(props,null);
            mailSession.setDebug(true);
            Message message = new MimeMessage(mailSession);
            //StringTokenizer st = new StringTokenizer(mailTo.getText().trim(),";");
            InternetAddress[] address = InternetAddress.parse(mailTo.getText().trim());
            message.setFrom(new InternetAddress(mailFrom.getText().trim()));
            message.addRecipients(Message.RecipientType.TO,address);
            message.setSubject(mailSubject.getText()); // set Subject
            
            // part 1 : set content msg
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent(contentMsg,"text/html;charset=big5");
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
                    
            // part 2  : set attachment file
            if (attachmentName.getText().length()!=0) {
              messageBodyPart = new MimeBodyPart();
              DataSource Source = new FileDataSource(attachmentName.getText());
              messageBodyPart.setDataHandler(new DataHandler(Source));
              messageBodyPart.setFileName(
                        attachmentName.getText().substring(
                            attachmentName.getText().lastIndexOf("\\")));
              multipart.addBodyPart(messageBodyPart);
            }
            message.setContent(multipart);
            Transport.send(message);
          }catch (Exception ignored) {}
        }
      }.start();
    }
  }
  
  public static void main(String[] args) {
    JFrame f = new MailTest();
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      SwingUtilities.updateComponentTreeUI(f);
    } catch (Exception ignored) {}
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setBounds(100,100,600,400);
    f.show();
  }
  
} //:~



reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
13233 [網路]簡單的SMTP客戶端 caterpillar 6425 2004-02-26 12:49
10104 Re:[分享]簡單的SMTP客戶端 weaker 11022 2004-03-01 18:30
5713 Re:[分享]簡單的SMTP客戶端有 Myles1980 140 2006-05-23 13:30
» JWorld@TW »  Java 程式分享區 » Networking

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8