Simple email:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
class SimpleMail {
public static void main(String[] args) throws Exception{
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "mymail.server.org");
props.setProperty("mail.user", "emailuser");
props.setProperty("mail.password", "");
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing javamail plain");
message.setContent("This is a test", "text/plain");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("maixuanviet.com@gmail.com"));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}
HTML Email:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
class SimpleHTMLMail {
public static void main(String[] args) throws Exception{
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "mymail.server.org");
props.setProperty("mail.user", "emailuser");
props.setProperty("mail.password", "");
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing javamail html");
message.setContent
("This is a test <b>HOWTO<b>", "text/html; charset=ISO-8859-1");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("maixuanviet.com@gmail.com"));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}
Email with attachment:
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
import java.util.Properties;
class SimpleMailWithAttachment {
public static void main(String[] args) throws Exception{
boolean debug = false;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "mymail.server.org");
props.setProperty("mail.user", "emailuser");
props.setProperty("mail.password", "");
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(debug);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing javamail with attachment");
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent("<h1>Check attachment</h1>", "text/html");
MimeBodyPart attachFilePart = new MimeBodyPart();
FileDataSource fds =
new FileDataSource("SimpleMailWithAttachment.java");
attachFilePart.setDataHandler(new DataHandler(fds));
attachFilePart.setFileName(fds.getName());
Multipart mp = new MimeMultipart();
mp.addBodyPart(textPart);
mp.addBodyPart(attachFilePart);
message.setContent(mp);
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("maixuanviet.com@gmail.com"));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}
Done! Happy Coding!
Related posts:
Java Program to Implement HashSet API
A Guide to Java 9 Modularity
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
New Features in Java 8
Java Program to Check the Connectivity of Graph Using DFS
Java Program to Represent Linear Equations in Matrix Form
Java Program to Implement HashMap API
Java Program to Check whether Directed Graph is Connected using DFS
The StackOverflowError in Java
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Java Program to Find Minimum Element in an Array using Linear Search
Java 8 Collectors toMap
Understanding Memory Leaks in Java
Java Program to Implement VList
A Guide to TreeMap in Java
Map Interface trong java
Java Program to Implement Bubble Sort
Testing in Spring Boot
Using a Mutex Object in Java
How to Add a Single Element to a Stream
A Guide to Iterator in Java
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Java Program to Compute DFT Coefficients Directly
Map to String Conversion in Java
Spring @RequestParam Annotation
Java Program to Implement Dijkstra’s Algorithm using Set
Java Program to Implement Queue
Giới thiệu thư viện Apache Commons Chain
Java Program for Topological Sorting in Graphs
Guide to the Volatile Keyword in Java
A Guide to TreeSet in Java
Tiêu chuẩn coding trong Java (Coding Standards)