java smtp.126.com_java开发_STMP邮箱客户端_发送邮件
这里有一些常见的主流邮箱的收取和发送pop3,stmp服务配置总汇
这里只讨论邮件的发送。
1.qq邮箱的stmp服务配置
如果你没有开启stmp服务的话,你使用stmp的时候,管理员会发这样一封邮件到你的邮箱:
********************************************************************
尊敬的QQ邮箱用户:
我们发现您尝试设置SMTP服务,但设置未成功。 您可以检查以下的细节来解决:
您是否在邮箱中开启了这项服务,如果尚未开启,请您在【邮箱设置】的【帐号】中开启相关服务。
您是否设置了邮箱独立密码,如果您设置了独立密码,在客户端设置时,密码输入项需填写邮箱独立密码。
其他可能对您有用的帮助信息:
POP3/SMTP服务设置帮助
QQ邮箱管理员
********************************************************************
MailMessage.java
1 packagecom.b510.mail;2
3 /**
4 * 邮件信息5 *6 *@authorHongten7 *8 */
9 public classMailMessage {10 /**
11 * 发件人12 */
13 privateString from;14 /**
15 * 收件人16 */
17 privateString to;18 /**
19 * 发件人,在邮件的发件人栏目中显示20 */
21 privateString datafrom;22 /**
23 * 收件人,在邮件的收件人栏目中显示24 */
25 privateString datato;26 /**
27 * 邮件主题28 */
29 privateString subject;30 /**
31 * 邮件内容32 */
33 privateString content;34 /**
35 * 发送日期36 */
37 privateString date;38 /**
39 * 登陆邮箱的用户名40 */
41 privateString user;42 /**
43 * 登陆邮箱的密码44 */
45 privateString password;46
47 /**
48 * 获取发件人49 *50 *@return发件人51 */
52 publicString getFrom() {53 returnfrom;54 }55
56 /**
57 * 设置发件人58 *59 *@paramfrom60 * 发件人61 */
62 public voidsetFrom(String from) {63 this.from =from;64 }65
66 /**
67 * 获取收件人68 *69 *@return收件人70 */
71 publicString getTo() {72 returnto;73 }74
75 /**
76 * 设置收件人77 *78 *@paramto79 * 收件人80 */
81 public voidsetTo(String to) {82 this.to =to;83 }84
85 /**
86 * 获取发件人,在邮件的发件人栏目中显示87 *88 *@return发件人,在邮件的发件人栏目中显示89 */
90 publicString getDatafrom() {91 returndatafrom;92 }93
94 /**
95 * 设置发件人,在邮件的发件人栏目中显示96 *97 *@paramdatafrom98 * 发件人,在邮件的发件人栏目中显示99 */
100 public voidsetDatafrom(String datafrom) {101 this.datafrom =datafrom;102 }103
104 /**
105 * 获取收件人,在邮件的收件人栏目中显示106 *107 *@return收件人,在邮件的收件人栏目中显示108 */
109 publicString getDatato() {110 returndatato;111 }112
113 /**
114 * 设置收件人,在邮件的收件人栏目中显示115 *116 *@paramdatato117 * 收件人,在邮件的收件人栏目中显示118 */
119 public voidsetDatato(String datato) {120 this.datato =datato;121 }122
123 /**
124 * 获取邮件主题125 *126 *@return邮件主题127 */
128 publicString getSubject() {129 returnsubject;130 }131
132 /**
133 * 设置邮件主题134 *135 *@paramsubject136 * 邮件主题137 */
138 public voidsetSubject(String subject) {139 this.subject =subject;140 }141
142 /**
143 * 获取邮件内容144 *145 *@return邮件内容146 */
147 publicString getContent() {148 returncontent;149 }150
151 /**
152 * 设置邮件内容153 *154 *@paramcontent155 * 邮件内容156 */
157 public voidsetContent(String content) {158 this.content =content;159 }160
161 /**
162 * 获取发送日期163 *164 *@return发送日期165 */
166 publicString getDate() {167 returndate;168 }169
170 /**
171 * 设置发送日期172 *173 *@paramdate174 * 发送日期175 */
176 public voidsetDate(String date) {177 this.date =date;178 }179
180 /**
181 * 获取登陆邮箱的用户名182 *183 *@return登陆邮箱的用户名184 */
185 publicString getUser() {186 returnuser;187 }188
189 /**
190 * 设置登陆邮箱的用户名191 *192 *@paramuser193 * 登陆邮箱的用户名194 */
195 public voidsetUser(String user) {196 this.user =user;197 }198
199 /**
200 * 获取登陆邮箱的密码201 *202 *@return登陆邮箱的密码203 */
204 publicString getPassword() {205 returnpassword;206 }207
208 /**
209 * 设置登陆邮箱的密码210 *211 *@parampassword212 * 登陆邮箱的密码213 */
214 public voidsetPassword(String password) {215 this.password =password;216 }217
218 }
开启服务后,这时候我们就可以有stmp服务了。
1 /*2 * To change this template, choose Tools | Templates3 * 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 *@authorhongten22 */
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命令成功后返回25090 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:", 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:", 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);//helo209 authLogin(message, in, out);//auth login210 mailfrom(message.getFrom(), in, out);//mail from211 rcpt(message.getTo(), in, out);//rcpt to212 data(message.getDatafrom(), message.getDatato(), message.getSubject(), message.getContent(), in, out);//DATA213 quit(in, out);//quit214 } catch (Exception e) {
215 e.printStackTrace();
216 return false;
217 }
218 return true;
219 }
220 }
运行效果:
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 *@authorhongten18 */
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命令成功后返回25086 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:", 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:", 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);//helo205 authLogin(message, in, out);//auth login206 mailfrom(message.getFrom(), in, out);//mail from207 rcpt(message.getTo(), in, out);//rcpt to208 data(message.getDatafrom(), message.getDatato(), message.getSubject(), message.getContent(), in, out);//DATA209 quit(in, out);//quit210 } catch (Exception e) {
211 e.printStackTrace();
212 return false;
213 }
214 return true;
215 }
216 }
运行效果:
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 *@authorhongten18 */
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命令成功后返回25087 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:", 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:", 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);//helo206 authLogin(message, in, out);//auth login207 mailfrom(message.getFrom(), in, out);//mail from208 rcpt(message.getTo(), in, out);//rcpt to209 data(message.getDatafrom(), message.getDatato(), message.getSubject(), message.getContent(), in, out);//DATA210 quit(in, out);//quit211 } catch (Exception e) {
212 e.printStackTrace();
213 return false;
214 }
215 return true;
216 }
217 }
运行效果:
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 *@authorhongten18 */
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命令成功后返回25087 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:", 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:", 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);//helo206 authLogin(message, in, out);//auth login207 mailfrom(message.getFrom(), in, out);//mail from208 rcpt(message.getTo(), in, out);//rcpt to209 data(message.getDatafrom(), message.getDatato(), message.getSubject(), message.getContent(), in, out);//DATA210 quit(in, out);//quit211 } catch (Exception e) {
212 e.printStackTrace();
213 return false;
214 }
215 return true;
216 }
217 }
运行效果 :
java smtp.126.com_java开发_STMP邮箱客户端_发送邮件相关推荐
- java smtp.126.com_Java采用SMTP协议发送邮件 | 学步园
下载后解压,将mail.jar和activation.jar放到project里的lib文件夹中,为其配置环境变量,或在myEclipse里的"Java Build Path"中将 ...
- java smtp.126.com_Java Mail---SMTP、POP3协议-DOS下手动收发邮件演示过程
转载请注明出处: http://blog.csdn.net/qq_26525215 本文源自 E-Mail协议简介: 邮件服务器,按照提供的服务类型,可以分为发送邮件的服务器我接收邮件的服务器. 发送 ...
- 【源码+教程】Java课设项目_12款最热最新Java游戏项目_Java游戏开发_Java小游戏_飞翔的小鸟_王者荣耀_超级玛丽_推箱子_黄金矿工_贪吃蛇
马上就要期末了,同学们课设做的如何了呢?本篇为大家带来了12款热门Java小游戏项目的源码和教程,助力大家顺利迎接暑假![源码+教程]Java课设项目_12款最热最新Java游戏项目_Java游戏开发 ...
- java原生126邮箱发送邮件代码实现
1.必要条件: a.有126邮箱,并且要有授权码,授权码获取: 点击上图的设置,如下,确保这两个服务开启 在授权密码管理界面新增授权密码,记住新增的授权密码和下面的SMTP服务器名称,下面编码会用到 ...
- 记录一下开发邮件模板兼容outlook客户端以及其他主流邮箱客户端所引发的无数问题,头发要薅没了
最近整了一个钓鱼网站,然后会发送模板邮件,模板内容如下 等等诸如此类的样式布局,这个直接用原生的html+css写的模板内容,发送邮件之后在邮箱里面显示的很正常,结果出现了一个outlook邮箱. 这 ...
- evolution ubuntu邮箱_Ubuntu下使用Evolution电子邮箱客户端
=摘要= 话说一直是使用web的邮箱客户端,今天想了想应该试试Evolution这个linux下面的邮箱客户端.搞了搞,能够正常收发邮件了呵呵,在能够接收邮件的同时,现在也不用每一次都得登录到web邮 ...
- 使用java代码发送zip文件到邮箱_Azkaban安装与使用(下)
01 PART Azkaban进阶 JavaProcess作业类型案例 JavaProcess类型可以运行一个自定义主类方法,type类型为javaprocess,可用的配置为: Xms:最小堆 9 ...
- 我的第二个独立开发的邮箱类App—“简邮”(支持QQ、雅虎、阿里云、Outlook)
360手机市场地址: 360市场 其它市场还在审核,囧... 为什么做这个App? 主要有两个原因 1.10月份正逢校招季,--当时和面试官介绍了这个APP 2.在苹果手机上看到一款内置的邮箱app支 ...
- Java程序员常用开发软件介绍
Java程序员常用开发软件介绍 Java程序员常用开发软件介绍 后端开发利器:idea / eclipse及各种封装版本 数据库客户端:Navicat / Heidisql 前端开发: vs code ...
- 用pop3获取邮箱附件_在第三方电子邮件客户端如何测试邮箱账号?
我们常见的邮箱有:网易邮箱.outlook.foxmail.畅邮(Dreammail Pro)等等,下面以畅邮(Dreammail Pro)为例. 流程 排查邮箱故障:通过"测试帐号&quo ...
最新文章
- docker ubuntu镜像_Docker 入门指南 | Linux 中国
- mysql存储树结构_mysql 树形结构查询(存储过程)
- 从hotspot底层对象结构理解锁膨胀升级过程||深入jdk源码理解longadder的分段cas优化机制——分段CAS优化
- 更改Cmd默认默认路径(以设置为D:/为例)
- 动态改变stage桢数
- echarts中toolbox位置_echarts toolbox 扩展
- 新东方年会diss老板节目爆红!俞敏洪:奖励12万
- python后台Flask 快速入门
- React Native Android混合开发实战教程
- 一名Java开发人员装机必备软件
- 迅捷PDF虚拟打印机怎么安装和使用
- opencv实时录像+视频打码
- 简信CRM:什么样的企业适合引入CRM管理系统?
- 超详细——python把中文汉字转成拼音xpinyin,pypinyin,snownlp三种方法
- phpstorm 报 expecting statement
- c语言李白喝酒答案,思维的体操——李白喝酒(2014年春蓝桥杯个人赛)
- 计算机新教师汇报课报道,提升专业促成长—开元路小学开展新教师汇报课记实...
- matlab 精品课程,同济大学经济与管理学院精品课程
- 台州农商行计算机专业能力测试,银行/农商行笔试!刷题要刷到点子上,来这里专业老师给你出题...
- 继续教育公需课——人工智能技术及其发展趋势答案