In this video, we will be learning how to send an email with an attachment using Java and Gmail in Hindi. We will be using the JavaMail API library to accomplish this task.

First, we will start by adding the JavaMail API dependency to our project. This can be done by adding the following code to our build file.

Next, we will go over the code for sending an email with an attachment. We will be using the Properties class to set up our email server properties, and the Session class to create a session for sending the email.

We will then use the MimeMessage class to create the email message, and the MimeBodyPart and Multipart classes to attach the file to the email.

We will also show how to handle any errors that may occur during the sending process and how to authenticate the Gmail account that is being used to send the email.

This video will be a step-by-step guide for sending an email with an attachment using Java and Gmail in Hindi, and will be useful for beginners and experienced programmers alike.

 

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>

 

GEmailSender.java



import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeBodyPart;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.internet.MimeMultipart;

import java.io.File;
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 = "";
        String password = "";


        //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;
    }

    public boolean sendEmailWithAttachment(String to, String from, String subject, String text, File file) {

        boolean flag = false;
        //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 = "";
        String password = "";


        //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);

            MimeBodyPart part1 = new MimeBodyPart();
            part1.setText(text);

            MimeBodyPart part2 = new MimeBodyPart();
            part2.attachFile(file);

            MimeMultipart mimeMultipart = new MimeMultipart();
            mimeMultipart.addBodyPart(part1);
            mimeMultipart.addBodyPart(part2);

            message.setContent(mimeMultipart);

            Transport.send(message);
            flag = true;


        } catch (Exception e) {
            e.printStackTrace();
        }

        return flag;

    }

}

 

APP.JAVA

 



import com.concepts.email.GEmailSender;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class App {
    public static void main(String[] args) throws FileNotFoundException {

        GEmailSender gEmailSender = new GEmailSender();
        String to = "[email protected]";
        String from = "[email protected]";
        String subject = "Email with Text and File";
        String text = "This is a invoice with this email you can find out.";
        File file = new File("G:\\unnamed.jpg");


        boolean b = gEmailSender.sendEmailWithAttachment(to, from, subject, text, file);
        if (b) {
            System.out.println("Email is sent successfully");
        } else {
            System.out.println("Error in sending email !!");
        }


        //        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");
//        }


    }
}