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:
Guide to PriorityBlockingQueue in Java
Jackson Ignore Properties on Marshalling
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Guide to Guava Multimap
A Guide to BitSet in Java
Java Program to Implement Self Balancing Binary Search Tree
Java Program to Implement D-ary-Heap
Java Program to Implement Floyd Cycle Algorithm
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
How to Replace Many if Statements in Java
Jackson – Bidirectional Relationships
Java Program to Implement Miller Rabin Primality Test Algorithm
Spring Boot - Code Structure
Java Program to Implement ArrayDeque API
Spring REST API + OAuth2 + Angular
Java Program to Construct an Expression Tree for an Postfix Expression
Spring @Primary Annotation
Lập trình đa luồng với Callable và Future trong Java
Hướng dẫn Java Design Pattern – Composite
Java Program to Check for balanced parenthesis by using Stacks
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Immutable Objects in Java
Generate Spring Boot REST Client with Swagger
Jackson JSON Views
Disable DNS caching
Spring Data Java 8 Support
Reactive WebSockets with Spring 5
Jackson vs Gson
Java – Random Long, Float, Integer and Double
Spring Security and OpenID Connect
Spring Cloud AWS – RDS
Giới thiệu HATEOAS