Java Mail API — Send Email from Java Program — (Gmail Example — 2021)
In this video, we will see how to send email from your java program. We will use Java Mail API for sending emails. Java mail API can send email in text format or HTML format. If you would like to style an html email, you can add embedded CSS.
Gmail SMTP Host: smtp.gmail.com
Gmail SMTP Port: 587
Download Java Mail API: https://javaee.github.io/javamail/
Enable signin from less secure apps in Gmail:
https://myaccount.google.com/security
Youtube video (tutorial ) : https://youtu.be/A7HAB5whD6I
I tried it in javaFx application to send appointement email reminder for clients , this screenshot from my gmail demonstrate the e-mail I received :
source code that I used ( you can use websites like imgur.com to send images links in your code ) :
JavaFx application — Java Mail API — Email Service that sends emails for clients (in an online bike shop ) to tell them about thier next appointement.
/** -- File title : Velo/src/com/Velo/EmailService/JavaMailSender.java -- **/package com.Velo.EmailService;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class JavaMailSender {
public static void main(String[] args) {
String host="→myemailaddress@gmail.com←"; //← my email address
final String user="→myemailaddress@gmail.com←";//← my email address
final String password="→mypassword←";//change accordingly //← my email password
String to="→Destination-emailaddress@gmail.com←";//→ the EMAIL i want to send TO →
// session object
Properties props = new Properties();
props.put("mail.smtp.ssl.trust", "*");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});
//My message :
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(" NOTIFICATION APPOINTEMENTT !!! ");
//Text in emial :
//message.setText(" → Text message for Your Appointement ← ");
//Html code in email :
message.setContent(
"<h1 style =\"color:red\" >DON'T MISS YOUR APPOINTEMENT !! </h1> <br/> <img width=\"50%\" height=\"50%\" src=https://i.imgur.com/iYcBkOf.png>",
"text/html");
//send the message
Transport.send(message);
System.out.println("message sent successfully via mail ... !!! ");
} catch (MessagingException e) {e.printStackTrace();}
}
}
— — — — — — — — — — — — — — — — — — — — — — — — — — — — —
JavaFx Mail Sender + Java Mail API :
Source → :
https://github.com/purgoufr/JavaFX-Mail-Sender
Add javax.mail.jar to your project library :
mailsender.java :
// Source → : https://github.com/purgoufr/JavaFX-Mail-Sender
// add javax.mail.jar to your project library : https://javaee.github.io/javamail/#Download_JavaMail_Release// https://i.imgur.com/ZUvgbjJ.png
//MailSender.java
import java.io.IOException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class MailSender extends Application {
@Override
public void start(Stage stage) throws IOException {
final String from = "purgoufr@gmail.com";
final String username = "purgoufr@gmail.com";
final String password = "mehmet123";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.user", "username");
props.put("mail.smtp.password", "password");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", true);
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
Group root = new Group();
Scene scene = new Scene(root, 310, 350);
stage.setScene(scene);
stage.setTitle("Mail Sender");
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(25);
grid.setHgap(25);
scene.setRoot(grid);
// "To" Part
TextField toadress = new TextField();
toadress.setPromptText("To");
toadress.setPrefColumnCount(20);
GridPane.setConstraints(toadress, 0, 0);
grid.getChildren().add(toadress);
// "Subject" Part
TextField subject = new TextField();
GridPane.setConstraints(subject, 0, 1);
grid.getChildren().add(subject);
subject.setPromptText("Enter Subject");
subject.setPrefColumnCount(20);
subject.setPrefHeight(20);
// "Body" Part
TextArea body = new TextArea();
body.setPrefRowCount(10);
body.setPrefColumnCount(100);
body.setWrapText(true);
body.setPrefWidth(150);
body.setPromptText("Some text");
GridPane.setConstraints(body, 0, 2);
String cssDefault = "";
body.setText(cssDefault);
grid.getChildren().add(body);
// Button Part
Button btn = new Button();
grid.getChildren().add(btn);
btn.setText("Send");
GridPane.setConstraints(btn, 0, 4);
GridPane.setHalignment(btn, HPos.RIGHT);
stage.show();
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
DropShadow shadow = new DropShadow();
btn.setEffect(shadow);
stage.setScene(scene);
String text = toadress.getText();
String text1 = subject.getText();
String text2 = body.getText();
stage.show();
try {
// Create a default MimeMessage object :Message message = new MimeMessage(session);
// Set From: header field of the header :message.setFrom(new InternetAddress(from));
// Set To: header field of the header :
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(text));
// Set Subject: header field :message.setSubject(text1);
// Now set the actual message :message.setText(text2);
// Send messageTransport.send(message);
System.out.println("Sent message successfully.");))} catch (MessagingException e) {
)System.out.println("Sent message failed.");
)e.printStackTrace();
}
}
});
}
public static void main(String[] args) {
launch(args);
}
}