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 Perform the Unique Factorization of a Given Number
Display Auto-Configuration Report in Spring Boot
Removing Elements from Java Collections
Create Java Applet to Simulate Any Sorting Technique
Object cloning trong java
Java Program to Implement LinkedBlockingQueue API
Giới thiệu Java 8
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Intro to Spring Boot Starters
Creating Docker Images with Spring Boot
Create a Custom Auto-Configuration with Spring Boot
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Spring Boot - Creating Docker Image
Marker Interface trong Java
Java Program to Perform Matrix Multiplication
ETL with Spring Cloud Data Flow
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Hướng dẫn Java Design Pattern – Visitor
Converting Java Date to OffsetDateTime
Entity To DTO Conversion for a Spring REST API
Java Convenience Factory Methods for Collections
JUnit5 @RunWith
Wrapper Classes in Java
The DAO with JPA and Spring
Uploading MultipartFile with Spring RestTemplate
Introduction to Thread Pools in Java
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Java Program to Implement Interpolation Search Algorithm
Connect through a Proxy
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
CharSequence vs. String in Java
Java program to Implement Tree Set