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:
ETL with Spring Cloud Data Flow
Java Program to implement Array Deque
Java Program to Implement Find all Forward Edges in a Graph
Recommended Package Structure of a Spring Boot Project
Java Program to Implement Quick Sort Using Randomization
Java 8 StringJoiner
Converting a Stack Trace to a String in Java
Java Program to Implement ConcurrentLinkedQueue API
Hashing a Password in Java
Java Program to Implement Cartesian Tree
Java Program to Implement CountMinSketch
Guide to Java OutputStream
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Spring Security Basic Authentication
Java Program to Implement Gauss Jordan Elimination
Java Program to Perform Partition of an Integer in All Possible Ways
Java Program to Implement TreeMap API
Java Program to Implement Gale Shapley Algorithm
Exploring the New Spring Cloud Gateway
How to Convert List to Map in Java
Hướng dẫn sử dụng lớp Console trong java
Pagination and Sorting using Spring Data JPA
Java Program to Solve Tower of Hanoi Problem using Stacks
Java Program to Implement LinkedBlockingQueue API
Guide to the ConcurrentSkipListMap
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Hướng dẫn Java Design Pattern – Intercepting Filter
Java Program to Implement CopyOnWriteArraySet API
Hướng dẫn sử dụng Lớp FilePermission trong java
Java Program to Implement Horner Algorithm
Java Program to Create a Random Graph Using Random Edge Generation