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:
Introduction to Netflix Archaius with Spring Cloud
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
Java Program to Implement Coppersmith Freivald’s Algorithm
Java – Create a File
Java Program to Describe the Representation of Graph using Incidence List
How To Serialize and Deserialize Enums with Jackson
Deploy a Spring Boot WAR into a Tomcat Server
Java Program to Implement K Way Merge Algorithm
Java Program to Check for balanced parenthesis by using Stacks
Create a Custom Auto-Configuration with Spring Boot
Hướng dẫn Java Design Pattern – Transfer Object
Configure a RestTemplate with RestTemplateBuilder
Java Program to Implement Knight’s Tour Problem
Guide to PriorityBlockingQueue in Java
Java Program to Implement Ternary Tree
StringBuilder vs StringBuffer in Java
Spring Boot - Batch Service
Static Content in Spring WebFlux
Apache Commons Collections Bag
Java Program to Implement Bit Array
Injecting Prototype Beans into a Singleton Instance in Spring
Java Program to Implement Ford–Fulkerson Algorithm
So sánh Array và ArrayList trong Java
How to Delay Code Execution in Java
Java CyclicBarrier vs CountDownLatch
Test a REST API with Java
Java Program to Implement Vector API
Lớp Arrarys trong Java (Arrays Utility Class)
Spring Cloud – Securing Services
Java Program to Implement Depth-limited Search
Debugging Reactive Streams in Java
Hướng dẫn Java Design Pattern – Flyweight