Nemo

Nemo 关注TA

路漫漫其修远兮,吾将上下而求索。

Nemo

Nemo

关注TA

路漫漫其修远兮,吾将上下而求索。

  •  普罗旺斯
  • 负责帅就完事了
  • 写了1,496,113字

该文章投稿至Nemo社区   Java  板块 复制链接


java 纯socket发送邮件

发布于 2018/09/28 19:06 1,796浏览 0回复 2,146


其实只需要按照邮件发送的格式构建内容发送,对方邮件服务器能正常解析即可。

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();
}
}
本文标签
 {{tag}}
点了个评