1 /* |
1 /* |
2 * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
41 * @author James Gosling |
41 * @author James Gosling |
42 */ |
42 */ |
43 |
43 |
44 public class SmtpClient extends TransferProtocolClient { |
44 public class SmtpClient extends TransferProtocolClient { |
45 |
45 |
|
46 private static int DEFAULT_SMTP_PORT = 25; |
46 String mailhost; |
47 String mailhost; |
47 SmtpPrintStream message; |
48 SmtpPrintStream message; |
48 |
49 |
49 /** |
50 /** |
50 * issue the QUIT command to the SMTP server and close the connection. |
51 * issue the QUIT command to the SMTP server and close the connection. |
72 else |
73 else |
73 issueCommand("rcpt to: <" + s + ">\r\n", 250); |
74 issueCommand("rcpt to: <" + s + ">\r\n", 250); |
74 } |
75 } |
75 |
76 |
76 public void to(String s) throws IOException { |
77 public void to(String s) throws IOException { |
|
78 if (s.indexOf('\n') != -1) { |
|
79 throw new IOException("Illegal SMTP command", |
|
80 new IllegalArgumentException("Illegal carriage return")); |
|
81 } |
77 int st = 0; |
82 int st = 0; |
78 int limit = s.length(); |
83 int limit = s.length(); |
79 int pos = 0; |
84 int pos = 0; |
80 int lastnonsp = 0; |
85 int lastnonsp = 0; |
81 int parendepth = 0; |
86 int parendepth = 0; |
114 if (lastnonsp > st) |
119 if (lastnonsp > st) |
115 toCanonical(s.substring(st, lastnonsp)); |
120 toCanonical(s.substring(st, lastnonsp)); |
116 } |
121 } |
117 |
122 |
118 public void from(String s) throws IOException { |
123 public void from(String s) throws IOException { |
119 if (s.startsWith("<")) |
124 if (s.indexOf('\n') != -1) { |
|
125 throw new IOException("Illegal SMTP command", |
|
126 new IllegalArgumentException("Illegal carriage return")); |
|
127 } |
|
128 if (s.startsWith("<")) { |
120 issueCommand("mail from: " + s + "\r\n", 250); |
129 issueCommand("mail from: " + s + "\r\n", 250); |
121 else |
130 } else { |
122 issueCommand("mail from: <" + s + ">\r\n", 250); |
131 issueCommand("mail from: <" + s + ">\r\n", 250); |
|
132 } |
123 } |
133 } |
124 |
134 |
125 /** open a SMTP connection to host <i>host</i>. */ |
135 /** open a SMTP connection to host <i>host</i>. */ |
126 private void openServer(String host) throws IOException { |
136 private void openServer(String host) throws IOException { |
127 mailhost = host; |
137 mailhost = host; |
128 openServer(mailhost, 25); |
138 openServer(mailhost, DEFAULT_SMTP_PORT); |
129 issueCommand("helo "+InetAddress.getLocalHost().getHostName()+"\r\n", 250); |
139 issueCommand("helo "+InetAddress.getLocalHost().getHostName()+"\r\n", 250); |
130 } |
140 } |
131 |
141 |
132 public PrintStream startMessage() throws IOException { |
142 public PrintStream startMessage() throws IOException { |
133 issueCommand("data\r\n", 354); |
143 issueCommand("data\r\n", 354); |