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 Stack using Linked List
String Joiner trong Java 8
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Java Program to Implement Quick Sort with Given Complexity Constraint
Java Program to Implement Booth Algorithm
ETags for REST with Spring
A Guide to Spring Cloud Netflix – Hystrix
Check If a File or Directory Exists in Java
Java Program to Generate Random Hexadecimal Byte
Giới thiệu Google Guice – Binding
Spring Boot - Enabling HTTPS
A Quick Guide to Spring Cloud Consul
Java Program to Implement TreeMap API
Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays
Spring Data Java 8 Support
Split a String in Java
Java Program to Implement Range Tree
Programmatic Transaction Management in Spring
Java Program for Topological Sorting in Graphs
Sort a HashMap in Java
Properties with Spring and Spring Boot
Java Program to Find Minimum Element in an Array using Linear Search
Spring Boot - Tomcat Port Number
Getting Started with Forms in Spring MVC
So sánh HashSet, LinkedHashSet và TreeSet trong Java
Java Program to Implement LinkedBlockingDeque API
Hướng dẫn sử dụng Printing Service trong Java
Guide to ThreadLocalRandom in Java
Java Program to Encode a Message Using Playfair Cipher
Lập trình đa luồng với CompletableFuture trong Java 8
New Features in Java 14
How to Use if/else Logic in Java 8 Streams