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:
Simplify the DAO with Spring and Java Generics
Working with Kotlin and JPA
Guide to Escaping Characters in Java RegExps
Tính đóng gói (Encapsulation) trong java
Check If Two Lists are Equal in Java
How to Manually Authenticate User with Spring Security
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Java Program to Implement ArrayList API
Java Program to Implement Disjoint Sets
Spring Boot - Sending Email
Java Program to Construct an Expression Tree for an Postfix Expression
Get the workstation name or IP
Java Stream Filter with Lambda Expression
Count Occurrences of a Char in a String
wait() and notify() Methods in Java
Functional Interfaces in Java 8
Đồng bộ hóa các luồng trong Java
Java Program to Perform the Shaker Sort
Concurrent Test Execution in Spring 5
Java Program to Implement Hopcroft Algorithm
Hướng dẫn Java Design Pattern – Intercepting Filter
Interface trong Java 8 – Default method và Static method
Java – Convert File to InputStream
Java Program to Implement Sorted List
Java Program to Implement Jarvis Algorithm
A Guide to Java HashMap
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Lớp Collections trong Java (Collections Utility Class)
A Custom Data Binder in Spring MVC
Java Program to Perform Right Rotation on a Binary Search Tree
List Interface trong Java
Guide to Guava Table