📄 smtp.java
字号:
package jws.net;
// Copyright 1997, John Webster Small
// All rights Reserved
import java.io.*;
import java.util.*;
import java.net.*;
public class SMTP implements Serializable
{
protected String server;
protected int port;
/** The default SMTP port is 25. */
public static final int defaultPort = 25;
protected String senderHost;
protected String senderEmailAddress;
private transient String response = "";
public String getResponse() { return response; }
protected SMTP()
{
server = senderHost = senderEmailAddress = "";
port = defaultPort;
}
public SMTP
(String server, int port,
String senderHost, String senderEmailAddress)
{
this.server = server;
this.port = port;
this.senderHost = senderHost;
this.senderEmailAddress = senderEmailAddress;
}
public SMTP(String server,
String senderHost, String senderEmailAddress)
{ this(server,defaultPort,senderHost,senderEmailAddress); }
// return true if at least one addressee makes it
private boolean RCPT_TO
(BufferedReader in, PrintWriter out, String[] addresses)
throws IOException
{
boolean atLeastOneRecipient = false;
for (int i = 0; i < addresses.length; i++) {
out.println("RCPT TO:<"
+ Email.addressee(addresses[i]) + ">");
out.flush();
response = in.readLine();
atLeastOneRecipient |= response.startsWith("250");
}
return atLeastOneRecipient;
}
public void sendMail(Enumeration messages)
throws IOException, UnknownHostException
{
boolean ok;
Socket s = null;
BufferedReader in = null;
PrintWriter out = null;
try {
s = new Socket(server,port);
in = new BufferedReader
(new InputStreamReader(s.getInputStream()));
out = new PrintWriter
(new BufferedOutputStream(s.getOutputStream()));
response = in.readLine();
ok = response.startsWith("220");
if (!ok)
throw new IOException(response);
out.println("HELO " + senderHost);
out.flush();
response = in.readLine();
ok = response.startsWith("250");
if (!ok)
throw new IOException(response);
while (messages.hasMoreElements())
{
Email msg = (Email) messages.nextElement();
msg.setFrom(senderEmailAddress);
msg.postMark();
out.println("MAIL FROM:<"
+ Email.addressee(msg.getFrom())+">");
out.flush();
response = in.readLine();
ok = response.startsWith("250");
if (!ok)
throw new IOException(response);
boolean atLeastOneRecipient
= RCPT_TO(in,out,msg.getTo());
atLeastOneRecipient
|= RCPT_TO(in,out,msg.getCc());
atLeastOneRecipient
|= RCPT_TO(in,out,msg.getBcc());
if (!atLeastOneRecipient)
throw new IOException
("at least one recipient must be valid");
out.println("DATA");
out.flush();
response = in.readLine();
ok = response.startsWith("354");
if (!ok)
throw new IOException(response);
Enumeration headers = msg.getHeaders();
while (headers.hasMoreElements()) {
out.println((String)headers.nextElement());
out.flush();
}
out.println("");
out.flush();
Enumeration body = msg.getBody();
while (body.hasMoreElements()) {
String line = (String) body.nextElement();
if (line.equals("."))
line = "..";
out.println(line);
out.flush();
}
out.println(".");
out.flush();
response = in.readLine();
ok = response.startsWith("250");
if (!ok)
throw new IOException(response);
}
out.println("QUIT");
out.flush();
} finally {
if (in != null)
in.close();
if (out != null)
out.close();
if (s != null)
s.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -