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 }

运行效果 :

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值