下面代码是我编写的SERVLET程序中的发邮件过程,供参考:
import java.io.*;
import java.net.*;
public class UserRegister extends HttpServlet
{
private static final int SMTP_PORT = 25;
private static final char SMTP_ERROR_CODE1 = '4';
private static final char SMTP_ERROR_CODE2 = '5';
private final String host = "smtpserver";//SMTP服务器名
private final Stringdo
main = "yourhostname";//当前执行JAVA程序的机器名
private final String sender = "yourname@yourdomain.com";//寄件地址
private final String recipients = "reviever@target.com";//收件地址
private final String subject = "subject";//邮件标题
protected voiddo
Post(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
//Variables to send mail.
String maildata;
String content;//邮件内容
Vector sessionTrace = new Vector(20);
// Try to send the mail, then
the response, catching
// any IO exception.
try {//try2
// Send the mail.
maildata =
"Date: " + (new Date()).toString() + "/r/n" +
"From: " + sender + "/r/n" +
"To: " + recipients + "/r/n" +
"Subject: " + subject + "/r/n" +
"/r/n" +
content + "/r/n";
sendMail(host,do
main, sender, recipients, subject, maildata, sessionTrace);
// Send a response to the client.
out.println("<tr>");
out.println("<td><span class=font>OK! Mail send successfully!</span></td>");
out.println("</tr>");
}
catch(IOException theException) {//寄件失败
// Send a response page to the client.
out.println("<tr>");
out.println("<td><span class=font>Can't send mail!</span></td>");
out.println("</tr>");
}
}
/**
* sendMail() sends the mail.
*/
protected void sendMail(
String host,
Stringdo
main,
String sender,
String recipients,
String subject,
String maildata,
Vector sessionTrace) throws IOException {
Socket mailSocket;
BufferedReader socketIn;
DataOutputStream socketOut;
String address;
StringTokenizer tokenizer;
// Open the connection to the SMTP server, then
get references to the
// input and output streams.
mailSocket = new Socket(host, SMTP_PORT);
socketIn = new BufferedReader(
new InputStreamReader(mailSocket.getInputStream()) );
socketOut = new DataOutputStream(mailSocket.getOutputStream());
// Get the initial reply from the server.
readReply(socketIn, sessionTrace);
// Greet the server.
sendCommand(socketOut, "HELO " +do
main, sessionTrace);
readReply(socketIn, sessionTrace);
// Send the sender's address.
sendCommand(socketOut, "MAIL FROM: " + sender, sessionTrace);
readReply(socketIn, sessionTrace);
// Send the list of recipients.
tokenizer = new StringTokenizer(recipients, ",");
while (tokenizer.hasMoreElements()) {
sendCommand(socketOut, "RCPT TO: " + tokenizer.nextToken(), sessionTrace);
readReply(socketIn, sessionTrace);
}
// Start the data section.
sendCommand(socketOut, "DATA", sessionTrace);
readReply(socketIn, sessionTrace);
// Send the mail message.
sendCommand(socketOut, maildata + ".", sessionTrace);
readReply(socketIn, sessionTrace);
// End the session.
sendCommand(socketOut, "QUIT", sessionTrace);
readReply(socketIn, sessionTrace);
}
/**
* sendCommand() sends an SMTP command to the SMTP server. An
*/
private void sendCommand(DataOutputStream out, String command, Vector sessionTrace)
throws IOException {
out.writeBytes(command + "/r/n");
sessionTrace.addElement(command);
// System.out.println(command);
}
/**
* readReply() reads the reply from the SMTP server.
*/
private void readReply(BufferedReader reader, Vector sessionTrace)
throws IOException {
String reply;
char statusCode;
reply = reader.readLine();
statusCode = reply.charAt(0);
sessionTrace.addElement(reply);
// System.out.println(reply);
if ( (statusCode == SMTP_ERROR_CODE1) |
(statusCode == SMTP_ERROR_CODE2) ) {
throw (new IOException("SMTP: " + reply));
}
}
}