1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.mercury.smtp;
14
15 import java.io.ByteArrayOutputStream;
16 import java.io.IOException;
17 import java.io.OutputStream;
18
19
20
21
22
23
24 public class SMTPResponse {
25
26
27 protected static final byte[] CRLF = new byte[] { '\r', '\n' };
28
29
30 protected int code;
31
32
33 protected String[] msg;
34
35
36 private static final String[] empty = new String[] { "" };
37
38
39
40
41
42 public SMTPResponse(int code) {
43 this.code = code;
44 msg = empty;
45 }
46
47
48
49
50
51
52 public SMTPResponse(int code, String msg) {
53 this.code = code;
54 if (msg == null) {
55 msg = "";
56 }
57 this.msg = new String[] { msg };
58 }
59
60
61
62
63
64
65 public SMTPResponse(int code, String[] msg) {
66 this.code = code;
67 this.msg = msg;
68 }
69
70
71
72
73
74 public void setLine(String line) {
75 msg = new String[] { line };
76 }
77
78
79
80
81
82 public void addLine(String line) {
83 String[] n = new String[msg.length + 1];
84 n[msg.length] = line;
85 msg = n;
86 }
87
88
89
90
91
92
93 public void submit(OutputStream out) throws IOException {
94 if (msg.length > 1) {
95 for (int i = 0; i < msg.length - 1; i++) {
96 out.write((code + "-" + msg[i]).getBytes());
97 out.write(CRLF);
98 }
99 }
100 out.write((code + " " + msg[msg.length - 1]).getBytes());
101 out.write(CRLF);
102 out.flush();
103 }
104
105
106
107
108
109 public String toString() {
110 ByteArrayOutputStream res = new ByteArrayOutputStream();
111 try {
112 submit(res);
113 } catch (IOException e) {
114 }
115 return new String(res.toByteArray());
116 }
117
118
119
120
121
122 public int getCode() {
123 return code;
124 }
125 }