1.package org.whsvc.pop.mail;
2.
3.import java.io.BufferedReader;
4.import java.io.IOException;
5.import java.io.InputStreamReader;
6.import java.io.PrintStream;
7.import java.io.PrintWriter;
8.import java.net.Socket;
9.import java.net.UnknownHostException;
10.import java.util.Date;
11.import java.util.Vector;
12.
13.public class JMail implements Send {
14.private Socket client = null;
15.private String from_address = null, account = null, password = null,to_address;
16.private StringBuffer body = new StringBuffer();
17.private PrintWriter out = null;
18.private BufferedReader in = null;
19.private String date = new Date().toLocaleString();
20.private String from_name = null, to_name = null, subject = null,
21. content_Type = "Content-Type: text/plain;charset=\"GB2312\"";
22.private boolean isAuthLogin = true;
23.private String serverName = null;
24.private int port = 25;
25.
26.public boolean destory() throws Exception {
27. client.close();
28. in.close();
29. out.close();
30. response("Done");
31. return true;
32.
33.}
34.
35.public JMail(String from_address, String account, String password,
36. String serverName) {
37. super();
38. this.from_address = from_address;
39. this.account = account;
40. this.password = password;
41. this.serverName = serverName;
42. this.from_name = this.from_address;
43.
44.}
45.
46.public JMail() {
47.
48.}
49.
50.public int getPort() {
51. return port;
52.}
53.
54.public void setPort(int port) {
55. this.port = port;
56.}
57.
58.public String getServerName() {
59. return serverName;
60.}
61.
62.public void setServerName(String serverName) {
63. this.serverName = serverName;
64.}
65.
66.public String getFrom_address() {
67. return from_address;
68.}
69.
70.public void setFrom_address(String from_address) {
71. this.from_address = from_address;
72.}
73.
74.
75.public String getTo_address() {
76. return to_address;
77.}
78.
79.public void setTo_address(String to_address) {
80. this.to_address = to_address;
81.}
82.
83.public String getAccount() {
84. return account;
85.}
86.
87.public void setAccount(String account) {
88. this.account = account;
89.}
90.
91.public String getPassword() {
92. return password;
93.}
94.
95.public void setPassword(String password) {
96. this.password = password;
97.}
98.
99.public void addBody(String content) {
100. if (isAuthLogin) {
101.
102.body.append("From: <" + this.from_name + ">\r\n");
103. body.append("To: <" + this.to_name + ">\r\n");
104. body.append("Subject: " + this.subject + "\r\n");
105. body.append(this.content_Type + "\r\n");
106. body.append("\r\n");
107. body.append(content + "\r\n");
108. body.append(".\r\n");
109.
110. }
111.}
112.
113.public String getDate() {
114. return date;
115.}
116.
117.public void setDate(String date) {
118. this.date = date;
119.}
120.
121.public String getFrom_name() {
122. return from_name;
123.}
124.
125.public void setFrom_name(String from_name) {
126. this.from_name = from_name;
127.}
128.
129.public String getTo_name() {
130. return to_name;
131.}
132.
133.public void setTo_name(String to_name) {
134. this.to_name = to_name;
135.}
136.
137.public String getSubject() {
138. return subject;
139.}
140.
141.public void setSubject(String subject) {
142. this.subject = subject;
143.}
144.
145.public String getContent_Type() {
146. return content_Type;
147.}
148.
149.public void setContent_Type(String content_Type) {
150. this.content_Type = content_Type;
151.}
152.
153.public boolean isAuthLogin() {
154. return (isAuthLogin != (this.account == null && this.password == null
155. && this.from_address == null && this.to_address==null
156. && this.serverName == null && this.body.length() > 1));
157.}
158.
159.public void send() throws Exception {
160. if (parse()) {
161. out.println("QUIT");
162. }
163.}
164.
165.private boolean parse() {
166. boolean isReady = false;
167. if (this.account == null && this.password == null
168. && this.from_address == null && this.to_address ==null
169. && this.serverName == null && this.body.length() > 1) {
170. isAuthLogin = false;
171. isReady = false;
172. return isReady;
173. }
174. try {
175. client = new Socket(this.serverName, this.port);
176. out = new PrintWriter(client.getOutputStream());
177. in = new BufferedReader(new InputStreamReader(client
178. .getInputStream()));
179. response(in.readLine());
180. out.println("ehlo MDJ");
181. out.flush();
182. response(in.readLine());
183. out.println("AUTH LOGIN");
184. out.flush();
185. response(in.readLine());
186. response(in.readLine());
187. response(in.readLine());
188. response(in.readLine());
189. out.println(Encode(this.account));
190. out.flush();
191. response(in.readLine());
192. out.println(Encode(this.password));
193. out.flush();
194. response(in.readLine());
195. out.println("mail from: <"+getFrom_address()+">");
196. out.flush();
197. response(in.readLine());
198. out.println("rcpt to: <"+getTo_address()+">");
199. out.flush();
200. response(in.readLine());
201. out.println("data");
202. out.flush();
203. response(in.readLine());
204. /**
205. * out.print("from: <"+this.from_address+">\n");
206. * out.print("to: <"+this.from_name+">");
207. * out.print("Subject: <"+this.from_name+">\n"); out.print("\n\n");
208. */
209. out.println(body.toString());
210. out.flush();
211. response(in.readLine());
212. response(in.readLine());
213. // out.println("<CRLF>.<CRLF>");
214. isReady = true;
215. } catch (UnknownHostException e) {
216. e.printStackTrace();
217. } catch (IOException e) {
218. e.printStackTrace();
219. }
220. return isReady;
221.}
222.
223.public void response(String msg) {
224. System.out.println(msg);
225.
226.}
227.
228.public String Encode(String s) {
229. String old = null;
230. if (s == "" || s.equals("")) {
231. old = "";
232. }
233. try {
234. old = new sun.misc.BASE64Encoder().encode(s.getBytes());
235. } catch (Exception e) {
236. e.printStackTrace();
237. }
238. return old;
239.}
240.
241.}
242.
243.
244.Test.java
245.
246.package org.whsvc.pop.mail;
247.
248.import java.util.Date;
249.
250.public class Test {
251.
252./**
253.* @param args
254.*/
255.public static void main(String[] args)
256.{
257. JMail mail=new JMail();
258. mail.setServerName("smtp.163.com");
259. mail.setDate(new Date().toString());
260. mail.setAccount("mengdejun_520");
261. mail.setFrom_address("mengdejun_520@163.com");
262. mail.setTo_address("280759843@qq.com");
263. mail.setPassword("*****");
264. mail.setFrom_name("mengdejun_520@163.com");
265. mail.setPort(25);
266. mail.setSubject("test");
267. mail.setTo_name("mengdejun_168@126.com");
268. mail.addBody("你好吗?java编程!"+new Date());
269. try
270. {
271. mail.send();
272. mail.destory();
273. } catch (Exception e) {
274. e.printStackTrace();
275. }
276.
277.}
278.
279.
280.}
转载于:https://my.oschina.net/mengdejun/blog/9431