明白了,谢谢典型的SMTP操作此类显示邮件如何由在USC-ISIF和机上的Smith发送到BBN-UNIX主机上Jones,Green和Brown的。这里,我们假设USC-ISIF主机直接和BBN-UNIX主机联系。Jones和Brown接收邮件,而Green在BBN-UNIX上没有邮箱。R: 220 BBN-UNIX.ARPA Simple Mail Transfer Service ReadyS: HELO USC-ISIF.ARPAR: 250 BBN-UNIX.ARPA S: MAIL FROM:<[email protected]>R: 250 OK S: RCPT TO:<[email protected]>R: 250 OK S: RCPT TO:<[email protected]>R: 550 No such user here S: RCPT TO:<[email protected]>R: 250 OK S: DATAR: 354 Start mail input; end with <CRLF>.<CRLF>S: Blah blah blah...S: ...etc. etc. etc.S: .R: 250 OK S: QUIT
Socket来实现一个SMTP客户端,你连接上服务器之后,只要遵从rfc:smtp标准协议,通过
向输出流中写入指令,便可完成收发电子邮件的功能了。
你这里就是不实现Serializable接口同样可以
output.print(command + "\r\n");
serverReply = input.readLine();
if (serverReply.charAt(0) == '4' || serverReply.charAt(0) == '5')
return true;
就可以了???public void sendMessage(String mailfrom, String mailto, String subject, String message) throws SMTPException { Date ldate_today = new Date(System.currentTimeMillis());
SimpleDateFormat lgmt_date = new SimpleDateFormat("d MMM yyyy HH:mm:ss 'GMT'");
lgmt_date.setTimeZone(TimeZone.getTimeZone("GMT"));
lgmt_date.format(ldate_today); if (submitCommand("DATA"))
throw new SMTPException("Error during DATA command.");
String header = "From: " + mailfrom + "\r\n";
header += "To: " + mailto + "\r\n";
header += "Subject: " + subject + "\r\n";
header += "Date: " + lgmt_date + "\r\n\r\n";
if (submitCommand(header + message + "\r\n."))
throw new SMTPException("Error during mail transmission.");
}
private boolean submitCommand(String command) throws SMTPException {
try {
output.print(command + "\r\n");
serverReply = input.readLine();
if (serverReply.charAt(0) == '4' || serverReply.charAt(0) == '5')
return true;
else
return false;
}
catch(Exception e) {
throw new SMTPException(e.getMessage());
}
}