SMTP is the protocol used to send an email.
import java.net.*;
import java.io.*;
public class SendVietMXMail {
public static void main(String s[]) {
//
// Send fake mail from Elvis Presley
//
// SendVietMXMail [mail server] [recipient address]
// mail server can be hostname or IP address
//
// ex. SendVietMXMail mail.company.com myFriend@somewhere.vn
//
SendVietMXMail t = new SendVietMXMail();
t.sendMail(s[0], s[1]);
}
public void sendMail(String mailServer, String recipient) {
try {
Socket s = new Socket(mailServer, 25);
BufferedReader in = new BufferedReader
(new InputStreamReader(s.getInputStream(), "8859_1"));
BufferedWriter out = new BufferedWriter
(new OutputStreamWriter(s.getOutputStream(), "8859_1"));
send(in, out, "HELO theWorld");
// warning : some mail server validate the sender address
// in the MAIL FROm command, put your real address here
send(in, out, "MAIL FROM: <viet.mai@maixuanviet.com>");
send(in, out, "RCPT TO: " + recipient);
send(in, out, "DATA");
send(out, "Subject: In the ghetto");
send(out, "From: Viet Mai <viet.mai@maixuanviet.com>");
send (out, "\n");
// message body
send(out, "I'm alive. Help me!");
send(out, "\n.\n");
send(in, out, "QUIT");
s.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void send(BufferedReader in, BufferedWriter out, String s) {
try {
out.write(s + "\n");
out.flush();
System.out.println(s);
s = in.readLine();
System.out.println(s);
}
catch (Exception e) {
e.printStackTrace();
}
}
public void send(BufferedWriter out, String s) {
try {
out.write(s + "\n");
out.flush();
System.out.println(s);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Done! Happy Coding!
Related posts:
Spring Boot - Rest Template
Receive email by java client
Spring Boot Integration Testing with Embedded MongoDB
Java Convenience Factory Methods for Collections
Thao tác với tập tin và thư mục trong Java
Java Program to Implement Regular Falsi Algorithm
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Model, ModelMap, and ModelAndView in Spring MVC
Debug a JavaMail Program
Arrays.asList vs new ArrayList(Arrays.asList())
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Constructor Injection in Spring with Lombok
Spring Boot - Build Systems
Java Program to Perform Right Rotation on a Binary Search Tree
Java Program to Implement Selection Sort
Removing all Nulls from a List in Java
Call Methods at Runtime Using Java Reflection
Registration – Password Strength and Rules
Java Program to Implement LinkedList API
Supplier trong Java 8
Recommended Package Structure of a Spring Boot Project
An Intro to Spring Cloud Task
Java Concurrency Interview Questions and Answers
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Java Program to Implement Max-Flow Min-Cut Theorem
Java Program to Implement Fibonacci Heap
Java Program to Solve TSP Using Minimum Spanning Trees
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Từ khóa static và final trong java
Spring WebClient and OAuth2 Support
Java Program to Implement Range Tree