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:
Spring MVC Custom Validation
Java Program to Find Transpose of a Graph Matrix
Sort a HashMap in Java
What is Thread-Safety and How to Achieve it?
Spring Cloud AWS – Messaging Support
Consumer trong Java 8
An Intro to Spring Cloud Contract
Java Program to Solve the 0-1 Knapsack Problem
Java Program to Represent Graph Using Incidence List
Java Program to Implement RenderingHints API
Spring Security Logout
Send email with SMTPS (eg. Google GMail)
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Spring @RequestParam Annotation
Java Program to Implement Stack using Linked List
Java Program to Implement EnumMap API
Java Program to Implement Affine Cipher
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Abstract class và Interface trong Java
Tips for dealing with HTTP-related problems
Hướng dẫn Java Design Pattern – Visitor
Giới thiệu Json Web Token (JWT)
Creating a Custom Starter with Spring Boot
Email Authentication and Verification using Node.js and Firebase
Logging in Spring Boot
Netflix Archaius with Various Database Configurations
Hướng dẫn Java Design Pattern – Chain of Responsibility
New Features in Java 11
Cachable Static Assets with Spring MVC
So sánh ArrayList và Vector trong Java
Call Methods at Runtime Using Java Reflection
Calling Stored Procedures from Spring Data JPA Repositories