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 Implement Queue using Linked List
Java Program to Implement Segment Tree
Tổng quan về ngôn ngữ lập trình java
So sánh Array và ArrayList trong Java
Quick Guide to @RestClientTest in Spring Boot
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Jackson – Change Name of Field
Java Program to Implement Sieve Of Sundaram
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
Một số ký tự đặc biệt trong Java
Spring Boot - Creating Docker Image
Java Program to Perform the Sorting Using Counting Sort
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Convert char to String in Java
Java Program to Check whether Graph is a Bipartite using DFS
Guide to the Volatile Keyword in Java
Check if a String is a Palindrome in Java
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Java Program to Implement Euler Circuit Problem
Java Program to Implement Euclid GCD Algorithm
Overview of the java.util.concurrent
Java Program to Implement Hash Tables Chaining with List Heads
Java List UnsupportedOperationException
Java Program to Perform the Shaker Sort
Spring Boot - Application Properties
Guide to Apache Commons CircularFifoQueue
Apache Commons Collections SetUtils
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Java Program to Find Maximum Element in an Array using Binary Search
Spring Boot - File Handling
Java Program to Represent Graph Using 2D Arrays
Chuyển đổi từ HashMap sang ArrayList