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:
Display Auto-Configuration Report in Spring Boot
Spring Boot - Securing Web Applications
Jackson – Decide What Fields Get Serialized/Deserialized
Java Program to Implement LinkedBlockingDeque API
Spring Boot - Hystrix
Getting a File’s Mime Type in Java
Difference Between Wait and Sleep in Java
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Java Multi-line String
Guava Collections Cookbook
Introduction to Spring Data MongoDB
Spring Security OAuth2 – Simple Token Revocation
@DynamicUpdate with Spring Data JPA
Inheritance with Jackson
HttpClient with SSL
Hướng dẫn Java Design Pattern – DAO
Java Program to Implement Bucket Sort
Java Program to Implement Maximum Length Chain of Pairs
Java Program to Implement Pagoda
Từ khóa static và final trong java
Apache Commons Collections SetUtils
Java Program to Implement Hash Tables
Java Stream Filter with Lambda Expression
Extract links from an HTML page
Removing all duplicates from a List in Java
Primitive Type Streams in Java 8
An Intro to Spring Cloud Zookeeper
Hướng dẫn Java Design Pattern – Command
Java Program to Implement Segment Tree
Most commonly used String methods in Java
Spring REST with a Zuul Proxy