Biblioteca Java - Blame information for rev 3
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
3 | mihai | 1 | package lab.scd.net.smtp_client; |
2 | /* | ||
3 | * SMTPClient.java | ||
4 | */ | ||
5 | |||
6 | /** | ||
7 | * Class created by @author Mihai HULEA at Feb 25, 2005. | ||
8 | * | ||
9 | * This class is part of the laborator2_net project. | ||
10 | * | ||
11 | */ | ||
12 | import javax.mail.*; | ||
13 | import javax.mail.internet.*; | ||
14 | |||
15 | import java.util.*; | ||
16 | |||
17 | import javax.swing.*; | ||
18 | import java.awt.event.*; | ||
19 | import java.awt.*; | ||
20 | |||
21 | public class SMTPClient extends JFrame { | ||
22 | |||
23 | private JButton sendButton = new JButton("Send Message"); | ||
24 | private JLabel fromLabel = new JLabel("From: "); | ||
25 | private JLabel toLabel = new JLabel("To: "); | ||
26 | private JLabel hostLabel = new JLabel("SMTP Server: "); | ||
27 | private JLabel subjectLabel = new JLabel("Subject: "); | ||
28 | private JTextField fromField = new JTextField(40); | ||
29 | private JTextField toField = new JTextField(40); | ||
30 | private JTextField hostField = new JTextField(40); | ||
31 | private JTextField subjectField = new JTextField(40); | ||
32 | private JTextArea message = new JTextArea(40, 72); | ||
33 | private JScrollPane jsp = new JScrollPane(message); | ||
34 | |||
35 | public SMTPClient( ) { | ||
36 | |||
37 | super("SMTP Client"); | ||
38 | |||
39 | Container contentPane = this.getContentPane( ); | ||
40 | contentPane.setLayout(new BorderLayout( )); | ||
41 | |||
42 | JPanel labels = new JPanel( ); | ||
43 | labels.setLayout(new GridLayout(4, 1)); | ||
44 | labels.add(hostLabel); | ||
45 | |||
46 | JPanel fields = new JPanel( ); | ||
47 | fields.setLayout(new GridLayout(4, 1)); | ||
48 | |||
49 | String host = System.getProperty("mail.host", ""); | ||
50 | hostField.setText(host); | ||
51 | fields.add(hostField); | ||
52 | labels.add(toLabel); | ||
53 | fields.add(toField); | ||
54 | String from = System.getProperty("mail.from", ""); | ||
55 | fromField.setText(from); | ||
56 | labels.add(fromLabel); | ||
57 | fields.add(fromField); | ||
58 | labels.add(subjectLabel); | ||
59 | fields.add(subjectField); | ||
60 | Box north = Box.createHorizontalBox( ); | ||
61 | north.add(labels); | ||
62 | north.add(fields); | ||
63 | contentPane.add(north, BorderLayout.NORTH); | ||
64 | message.setFont(new Font("Monospaced", Font.PLAIN, 12)); | ||
65 | contentPane.add(jsp, BorderLayout.CENTER); | ||
66 | JPanel south = new JPanel( ); | ||
67 | south.setLayout(new FlowLayout(FlowLayout.CENTER)); | ||
68 | south.add(sendButton); | ||
69 | sendButton.addActionListener(new SendAction( )); | ||
70 | contentPane.add(south, BorderLayout.SOUTH); | ||
71 | this.pack( ); | ||
72 | |||
73 | } | ||
74 | |||
75 | class SendAction implements ActionListener { | ||
76 | |||
77 | public void actionPerformed(ActionEvent evt) { | ||
78 | |||
79 | try { | ||
80 | Properties props = new Properties( ); | ||
81 | props.put("mail.host", hostField.getText( )); | ||
82 | Session mailConnection = Session.getInstance(props, null); | ||
83 | final Message msg = new MimeMessage(mailConnection); | ||
84 | Address to = new InternetAddress(toField.getText( )); | ||
85 | Address from = new InternetAddress(fromField.getText( )); | ||
86 | msg.setContent(message.getText( ), "text/plain"); | ||
87 | msg.setFrom(from); | ||
88 | msg.setRecipient(Message.RecipientType.TO, to); | ||
89 | msg.setSubject(subjectField.getText( )); | ||
90 | |||
91 | /** | ||
92 | * Start un nou fir pentru a trimite emailul. | ||
93 | */ | ||
94 | Runnable r = new Runnable( ) { | ||
95 | public void run( ) { | ||
96 | try { | ||
97 | Transport.send(msg); | ||
98 | } | ||
99 | catch (Exception e) { | ||
100 | e.printStackTrace( ); | ||
101 | } | ||
102 | } | ||
103 | }; | ||
104 | |||
105 | Thread t = new Thread(r); | ||
106 | t.start( ); | ||
107 | message.setText(""); | ||
108 | } | ||
109 | catch (Exception e) { | ||
110 | e.printStackTrace( ); | ||
111 | } | ||
112 | |||
113 | }//.end method | ||
114 | |||
115 | }//.class | ||
116 | |||
117 | public static void main(String[] args) { | ||
118 | SMTPClient client = new SMTPClient( ); | ||
119 | client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
120 | client.setVisible(true); | ||
121 | } | ||
122 | |||
123 | } |