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 CopyOnWriteArraySet API
How to Change the Default Port in Spring Boot
Initialize a HashMap in Java
Programmatic Transaction Management in Spring
Spring Boot - Building RESTful Web Services
Spring Boot With H2 Database
So sánh HashSet, LinkedHashSet và TreeSet trong Java
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
A Custom Data Binder in Spring MVC
Lớp Collections trong Java (Collections Utility Class)
Introduction to the Java NIO Selector
Java Program to Implement Sieve Of Atkin
Java Program to Perform Left Rotation on a Binary Search Tree
Receive email by java client
@Order in Spring
Registration – Activate a New Account by Email
Java Program to Implement Circular Doubly Linked List
Jackson vs Gson
Creating a Custom Starter with Spring Boot
The DAO with JPA and Spring
Getting a File’s Mime Type in Java
Mệnh đề Switch-case trong java
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Function trong Java 8
Java Program to subtract two large numbers using Linked Lists
Checking for Empty or Blank Strings in Java
How to Remove the Last Character of a String?
Java Program to Implement HashMap API
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Java Program to Implement Binomial Tree
Các kiểu dữ liệu trong java
Generate Spring Boot REST Client with Swagger