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:
Receive email using IMAP
Most commonly used String methods in Java
Java Program to Perform Addition Operation Using Bitwise Operators
Java Program to Implement Ternary Heap
Hướng dẫn sử dụng Java Annotation
Hamcrest Collections Cookbook
Java Program to Check Whether Graph is DAG
Java Program to Check if it is a Sparse Matrix
4 tính chất của lập trình hướng đối tượng trong Java
Java Timer
So sánh ArrayList và LinkedList trong Java
Java Program to Evaluate an Expression using Stacks
Java Program to Solve Knapsack Problem Using Dynamic Programming
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Send email with SMTPS (eg. Google GMail)
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Jackson JSON Views
Java Program to Implement D-ary-Heap
Java Convenience Factory Methods for Collections
Converting Between Byte Arrays and Hexadecimal Strings in Java
Java Program to Represent Graph Using Incidence Matrix
Quick Guide to the Java StringTokenizer
Java Program to Implement Skew Heap
Java Program to Implement Find all Back Edges in a Graph
Tips for dealing with HTTP-related problems
Java Program to Implement Ternary Search Tree
A Guide to Java SynchronousQueue
Get the workstation name or IP
Java Program to Generate All Possible Combinations of a Given List of Numbers
Spring Boot - Enabling Swagger2
Getting the Size of an Iterable in Java
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle