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:
Guide to Mustache with Spring Boot
Overview of the java.util.concurrent
Java Program to Implement Cubic convergence 1/pi Algorithm
Debug a JavaMail Program
Spring Boot - Google Cloud Platform
Hướng dẫn sử dụng Lớp FilePermission trong java
Registration – Password Strength and Rules
Giới thiệu Design Patterns
Handling Errors in Spring WebFlux
Java Program to Implement Counting Sort
How to Remove the Last Character of a String?
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Spring Boot - Build Systems
Lớp TreeMap trong Java
Hướng dẫn Java Design Pattern – MVC
How to Delay Code Execution in Java
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
RestTemplate Post Request with JSON
Thao tác với tập tin và thư mục trong Java
Java Program to Implement Knapsack Algorithm
Java CyclicBarrier vs CountDownLatch
Java Program to Perform the Sorting Using Counting Sort
JPA/Hibernate Persistence Context
Supplier trong Java 8
Java Program to Convert a Decimal Number to Binary Number using Stacks
Iterating over Enum Values in Java
Toán tử instanceof trong java
Chuyển đổi Array sang ArrayList và ngược lại
Configuring a DataSource Programmatically in Spring Boot
Enum trong java
Spring Boot - Actuator
Read an Outlook MSG file