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:
Exception Handling in Java
Tìm hiểu về xác thực và phân quyền trong ứng dụng
Introduction to the Java NIO2 File API
OAuth2 Remember Me with Refresh Token
Guava Collections Cookbook
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Tổng quan về ngôn ngữ lập trình java
Java Program to Implement Min Heap
A Guide to HashSet in Java
Java Program to Implement Max Heap
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Guide to @ConfigurationProperties in Spring Boot
Working with Kotlin and JPA
Guide to PriorityBlockingQueue in Java
Java Program to Implement Repeated Squaring Algorithm
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Java Program to Use rand and srand Functions
Summing Numbers with Java Streams
Hướng dẫn Java Design Pattern – Prototype
Instance Profile Credentials using Spring Cloud
Serialization và Deserialization trong java
Java Program to Implement Word Wrap Problem
Java Program to Implement Bloom Filter
Java Program to Show the Duality Transformation of Line and Point
Java Program to Implement Levenshtein Distance Computing Algorithm
Dockerizing a Spring Boot Application
Java Program to Represent Linear Equations in Matrix Form
Java Program to Perform Naive String Matching
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Java Collections Interview Questions
Java Program to Perform Insertion in a 2 Dimension K-D Tree
Intro to Spring Boot Starters