import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import java.util.Properties;
public class SimpleMail {
private static final String SMTP_HOST_NAME = "smtp.myserver.com";
private static final String SMTP_AUTH_USER = "myusername";
private static final String SMTP_AUTH_PWD = "mypwd";
public static void main(String[] args) throws Exception{
new SimpleMail().test();
}
public void test() throws Exception{
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session mailSession = Session.getDefaultInstance(props, auth);
// uncomment for debugging infos to stdout
// mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setContent("This is a test", "text/plain");
message.setFrom(new InternetAddress("info@maixuanviet.com"));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("maixuanviet.com@gmail.com"));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}
NOTE: The JavaMail Authenticator is found in the javax.mail package and is different from the java.net class of the same name. The two don’t share the same Authenticator as the JavaMail API works with Java 1.1, which didn’t have the java.net variety.
Related posts:
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Migrating from JUnit 4 to JUnit 5
Java Program to implement Bit Matrix
Java Program to Implement Sieve Of Atkin
Extra Login Fields with Spring Security
Hướng dẫn Java Design Pattern – Memento
Quick Guide to Spring Controllers
Java Program to Implement Counting Sort
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
How to Return 404 with Spring WebFlux
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Concatenating Strings In Java
Generic Constructors in Java
Java Program to Find the Connected Components of an UnDirected Graph
A Guide to JUnit 5 Extensions
Send email with JavaMail
Các nguyên lý thiết kế hướng đối tượng – SOLID
Converting a Stack Trace to a String in Java
Tạo chương trình Java đầu tiên sử dụng Eclipse
Returning Custom Status Codes from Spring Controllers
Java Program to Implement Adjacency List
Hướng dẫn Java Design Pattern – Composite
Spring Boot - Internationalization
How to Add a Single Element to a Stream
An Intro to Spring Cloud Contract
Vector trong Java
Java Program to Implement Warshall Algorithm
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
A Guide to Iterator in Java
Inject Parameters into JUnit Jupiter Unit Tests
Java Program to Implement Interpolation Search Algorithm