你可以试试这个:这里有很好的文档http://www.cs.cf.ac.uk/Dave/PERL/node175.html(我收到一个套接字连接错误,但最终代码看起来或多或少相同的)
public class Mailer {
private static BufferedReader reader;
private static OutputStream outputStream;
public static void main(String args[]) {
args = new String[]{"smtp.gmail.com","my_id@gmail.com","your_id@gmail.com"};
try {
if (args.length == 3) {
Socket socket = new Socket(args[0], 465);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
outputStream = socket.getOutputStream();
smtpCommand("HELO " + args[0]);
smtpCommand("MAIL From: " + args[1]);
smtpCommand("RCPT To: " + args[2]);
smtpCommand("DATA");
smtpCommand("From: " + args[1] + "\nTo: " + args[2] + "\nContent-Type:text/html;\nSubject: test\n MAIL CONTENT \n.\n");
System.out.println("\nMessage Sent Successfully to" + args[1].substring(0, args[1].indexOf("@")));
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void smtpCommand(String command) throws Exception {
System.out.println(command);
reader.readLine();
outputStream.write((command + "\r\n").getBytes());
}
}