运行界面如下:
源代码如下:(本程序使用的是Merak mail server)
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
|
package
com.zzk;
/**
* @author 任文超
* @version 1.0
* */
import
java.awt.Color;
import
java.awt.EventQueue;
import
java.awt.Font;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
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.JButton;
import
javax.swing.JFrame;
import
javax.swing.JLabel;
import
javax.swing.JOptionPane;
import
javax.swing.JScrollPane;
import
javax.swing.JTextArea;
import
javax.swing.JTextField;
public
class
SendMailFrame
extends
JFrame {
private
JTextArea ta_text;
private
JTextField tf_title;
private
JTextField tf_send;
private
JTextField tf_receive;
private
Session session;
// 定义Session对象
private
String sendHost =
"localhost"
;
// 定义发送邮件的主机
private
String sendProtocol=
"smtp"
;
// 定义使用的发送协议
public
static
void
main(String args[]) {
EventQueue.invokeLater(
new
Runnable() {
public
void
run() {
try
{
SendMailFrame frame =
new
SendMailFrame();
frame.init();
frame.setVisible(
true
);
}
catch
(Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public
SendMailFrame() {
super
();
setTitle(
"发送邮件窗体"
);
getContentPane().setLayout(
null
);
setBounds(
100
,
100
,
439
,
299
);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final
JLabel label =
new
JLabel();
label.setForeground(
new
Color(
0
,
0
,
255
));
label.setFont(
new
Font(
""
, Font.BOLD,
22
));
label.setText(
"发送电子邮件"
);
label.setBounds(
144
,
10
,
185
,
24
);
getContentPane().add(label);
final
JLabel label_1 =
new
JLabel();
label_1.setText(
"收件人地址:"
);
label_1.setBounds(
22
,
42
,
85
,
18
);
getContentPane().add(label_1);
tf_receive =
new
JTextField();
tf_receive.setBounds(
113
,
40
,
287
,
22
);
getContentPane().add(tf_receive);
final
JLabel label_2 =
new
JLabel();
label_2.setText(
"发件人地址:"
);
label_2.setBounds(
22
,
68
,
78
,
18
);
getContentPane().add(label_2);
tf_send =
new
JTextField();
tf_send.setBounds(
113
,
66
,
287
,
22
);
getContentPane().add(tf_send);
final
JLabel label_3 =
new
JLabel();
label_3.setText(
"主 题:"
);
label_3.setBounds(
32
,
92
,
66
,
18
);
getContentPane().add(label_3);
tf_title =
new
JTextField();
tf_title.setBounds(
113
,
94
,
287
,
22
);
getContentPane().add(tf_title);
final
JLabel label_4 =
new
JLabel();
label_4.setText(
"正 文:"
);
label_4.setBounds(
34
,
128
,
66
,
18
);
getContentPane().add(label_4);
final
JScrollPane scrollPane =
new
JScrollPane();
scrollPane.setBounds(
113
,
128
,
287
,
91
);
getContentPane().add(scrollPane);
ta_text =
new
JTextArea();
scrollPane.setViewportView(ta_text);
final
JButton btn_send =
new
JButton();
btn_send.addActionListener(
new
ActionListener() {
public
void
actionPerformed(
final
ActionEvent e) {
String fromAddr = tf_send.getText().trim();
String toAddr = tf_receive.getText().trim();
// 真实存在的目标邮件地址
String title = tf_title.getText().trim();
String text = ta_text.getText().trim();
try
{
sendMessage(fromAddr, toAddr, title, text);
}
catch
(Exception e1) {
e1.printStackTrace();
}
}
});
btn_send.setText(
"发 送"
);
btn_send.setBounds(
144
,
225
,
78
,
28
);
getContentPane().add(btn_send);
final
JButton btn_exit =
new
JButton();
btn_exit.addActionListener(
new
ActionListener() {
public
void
actionPerformed(
final
ActionEvent e) {
System.exit(
0
);
}
});
btn_exit.setText(
"退 出"
);
btn_exit.setBounds(
279
,
225
,
78
,
28
);
getContentPane().add(btn_exit);
}
public
void
init()
throws
Exception {
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);
// 创建Session对象
}
/**
* @param fromAddr 发送者
* @param toAddr 接收者
* @param title 主题
* @param text 内容
* @throws Exception 异常
*/
public
void
sendMessage(String fromAddr,String toAddr,String title,String text)
throws
Exception {
Message msg =
new
MimeMessage(session);
// 创建Message对象
InternetAddress[] toAddrs = InternetAddress.parse(toAddr,
false
);
// 创建接收方的InternetAddress对象
msg.setRecipients(Message.RecipientType.TO, toAddrs);
// 指定接收方
msg.setSentDate(
new
Date());
// 指定接发送日期
msg.setSubject(title);
// 设置主题
msg.setFrom(
new
InternetAddress(fromAddr));
// 指定发送者
msg.setText(text);
// 指定发送内容
Transport.send(msg);
// 发送邮件
JOptionPane.showMessageDialog(
null
,
"邮件发送成功。"
);
}
}
|
==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2011/10/19/2217431.html,如需转载请自行联系原作者