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:
String Processing with Apache Commons Lang 3
Converting Between a List and a Set in Java
Apache Commons Collections MapUtils
Configuring a DataSource Programmatically in Spring Boot
Java Program to Implement Rope
Simultaneous Spring WebClient Calls
Bootstrap a Web Application with Spring 5
Java Program to Implement Adjacency List
Autoboxing và Unboxing trong Java
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
A Quick Guide to Spring Cloud Consul
Java – Rename or Move a File
Giới thiệu Design Patterns
Java Program to Check whether Directed Graph is Connected using BFS
Static Content in Spring WebFlux
Hướng dẫn sử dụng Java Generics
Guide to the Java TransferQueue
Java Program to Implement Bellman-Ford Algorithm
Java Program to Perform Encoding of a Message Using Matrix Multiplication
REST Web service: Upload và Download file với Jersey 2.x
Apache Commons Collections OrderedMap
HTTP Authentification and CGI/Servlet
Spring Boot - Hystrix
Lớp Collections trong Java (Collections Utility Class)
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Logging in Spring Boot
A Guide to JPA with Spring
Java Program to Implement Strassen Algorithm
Converting a Stack Trace to a String in Java
LIKE Queries in Spring JPA Repositories
Calling Stored Procedures from Spring Data JPA Repositories