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:
Creating a Custom Starter with Spring Boot
The Order of Tests in JUnit
Java Program to Perform Sorting Using B-Tree
Java Program to implement Priority Queue
Java Program to Find Maximum Element in an Array using Binary Search
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Java Program to Generate Random Numbers Using Probability Distribution Function
Tính đóng gói (Encapsulation) trong java
Call Methods at Runtime Using Java Reflection
Java Program to Implement Hash Tables Chaining with List Heads
Java Program to Check whether Undirected Graph is Connected using DFS
Functional Interfaces in Java 8
Spring Security OAuth2 – Simple Token Revocation
Java Program to Represent Graph Using 2D Arrays
Java Program to Implement Gaussian Elimination Algorithm
How to Add a Single Element to a Stream
A Guide to the Java LinkedList
Getting Started with Custom Deserialization in Jackson
Java Program to Perform Stooge Sort
Xử lý ngoại lệ trong Java (Exception Handling)
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Java Program to Implement Naor-Reingold Pseudo Random Function
Spring 5 and Servlet 4 – The PushBuilder
Flattening Nested Collections in Java
A Guide to Spring Boot Admin
Java Program to Show the Duality Transformation of Line and Point
Form Validation with AngularJS and Spring MVC
Java Program to Implement ArrayBlockingQueue API
Comparing Dates in Java
Exception Handling in Java
Concatenating Strings In Java
Spring Security Remember Me