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 Cloud – Bootstrapping
Chương trình Java đầu tiên
Lớp TreeMap trong Java
Spring MVC Custom Validation
Logout in an OAuth Secured Application
Overview of the java.util.concurrent
Guava Collections Cookbook
Java Program to Check Whether Graph is DAG
Java Program to Check whether Undirected Graph is Connected using DFS
String Initialization in Java
Spring NoSuchBeanDefinitionException
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Program to Perform integer Partition for a Specific Case
Java Program to Implement Tarjan Algorithm
Beans and Dependency Injection
Java Program to Implement Efficient O(log n) Fibonacci generator
Sorting Query Results with Spring Data
Spring Boot Security Auto-Configuration
Java Program to Implement Sorted Circularly Singly Linked List
Introduction to Spring Cloud CLI
Lớp lồng nhau trong java (Java inner class)
The StackOverflowError in Java
Mapping Nested Values with Jackson
Java Program to Perform the Unique Factorization of a Given Number
Disable Spring Data Auto Configuration
Java Program to Find the Minimum value of Binary Search Tree
Java Program to Convert a Decimal Number to Binary Number using Stacks
Getting Started with Custom Deserialization in Jackson
Introduction to the Java ArrayDeque
Apache Camel with Spring Boot
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Java Program to Perform Left Rotation on a Binary Search Tree