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:
Partition a List in Java
A Guide to Concurrent Queues in Java
Hướng dẫn Java Design Pattern – Factory Method
Java 8 and Infinite Streams
Deploy a Spring Boot App to Azure
Guide to UUID in Java
Configuring a DataSource Programmatically in Spring Boot
Introduction to Eclipse Collections
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Java Program to Implement a Binary Search Tree using Linked Lists
Servlet 3 Async Support with Spring MVC and Spring Security
Running Spring Boot Applications With Minikube
Java Program to Perform Insertion in a BST
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Send an email using the SMTP protocol
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Use Liquibase to Safely Evolve Your Database Schema
The Basics of Java Security
HashSet trong Java hoạt động như thế nào?
Java Program to Implement WeakHashMap API
Guide to ThreadLocalRandom in Java
Quick Intro to Spring Cloud Configuration
String Initialization in Java
Using Optional with Jackson
Java Program to Find the Vertex Connectivity of a Graph
Quick Guide to Spring Bean Scopes
Convert XML to JSON Using Jackson
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
How to Get All Spring-Managed Beans?
Introduction to Spring Data JDBC
An Example of Load Balancing with Zuul and Eureka