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:
Flattening Nested Collections in Java
Comparing Arrays in Java
Handle EML file with JavaMail
Java – String to Reader
Hướng dẫn sử dụng Printing Service trong Java
Send an email with an attachment
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Guide to Spring Cloud Kubernetes
SOAP Web service: Authentication trong JAX-WS
Implementing a Binary Tree in Java
Map Serialization and Deserialization with Jackson
Understanding Memory Leaks in Java
Java Optional as Return Type
Send email with SMTPS (eg. Google GMail)
Guide to the Fork/Join Framework in Java
Consumer trong Java 8
File Upload with Spring MVC
New Features in Java 9
Handling URL Encoded Form Data in Spring REST
HttpClient Basic Authentication
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Guide to Java 8’s Collectors
Spring Boot - Eureka Server
Spring Data MongoDB – Indexes, Annotations and Converters
Java Program to Find Nearest Neighbor Using Linear Search
New Features in Java 14
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Java – Delete a File
Intro to the Jackson ObjectMapper
Use Liquibase to Safely Evolve Your Database Schema
Java Program to Implement Queue using Two Stacks
Working with Network Interfaces in Java