In this video we are going to learn how to send email using java and gmail without less secure app 2023 step by step.
POM.XML
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.concepts</groupId>
<artifactId>CoreMaven</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>CoreMaven</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/jakarta.mail/jakarta.mail-api -->
<dependency>
<groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail-api</artifactId>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.mail/jakarta.mail -->
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>jakarta.mail</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
GEmailer.java
import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import java.util.Properties;
public class GEmailSender {
public boolean sendEmail(String to, String from, String subject, String text) {
boolean flag = false;
//logic
//smtp properties
Properties properties = new Properties();
properties.put("mail.smtp.auth", true);
properties.put("mail.smtp.starttls.enable", true);
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.host", "smtp.gmail.com");
String username = "youremailusername";
String password = "jfrgaungfubodnni";
//session
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setFrom(new InternetAddress(from));
message.setSubject(subject);
message.setText(text);
Transport.send(message);
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
}
Main.java
import com.concepts.email.GEmailSender;
public class App {
public static void main(String[] args) {
GEmailSender gEmailSender = new GEmailSender();
String to = "[email protected]";
String from = "[email protected]";
String subject = "Second: Sending email using GMail";
String text = "This is a example email send using gmail and java program with out less secure app";
boolean b = gEmailSender.sendEmail(to, from, subject, text);
if (b) {
System.out.println("Email is sent successfully");
} else {
System.out.println("There is problem in sending email");
}
}
}
checkout out preminum courses:
https://courses.learncodewithdurgesh.com/learn