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 Implement VList
Lập trình đa luồng trong Java (Java Multi-threading)
Guide to Spring 5 WebFlux
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Service Registration with Eureka
Java Program to Implement Adjacency List
Java Program to Construct an Expression Tree for an Postfix Expression
Java Program to Implement Hash Tree
Java Program to Generate Date Between Given Range
Java Program to Implement Naor-Reingold Pseudo Random Function
Toán tử instanceof trong java
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Spring Boot - Enabling HTTPS
Java Program to Implement Sorted List
Using Java Assertions
Testing an OAuth Secured API with Spring MVC
Java Program to Find the Longest Path in a DAG
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Java Streams vs Vavr Streams
Java Program to Perform LU Decomposition of any Matrix
Java Program to Check Whether a Given Point is in a Given Polygon
Introduction to Spring Cloud CLI
Java IO vs NIO
Transaction Propagation and Isolation in Spring @Transactional
Java Program to Implement Insertion Sort
Spring Cloud AWS – RDS
Java Program to Implement PriorityQueue API
Control Structures in Java
Java Program to Represent Graph Using Adjacency List
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Java Program to Implement Attribute API
Java Program to Implement ConcurrentHashMap API