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:
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Configure a Spring Boot Web Application
Autoboxing và Unboxing trong Java
Control the Session with Spring Security
DistinctBy in the Java Stream API
Java Program to Implement LinkedHashSet API
Java – Reader to InputStream
Java Program to Implement Ternary Heap
Jackson – Bidirectional Relationships
Guide to CountDownLatch in Java
Spring Boot - Admin Server
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Guide to ThreadLocalRandom in Java
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Enum trong java
Java Program to Implement Binary Tree
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Spring Boot - Tomcat Deployment
Java Program to Check whether Undirected Graph is Connected using DFS
Serialize Only Fields that meet a Custom Criteria with Jackson
Spring – Injecting Collections
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Transactions with Spring and JPA
Allow user:password in URL
OAuth 2.0 Resource Server With Spring Security 5
A Guide to HashSet in Java
Guide to Spring 5 WebFlux
Java Program to Implement Sparse Matrix
Spring Autowiring of Generic Types