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:
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
A Guide to Concurrent Queues in Java
Giới thiệu thư viện Apache Commons Chain
Java Program to Implement the Bin Packing Algorithm
Spring Boot - Batch Service
Java – Get Random Item/Element From a List
Java Web Services – JAX-WS – SOAP
Java Program to Implement Sorted Circularly Singly Linked List
Java Program to Implement AA Tree
Intersection of Two Lists in Java
LIKE Queries in Spring JPA Repositories
Java Program to Implement Fenwick Tree
Converting String to Stream of chars
Derived Query Methods in Spring Data JPA Repositories
Java Program to implement Circular Buffer
Quick Guide to java.lang.System
Convert Time to Milliseconds in Java
Spring Boot - Sending Email
A Guide to EnumMap
Predicate trong Java 8
Spring MVC Tutorial
Java InputStream to String
A Guide to JUnit 5 Extensions
Hướng dẫn Java Design Pattern – Null Object
Merging Streams in Java
Java Program to Perform Partial Key Search in a K-D Tree
Converting Between a List and a Set in Java
Java Program to Check Multiplicability of Two Matrices
Login For a Spring Web App – Error Handling and Localization
Java Program to Perform Searching Using Self-Organizing Lists
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Programmatic Transaction Management in Spring