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:
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Java Program to Implement Sieve Of Atkin
Convert XML to JSON Using Jackson
Deploy a Spring Boot App to Azure
Spring Web Annotations
Converting a Stack Trace to a String in Java
Java Program to Implement Adjacency List
Intersection of Two Lists in Java
Control Structures in Java
Chuyển đổi Array sang ArrayList và ngược lại
Optional trong Java 8
Java Program to Implement Hash Tables
Integer Constant Pool trong Java
Guide to the Synchronized Keyword in Java
Bootstrap a Web Application with Spring 5
Java Program to Implement Levenshtein Distance Computing Algorithm
Introduction to Spring Method Security
Spring REST API with Protocol Buffers
Apache Commons Collections OrderedMap
Chương trình Java đầu tiên
Spring Boot - Creating Docker Image
Java Program to Find the Longest Path in a DAG
Queue và PriorityQueue trong Java
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Adding Shutdown Hooks for JVM Applications
Java Program to Implement the RSA Algorithm
Finding the Differences Between Two Lists in Java
Spring 5 WebClient
StringBuilder vs StringBuffer in Java
Spring Boot Application as a Service
Spring Autowiring of Generic Types
Java Program to Implement Solovay Strassen Primality Test Algorithm