📄 smtp.java
字号:
package Email.net;
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; //发送邮件地址
protected String user; //邮箱用户名
protected String password; //邮箱密码
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,String user,String password){
this.server = server;
this.port = port;
this.senderHost = senderHost;
this.senderEmailAddress = senderEmailAddress;
this.user = user;
this.password = password;
}
public SMTP(String server,String senderHost, String senderEmailAddress,String user,String password){
this(server,defaultPort,senderHost,senderEmailAddress,user,password);
}
// 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;
}
private String get64Code( String code32 ){
String code64 = new sun.misc.BASE64Encoder().encode( code32.getBytes() );
return code64;
}
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);
//身份验证
out.println(" AUTH LOGIN ");
out.flush();
response = in.readLine();
ok = response.startsWith("334");
if (!ok)
throw new IOException(response);
//输入用户名和密码
out.println( get64Code(user) ); //输入用户名
//System.out.println("User = "+ user);
out.flush();
response = in.readLine();
ok = response.startsWith("334");
if (!ok)
throw new IOException(response);
out.println( get64Code(password) ); //输入密码
//System.out.println("Pass = "+ password);
out.flush();
response = in.readLine();
ok = response.startsWith("235"); //通过验证
if (!ok)
throw new IOException(response);
//发邮件部分
while ( messages.hasMoreElements() ){
Email msg = (Email) messages.nextElement();
//System.out.println( "senderEmailAddress = "+ senderEmailAddress );
msg.setFrom( senderEmailAddress );
msg.postMark();
out.println("MAIL FROM:<" + Email.addressee( msg.getFrom() )+ ">");
out.flush();
response = in.readLine();
//System.out.println("r: " + response);
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");
System.out.println("response = "+ response );
System.out.println("ok = "+ ok);
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 + -