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:
Java Program to Implement Unrolled Linked List
Java Program to Implement Uniform-Cost Search
How to Iterate Over a Stream With Indices
Build a REST API with Spring and Java Config
A Guide to Iterator in Java
Lập trình đa luồng với CompletableFuture trong Java 8
Simple Single Sign-On with Spring Security OAuth2
Converting a List to String in Java
Request a Delivery / Read Receipt in Javamail
Spring Boot - Tomcat Deployment
Spring Security Custom AuthenticationFailureHandler
Guava Collections Cookbook
Java Program to Perform LU Decomposition of any Matrix
The Guide to RestTemplate
How to Set TLS Version in Apache HttpClient
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Initialize a HashMap in Java
Java NIO2 Path API
Java Program to Implement Patricia Trie
Java Program to Implement Ternary Heap
Java Program to Implement Affine Cipher
Phương thức forEach() trong java 8
Java Program to Implement Treap
Limiting Query Results with JPA and Spring Data JPA
Hướng dẫn Java Design Pattern – Iterator
Request Method Not Supported (405) in Spring
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Java Program to Implement the RSA Algorithm
So sánh ArrayList và Vector trong Java
Spring MVC Tutorial
Spring Boot - OAuth2 with JWT