2012年4月2日星期一

java开发_STMP邮箱客户端_发送邮件

java开发_STMP邮箱客户端_发送邮件



这里有一些常见的主流邮箱的收取和发送pop3,stmp服务配置总汇

http://zhiwenweb.cn/jszx/czxt/201009/20179.html

这里只讨论邮件的发送。

1.qq邮箱的stmp服务配置

如果你没有开启stmp服务的话,你使用stmp的时候,管理员会发这样一封邮件到你的邮箱:

********************************************************************

尊敬的QQ邮箱用户:

我们发现您尝试设置SMTP服务,但设置未成功。 您可以检查以下的细节来解决:
您是否在邮箱中开启了这项服务,如果尚未开启,请您在【邮箱设置】的【帐号】中开启相关服务。

您是否设置了邮箱独立密码,如果您设置了独立密码,在客户端设置时,密码输入项需填写邮箱独立密码。
其他可能对您有用的帮助信息:
POP3/SMTP服务设置帮助

QQ邮箱管理员

********************************************************************

开启服务后,这时候我们就可以有stmp服务了。

  1 /*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5 package com.b510.mail;
6
7 import java.io.BufferedReader;
8 import java.io.BufferedWriter;
9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.io.OutputStreamWriter;
12 import java.net.Socket;
13 import java.net.SocketException;
14 import java.net.UnknownHostException;
15 import java.util.StringTokenizer;
16
17 import sun.misc.BASE64Encoder;
18
19 /**
20 * STMP邮箱客户端,用于邮件发送和接收的管理
21 * @author hongten
22 */
23 public class SMTPClient {
24
25 static String from_mail="134******@qq.com";
26 static String to_mail="hongtenzone@foxmail.com";
27 static String server_mail="smtp.qq.com";
28 static String subject_mail="test";
29 static String content_mail="hello,this is a test mail,i'm hongten.\n你好,这是一封测试邮件,我是hongten";
30 static String datafrom_mail=from_mail;
31 static String datato_mail=to_mail;
32 static String user_mail="134******";
33 static String password_mail="********";
34
35
36
37 private boolean debug = true;
38 BASE64Encoder encode = new BASE64Encoder();//用于加密后发送用户名和密码
39
40 public static void main(String[] args) throws UnknownHostException, IOException {
41 MailMessage message = new MailMessage();
42 message.setFrom(from_mail);//发件人
43 message.setTo(to_mail);//收件人
44 String server = server_mail;//邮件服务器
45 message.setSubject(subject_mail);//邮件主题
46 message.setContent(content_mail);//邮件内容
47 message.setDatafrom(datafrom_mail);//发件人,在邮件的发件人栏目中显示
48 message.setDatato(datato_mail);//收件人,在邮件的收件人栏目中显示
49 message.setUser(user_mail);//登陆邮箱的用户名
50 message.setPassword(password_mail);//登陆邮箱的密码
51
52 SMTPClient smtp = new SMTPClient(server, 25);
53
54 boolean flag;
55 flag = smtp.sendMail(message, server);
56
57 if (flag) {
58 System.out.println("邮件发送成功!");
59 } else {
60 System.out.println("邮件发送失败!");
61 }
62 }
63 private Socket socket;
64
65 public SMTPClient(String server, int port) throws UnknownHostException, IOException {
66 try {
67 socket = new Socket(server, 25);
68 } catch (SocketException e) {
69 System.out.println(e.getMessage());
70 } catch (Exception e) {
71 e.printStackTrace();
72 } finally {
73 System.out.println("已经建立连接!");
74 }
75 }
76
77 //注册到邮件服务器
78 public void helo(String server, BufferedReader in, BufferedWriter out) throws IOException {
79 int result;
80 result = getResult(in);
81
82 //连接上邮件服务后,服务器给出220应答
83 if (result != 220) {
84 throw new IOException("连接服务器失败");
85 }
86
87 result = sendServer("HELO " + server, in, out);
88
89 //HELO命令成功后返回250
90 if (result != 250) {
91 throw new IOException("注册邮件服务器失败!");
92 }
93 }
94
95 private int sendServer(String str, BufferedReader in, BufferedWriter out) throws IOException {
96 out.write(str);
97 out.newLine();
98 out.flush();
99
100 if (debug) {
101 System.out.println("已发送命令:" + str);
102 }
103
104 return getResult(in);
105 }
106
107 public int getResult(BufferedReader in) {
108 String line = "";
109
110 try {
111 line = in.readLine();
112 if (debug) {
113 System.out.println("服务器返回状态:" + line);
114 }
115 } catch (Exception e) {
116 e.printStackTrace();
117 }
118
119 //从服务器返回消息中读出状态码,将其转换成整数返回
120 StringTokenizer st = new StringTokenizer(line, " ");
121
122 return Integer.parseInt(st.nextToken());
123 }
124
125 public void authLogin(MailMessage message, BufferedReader in, BufferedWriter out) throws IOException {
126 int result;
127 result = sendServer("AUTH LOGIN", in, out);
128
129 if (result != 334) {
130 throw new IOException("用户验证失败!");
131 }
132 result = sendServer(encode.encode(message.getUser().getBytes()), in, out);
133
134 if (result != 334) {
135 throw new IOException("用户名错误!");
136 }
137 result = sendServer(encode.encode(message.getPassword().getBytes()), in, out);
138
139 if (result != 235) {
140 throw new IOException("验证失败!");
141 }
142 }
143
144 //开始发送消息,邮件源地址
145 public void mailfrom(String source, BufferedReader in, BufferedWriter out) throws IOException {
146 int result;
147 result = sendServer("MAIL FROM:<" + source + ">", in, out);
148
149 if (result != 250) {
150 throw new IOException("指定源地址错误");
151 }
152 }
153
154 // 设置邮件收件人
155 public void rcpt(String touchman, BufferedReader in, BufferedWriter out) throws IOException {
156 int result;
157 result = sendServer("RCPT TO:<" + touchman + ">", in, out);
158
159 if (result != 250) {
160 throw new IOException("指定目的地址错误!");
161 }
162 }
163
164 //邮件体
165 public void data(String from, String to, String subject, String content, BufferedReader in, BufferedWriter out) throws IOException {
166 int result;
167 result = sendServer("DATA", in, out);
168
169 //输入date回车后,若收到354应答后,继续输入邮件内容
170 if (result != 354) {
171 throw new IOException("不能发送数据");
172 }
173
174 out.write("From: " + from);
175 out.newLine();
176 out.write("To: " + to);
177 out.newLine();
178 out.write("Subject: " + subject);
179 out.newLine();
180 out.newLine();
181 out.write(content);
182 out.newLine();
183
184 //句点加回车结束邮件内容输入
185 result = sendServer(".", in, out);
186 System.out.println(result);
187
188 if (result != 250) {
189 throw new IOException("发送数据错误");
190 }
191 }
192
193 //退出
194 public void quit(BufferedReader in, BufferedWriter out) throws IOException {
195 int result;
196 result = sendServer("QUIT", in, out);
197
198 if (result != 221) {
199 throw new IOException("未能正确退出");
200 }
201 }
202
203 //发送邮件主程序
204 public boolean sendMail(MailMessage message, String server) {
205 try {
206 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
207 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
208 helo(server, in, out);//helo
209 authLogin(message, in, out);//auth login
210 mailfrom(message.getFrom(), in, out);//mail from
211 rcpt(message.getTo(), in, out);//rcpt to
212 data(message.getDatafrom(), message.getDatato(), message.getSubject(), message.getContent(), in, out);//DATA
213 quit(in, out);//quit
214 } catch (Exception e) {
215 e.printStackTrace();
216 return false;
217 }
218 return true;
219 }
220 }

运行效果:

http://www.cnblogs.com/hongten/gallery/image/113731.html

2.sina邮箱的stmp服务配置

  1 package com.b510.mail;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.OutputStreamWriter;
8 import java.net.Socket;
9 import java.net.SocketException;
10 import java.net.UnknownHostException;
11 import java.util.StringTokenizer;
12
13 import sun.misc.BASE64Encoder;
14
15 /**
16 * STMP邮箱客户端,用于邮件发送和接收的管理
17 * @author hongten
18 */
19 public class SMTPClient {
20
21 static String from_mail="hongtenzone@sina.com";
22 static String to_mail="hongtenzone@foxmail.com";
23 static String server_mail="smtp.sina.com";
24 static String subject_mail="test";
25 static String content_mail="hello,this is a test mail,i'm hongten.\n你好,这是一封测试邮件,我是hongten";
26 static String datafrom_mail=from_mail;
27 static String datato_mail=to_mail;
28 static String user_mail="hongtenzone";
29 static String password_mail="**********";
30
31
32
33 private boolean debug = true;
34 BASE64Encoder encode = new BASE64Encoder();//用于加密后发送用户名和密码
35
36 public static void main(String[] args) throws UnknownHostException, IOException {
37 MailMessage message = new MailMessage();
38 message.setFrom(from_mail);//发件人
39 message.setTo(to_mail);//收件人
40 String server = server_mail;//邮件服务器
41 message.setSubject(subject_mail);//邮件主题
42 message.setContent(content_mail);//邮件内容
43 message.setDatafrom(datafrom_mail);//发件人,在邮件的发件人栏目中显示
44 message.setDatato(datato_mail);//收件人,在邮件的收件人栏目中显示
45 message.setUser(user_mail);//登陆邮箱的用户名
46 message.setPassword(password_mail);//登陆邮箱的密码
47
48 SMTPClient smtp = new SMTPClient(server, 25);
49
50 boolean flag;
51 flag = smtp.sendMail(message, server);
52
53 if (flag) {
54 System.out.println("邮件发送成功!");
55 } else {
56 System.out.println("邮件发送失败!");
57 }
58 }
59 private Socket socket;
60
61 public SMTPClient(String server, int port) throws UnknownHostException, IOException {
62 try {
63 socket = new Socket(server, 25);
64 } catch (SocketException e) {
65 System.out.println(e.getMessage());
66 } catch (Exception e) {
67 e.printStackTrace();
68 } finally {
69 System.out.println("已经建立连接!");
70 }
71 }
72
73 //注册到邮件服务器
74 public void helo(String server, BufferedReader in, BufferedWriter out) throws IOException {
75 int result;
76 result = getResult(in);
77
78 //连接上邮件服务后,服务器给出220应答
79 if (result != 220) {
80 throw new IOException("连接服务器失败");
81 }
82
83 result = sendServer("HELO " + server, in, out);
84
85 //HELO命令成功后返回250
86 if (result != 250) {
87 throw new IOException("注册邮件服务器失败!");
88 }
89 }
90
91 private int sendServer(String str, BufferedReader in, BufferedWriter out) throws IOException {
92 out.write(str);
93 out.newLine();
94 out.flush();
95
96 if (debug) {
97 System.out.println("已发送命令:" + str);
98 }
99
100 return getResult(in);
101 }
102
103 public int getResult(BufferedReader in) {
104 String line = "";
105
106 try {
107 line = in.readLine();
108 if (debug) {
109 System.out.println("服务器返回状态:" + line);
110 }
111 } catch (Exception e) {
112 e.printStackTrace();
113 }
114
115 //从服务器返回消息中读出状态码,将其转换成整数返回
116 StringTokenizer st = new StringTokenizer(line, " ");
117
118 return Integer.parseInt(st.nextToken());
119 }
120
121 public void authLogin(MailMessage message, BufferedReader in, BufferedWriter out) throws IOException {
122 int result;
123 result = sendServer("AUTH LOGIN", in, out);
124
125 if (result != 334) {
126 throw new IOException("用户验证失败!");
127 }
128 result = sendServer(encode.encode(message.getUser().getBytes()), in, out);
129
130 if (result != 334) {
131 throw new IOException("用户名错误!");
132 }
133 result = sendServer(encode.encode(message.getPassword().getBytes()), in, out);
134
135 if (result != 235) {
136 throw new IOException("验证失败!");
137 }
138 }
139
140 //开始发送消息,邮件源地址
141 public void mailfrom(String source, BufferedReader in, BufferedWriter out) throws IOException {
142 int result;
143 result = sendServer("MAIL FROM:<" + source + ">", in, out);
144
145 if (result != 250) {
146 throw new IOException("指定源地址错误");
147 }
148 }
149
150 // 设置邮件收件人
151 public void rcpt(String touchman, BufferedReader in, BufferedWriter out) throws IOException {
152 int result;
153 result = sendServer("RCPT TO:<" + touchman + ">", in, out);
154
155 if (result != 250) {
156 throw new IOException("指定目的地址错误!");
157 }
158 }
159
160 //邮件体
161 public void data(String from, String to, String subject, String content, BufferedReader in, BufferedWriter out) throws IOException {
162 int result;
163 result = sendServer("DATA", in, out);
164
165 //输入date回车后,若收到354应答后,继续输入邮件内容
166 if (result != 354) {
167 throw new IOException("不能发送数据");
168 }
169
170 out.write("From: " + from);
171 out.newLine();
172 out.write("To: " + to);
173 out.newLine();
174 out.write("Subject: " + subject);
175 out.newLine();
176 out.newLine();
177 out.write(content);
178 out.newLine();
179
180 //句点加回车结束邮件内容输入
181 result = sendServer(".", in, out);
182 System.out.println(result);
183
184 if (result != 250) {
185 throw new IOException("发送数据错误");
186 }
187 }
188
189 //退出
190 public void quit(BufferedReader in, BufferedWriter out) throws IOException {
191 int result;
192 result = sendServer("QUIT", in, out);
193
194 if (result != 221) {
195 throw new IOException("未能正确退出");
196 }
197 }
198
199 //发送邮件主程序
200 public boolean sendMail(MailMessage message, String server) {
201 try {
202 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
203 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
204 helo(server, in, out);//helo
205 authLogin(message, in, out);//auth login
206 mailfrom(message.getFrom(), in, out);//mail from
207 rcpt(message.getTo(), in, out);//rcpt to
208 data(message.getDatafrom(), message.getDatato(), message.getSubject(), message.getContent(), in, out);//DATA
209 quit(in, out);//quit
210 } catch (Exception e) {
211 e.printStackTrace();
212 return false;
213 }
214 return true;
215 }
216 }

运行效果:

http://www.cnblogs.com/hongten/gallery/image/113732.html

3.163邮箱的stmp服务配置

  1 package com.b510.mail;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.OutputStreamWriter;
8 import java.net.Socket;
9 import java.net.SocketException;
10 import java.net.UnknownHostException;
11 import java.util.StringTokenizer;
12
13 import sun.misc.BASE64Encoder;
14
15 /**
16 * STMP邮箱客户端,用于邮件发送和接收的管理
17 * @author hongten
18 */
19 public class SMTPClient {
20
21 static String from_mail="hongtenzoneb@163.com";
22 static String to_mail="hongtenzone@foxmail.com";
23 static String server_mail="smtp.163.com";
24 static String subject_mail="test";
25 static String content_mail="hello,this is a test mail,i'm hongten.\n你好,这是一封测试邮件,我是hongten";
26 static String datafrom_mail=from_mail;
27 static String datato_mail=to_mail;
28 static String user_mail="hongtenzoneb";
29 static String password_mail="**********";
30
31
32
33
34 private boolean debug = true;
35 BASE64Encoder encode = new BASE64Encoder();//用于加密后发送用户名和密码
36
37 public static void main(String[] args) throws UnknownHostException, IOException {
38 MailMessage message = new MailMessage();
39 message.setFrom(from_mail);//发件人
40 message.setTo(to_mail);//收件人
41 String server = server_mail;//邮件服务器
42 message.setSubject(subject_mail);//邮件主题
43 message.setContent(content_mail);//邮件内容
44 message.setDatafrom(datafrom_mail);//发件人,在邮件的发件人栏目中显示
45 message.setDatato(datato_mail);//收件人,在邮件的收件人栏目中显示
46 message.setUser(user_mail);//登陆邮箱的用户名
47 message.setPassword(password_mail);//登陆邮箱的密码
48
49 SMTPClient smtp = new SMTPClient(server, 25);
50
51 boolean flag;
52 flag = smtp.sendMail(message, server);
53
54 if (flag) {
55 System.out.println("邮件发送成功!");
56 } else {
57 System.out.println("邮件发送失败!");
58 }
59 }
60 private Socket socket;
61
62 public SMTPClient(String server, int port) throws UnknownHostException, IOException {
63 try {
64 socket = new Socket(server, 25);
65 } catch (SocketException e) {
66 System.out.println(e.getMessage());
67 } catch (Exception e) {
68 e.printStackTrace();
69 } finally {
70 System.out.println("已经建立连接!");
71 }
72 }
73
74 //注册到邮件服务器
75 public void helo(String server, BufferedReader in, BufferedWriter out) throws IOException {
76 int result;
77 result = getResult(in);
78
79 //连接上邮件服务后,服务器给出220应答
80 if (result != 220) {
81 throw new IOException("连接服务器失败");
82 }
83
84 result = sendServer("HELO " + server, in, out);
85
86 //HELO命令成功后返回250
87 if (result != 250) {
88 throw new IOException("注册邮件服务器失败!");
89 }
90 }
91
92 private int sendServer(String str, BufferedReader in, BufferedWriter out) throws IOException {
93 out.write(str);
94 out.newLine();
95 out.flush();
96
97 if (debug) {
98 System.out.println("已发送命令:" + str);
99 }
100
101 return getResult(in);
102 }
103
104 public int getResult(BufferedReader in) {
105 String line = "";
106
107 try {
108 line = in.readLine();
109 if (debug) {
110 System.out.println("服务器返回状态:" + line);
111 }
112 } catch (Exception e) {
113 e.printStackTrace();
114 }
115
116 //从服务器返回消息中读出状态码,将其转换成整数返回
117 StringTokenizer st = new StringTokenizer(line, " ");
118
119 return Integer.parseInt(st.nextToken());
120 }
121
122 public void authLogin(MailMessage message, BufferedReader in, BufferedWriter out) throws IOException {
123 int result;
124 result = sendServer("AUTH LOGIN", in, out);
125
126 if (result != 334) {
127 throw new IOException("用户验证失败!");
128 }
129 result = sendServer(encode.encode(message.getUser().getBytes()), in, out);
130
131 if (result != 334) {
132 throw new IOException("用户名错误!");
133 }
134 result = sendServer(encode.encode(message.getPassword().getBytes()), in, out);
135
136 if (result != 235) {
137 throw new IOException("验证失败!");
138 }
139 }
140
141 //开始发送消息,邮件源地址
142 public void mailfrom(String source, BufferedReader in, BufferedWriter out) throws IOException {
143 int result;
144 result = sendServer("MAIL FROM:<" + source + ">", in, out);
145
146 if (result != 250) {
147 throw new IOException("指定源地址错误");
148 }
149 }
150
151 // 设置邮件收件人
152 public void rcpt(String touchman, BufferedReader in, BufferedWriter out) throws IOException {
153 int result;
154 result = sendServer("RCPT TO:<" + touchman + ">", in, out);
155
156 if (result != 250) {
157 throw new IOException("指定目的地址错误!");
158 }
159 }
160
161 //邮件体
162 public void data(String from, String to, String subject, String content, BufferedReader in, BufferedWriter out) throws IOException {
163 int result;
164 result = sendServer("DATA", in, out);
165
166 //输入date回车后,若收到354应答后,继续输入邮件内容
167 if (result != 354) {
168 throw new IOException("不能发送数据");
169 }
170
171 out.write("From: " + from);
172 out.newLine();
173 out.write("To: " + to);
174 out.newLine();
175 out.write("Subject: " + subject);
176 out.newLine();
177 out.newLine();
178 out.write(content);
179 out.newLine();
180
181 //句点加回车结束邮件内容输入
182 result = sendServer(".", in, out);
183 System.out.println(result);
184
185 if (result != 250) {
186 throw new IOException("发送数据错误");
187 }
188 }
189
190 //退出
191 public void quit(BufferedReader in, BufferedWriter out) throws IOException {
192 int result;
193 result = sendServer("QUIT", in, out);
194
195 if (result != 221) {
196 throw new IOException("未能正确退出");
197 }
198 }
199
200 //发送邮件主程序
201 public boolean sendMail(MailMessage message, String server) {
202 try {
203 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
204 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
205 helo(server, in, out);//helo
206 authLogin(message, in, out);//auth login
207 mailfrom(message.getFrom(), in, out);//mail from
208 rcpt(message.getTo(), in, out);//rcpt to
209 data(message.getDatafrom(), message.getDatato(), message.getSubject(), message.getContent(), in, out);//DATA
210 quit(in, out);//quit
211 } catch (Exception e) {
212 e.printStackTrace();
213 return false;
214 }
215 return true;
216 }
217 }

运行效果:

http://www.cnblogs.com/hongten/gallery/image/113733.html

4.126邮箱的stmp服务配置

  1 package com.b510.mail;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.OutputStreamWriter;
8 import java.net.Socket;
9 import java.net.SocketException;
10 import java.net.UnknownHostException;
11 import java.util.StringTokenizer;
12
13 import sun.misc.BASE64Encoder;
14
15 /**
16 * STMP邮箱客户端,用于邮件发送和接收的管理
17 * @author hongten
18 */
19 public class SMTPClient {
20
21 static String from_mail="hongtenzoneb510@126.com";
22 static String to_mail="hongtenzone@foxmail.com";
23 static String server_mail="smtp.126.com";
24 static String subject_mail="test";
25 static String content_mail="hello,this is a test mail,i'm hongten.\n你好,这是一封测试邮件,我是hongten";
26 static String datafrom_mail=from_mail;
27 static String datato_mail=to_mail;
28 static String user_mail="hongtenzoneb510";
29 static String password_mail="************";
30
31
32
33
34 private boolean debug = true;
35 BASE64Encoder encode = new BASE64Encoder();//用于加密后发送用户名和密码
36
37 public static void main(String[] args) throws UnknownHostException, IOException {
38 MailMessage message = new MailMessage();
39 message.setFrom(from_mail);//发件人
40 message.setTo(to_mail);//收件人
41 String server = server_mail;//邮件服务器
42 message.setSubject(subject_mail);//邮件主题
43 message.setContent(content_mail);//邮件内容
44 message.setDatafrom(datafrom_mail);//发件人,在邮件的发件人栏目中显示
45 message.setDatato(datato_mail);//收件人,在邮件的收件人栏目中显示
46 message.setUser(user_mail);//登陆邮箱的用户名
47 message.setPassword(password_mail);//登陆邮箱的密码
48
49 SMTPClient smtp = new SMTPClient(server, 25);
50
51 boolean flag;
52 flag = smtp.sendMail(message, server);
53
54 if (flag) {
55 System.out.println("邮件发送成功!");
56 } else {
57 System.out.println("邮件发送失败!");
58 }
59 }
60 private Socket socket;
61
62 public SMTPClient(String server, int port) throws UnknownHostException, IOException {
63 try {
64 socket = new Socket(server, 25);
65 } catch (SocketException e) {
66 System.out.println(e.getMessage());
67 } catch (Exception e) {
68 e.printStackTrace();
69 } finally {
70 System.out.println("已经建立连接!");
71 }
72 }
73
74 //注册到邮件服务器
75 public void helo(String server, BufferedReader in, BufferedWriter out) throws IOException {
76 int result;
77 result = getResult(in);
78
79 //连接上邮件服务后,服务器给出220应答
80 if (result != 220) {
81 throw new IOException("连接服务器失败");
82 }
83
84 result = sendServer("HELO " + server, in, out);
85
86 //HELO命令成功后返回250
87 if (result != 250) {
88 throw new IOException("注册邮件服务器失败!");
89 }
90 }
91
92 private int sendServer(String str, BufferedReader in, BufferedWriter out) throws IOException {
93 out.write(str);
94 out.newLine();
95 out.flush();
96
97 if (debug) {
98 System.out.println("已发送命令:" + str);
99 }
100
101 return getResult(in);
102 }
103
104 public int getResult(BufferedReader in) {
105 String line = "";
106
107 try {
108 line = in.readLine();
109 if (debug) {
110 System.out.println("服务器返回状态:" + line);
111 }
112 } catch (Exception e) {
113 e.printStackTrace();
114 }
115
116 //从服务器返回消息中读出状态码,将其转换成整数返回
117 StringTokenizer st = new StringTokenizer(line, " ");
118
119 return Integer.parseInt(st.nextToken());
120 }
121
122 public void authLogin(MailMessage message, BufferedReader in, BufferedWriter out) throws IOException {
123 int result;
124 result = sendServer("AUTH LOGIN", in, out);
125
126 if (result != 334) {
127 throw new IOException("用户验证失败!");
128 }
129 result = sendServer(encode.encode(message.getUser().getBytes()), in, out);
130
131 if (result != 334) {
132 throw new IOException("用户名错误!");
133 }
134 result = sendServer(encode.encode(message.getPassword().getBytes()), in, out);
135
136 if (result != 235) {
137 throw new IOException("验证失败!");
138 }
139 }
140
141 //开始发送消息,邮件源地址
142 public void mailfrom(String source, BufferedReader in, BufferedWriter out) throws IOException {
143 int result;
144 result = sendServer("MAIL FROM:<" + source + ">", in, out);
145
146 if (result != 250) {
147 throw new IOException("指定源地址错误");
148 }
149 }
150
151 // 设置邮件收件人
152 public void rcpt(String touchman, BufferedReader in, BufferedWriter out) throws IOException {
153 int result;
154 result = sendServer("RCPT TO:<" + touchman + ">", in, out);
155
156 if (result != 250) {
157 throw new IOException("指定目的地址错误!");
158 }
159 }
160
161 //邮件体
162 public void data(String from, String to, String subject, String content, BufferedReader in, BufferedWriter out) throws IOException {
163 int result;
164 result = sendServer("DATA", in, out);
165
166 //输入date回车后,若收到354应答后,继续输入邮件内容
167 if (result != 354) {
168 throw new IOException("不能发送数据");
169 }
170
171 out.write("From: " + from);
172 out.newLine();
173 out.write("To: " + to);
174 out.newLine();
175 out.write("Subject: " + subject);
176 out.newLine();
177 out.newLine();
178 out.write(content);
179 out.newLine();
180
181 //句点加回车结束邮件内容输入
182 result = sendServer(".", in, out);
183 System.out.println(result);
184
185 if (result != 250) {
186 throw new IOException("发送数据错误");
187 }
188 }
189
190 //退出
191 public void quit(BufferedReader in, BufferedWriter out) throws IOException {
192 int result;
193 result = sendServer("QUIT", in, out);
194
195 if (result != 221) {
196 throw new IOException("未能正确退出");
197 }
198 }
199
200 //发送邮件主程序
201 public boolean sendMail(MailMessage message, String server) {
202 try {
203 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
204 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
205 helo(server, in, out);//helo
206 authLogin(message, in, out);//auth login
207 mailfrom(message.getFrom(), in, out);//mail from
208 rcpt(message.getTo(), in, out);//rcpt to
209 data(message.getDatafrom(), message.getDatato(), message.getSubject(), message.getContent(), in, out);//DATA
210 quit(in, out);//quit
211 } catch (Exception e) {
212 e.printStackTrace();
213 return false;
214 }
215 return true;
216 }
217 }

运行效果 :

http://www.cnblogs.com/hongten/gallery/image/113734.html



TAG:java stmp mail 126 com 163 com qq com sina com