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:
Primitive Type Streams in Java 8
Spring Data JPA @Modifying Annotation
Java Program to Implement Quick Sort with Given Complexity Constraint
Spring Security Custom AuthenticationFailureHandler
TreeSet và sử dụng Comparable, Comparator trong java
Hướng dẫn sử dụng Java Reflection
Spring Data MongoDB Transactions
Giới thiệu SOAP UI và thực hiện test Web Service
Handle EML file with JavaMail
LinkedHashSet trong Java hoạt động như thế nào?
Using a Mutex Object in Java
Java Program to Implement Singly Linked List
Spring REST with a Zuul Proxy
A Guide To UDP In Java
Java 8 Stream API Analogies in Kotlin
Luồng Daemon (Daemon Thread) trong Java
Java Program to Implement the String Search Algorithm for Short Text Sizes
Spring Boot with Multiple SQL Import Files
JWT – Token-based Authentication trong Jersey 2.x
Giới thiệu về Stream API trong Java 8
Map to String Conversion in Java
Java Program to Implement ConcurrentHashMap API
A Guide to WatchService in Java NIO2
Default Password Encoder in Spring Security 5
Java Program to Implement Attribute API
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Converting String to Stream of chars
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Spring Security Form Login
Java Program to Implement Pollard Rho Algorithm
Build a REST API with Spring and Java Config
Exception Handling in Java