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:
HttpClient 4 Cookbook
The Registration API becomes RESTful
HttpAsyncClient Tutorial
Handling Errors in Spring WebFlux
Find the Registered Spring Security Filters
Serialize Only Fields that meet a Custom Criteria with Jackson
Allow user:password in URL
Java 8 Predicate Chain
Wrapper Classes in Java
New Features in Java 8
Java Program to Generate All Possible Combinations of a Given List of Numbers
Spring Boot - Database Handling
Java Program to Implement Sieve Of Sundaram
ArrayList trong java
Inject Parameters into JUnit Jupiter Unit Tests
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Logout in an OAuth Secured Application
Java Program to Compute Determinant of a Matrix
Jackson – JsonMappingException (No serializer found for class)
Giới thiệu Aspect Oriented Programming (AOP)
Java Program to Check whether Graph is Biconnected
Java Program to Implement Hash Tree
Tránh lỗi NullPointerException trong Java như thế nào?
Converting Between Byte Arrays and Hexadecimal Strings in Java
Java Program to Implement Interval Tree
A Guide to the Java LinkedList
Spring Autowiring of Generic Types
Registration with Spring Security – Password Encoding
Java Program to Implement LinkedTransferQueue API
Sorting in Java
Using Custom Banners in Spring Boot
Java Program to Solve any Linear Equations