其实只需要按照邮件发送的格式构建内容发送,对方邮件服务器能正常解析即可。
public static void send() {
String sender = "sender@link-nemo.com";
String receiver = "receiver@link-nemo.com";
String password = "xxxxxxxxxx";
String user = new BASE64Encoder().encode(sender.substring(0, sender.indexOf("@")).getBytes());
String pass = new BASE64Encoder().encode(password.getBytes());
try {
Socket socket = new Socket("smtp.link-nemo.com", 25);
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
//
PrintWriter writter = new PrintWriter(outputStream, true);
System.out.println(reader.readLine());
//Hello
writter.println("HELO Nemo");
System.out.println(reader.readLine());
//AUTH LOGIN
writter.println("auth login");
System.out.println(reader.readLine());
writter.println(user);
System.out.println(reader.readLine());
writter.println(pass);
System.out.println(reader.readLine());
//Above Authentication successful
//Set mail from and rcpt to
writter.println("mail from:<" + sender + ">");
System.out.println(reader.readLine());
writter.println("rcpt to:<" + receiver + ">");
System.out.println(reader.readLine());
//Set data
writter.println("data");
System.out.println(reader.readLine());
//title
writter.println("subject:Hello Nemo,It's me!!!");
writter.println("from:" + sender);
writter.println("to:" + receiver);
writter.println("Content-Type: text/plain;charset=\"utf-8\"");
writter.println();
//content
writter.println("How do you do?");
writter.println(".");
writter.println("");
System.out.println(reader.readLine());
//Say GoodBye
writter.println("rset");
System.out.println(reader.readLine());
writter.println("quit");
System.out.println(reader.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}