Home > Back-end >  Java Mail Application
Java Mail Application

Time:10-14

I am just trying to create a simple Mail Application in Java. And have found out how to code it, but I seem to have some problems with my imports.

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

IntelliJ recommends adding Jave EE 6 JARs to module dependencies. But it just did not help! Maybe you know what to do.

CodePudding user response:

Download JavaMail Release

In addition, the JavaMail jar files are published to the Maven repository. The main JavaMail jar file, which is all most applications will need, can be included using this Maven dependency:

If you use Maven,

    <dependencies>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>
    </dependencies>

You can find all of the JavaMail jar files in both the java.net Maven repository, and in Maven Central.

Otherwise download the javax.mail jar and add it to your libs/classpath:

https://maven.java.net/content/repositories/releases/com/sun/mail/javax.mail/1.6.2/javax.mail-1.6.2.jar

CodePudding user response:

The Answer by Mustafa seems correct but is outdated.

Jakarta EE

JavaEE was owned by Oracle Corporation. JavaMail was part of JavaEE, one of the few dozen technologies housed there.

Years ago, Oracle transferred ownership to the Eclipse Foundation.

  • The name changed from JavaEE to Jakarta EE.
  • JavaMail changed its name to Jakarta Mail.
  • In the latest versions, the package names have changed from javax.* to jakarta.*.

Jakarta Mail

Spec

Find the Jakarta Mail specifications list. The current version is Jakarta Mail 2.0.

Implementation

At least one implementation exists: Eclipse Jakarta Mail.

Others are free to write implementations as well. I do not know if anyone has or not.

Example

I have not used Jakarta Mail myself, but your Question made me curious. So I put this dependency:

        <!-- https://mvnrepository.com/artifact/com.sun.mail/jakarta.mail -->
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>jakarta.mail</artifactId>
            <version>2.0.1</version>
        </dependency>

… into this Maven POM:

<?xml version="1.0" encoding="UTF-8"?>

<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>work.basil.example</groupId>
    <artifactId>JMail</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>JMail</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>17</maven.compiler.release>
    </properties>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.sun.mail/jakarta.mail -->
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>jakarta.mail</artifactId>
            <version>2.0.1</version>
        </dependency>

    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M5</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>3.0.0-M1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>3.0.0-M1</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.9.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.1.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

… to create a little project. I added the Registry class from here, a link provided here.

Minor note: I fixed a typo to change the class name from registry to Registry per Java naming conventions.

Notice the imports on that file use jakarta.* package naming.

import java.util.*;

import jakarta.mail.*;
import jakarta.mail.internet.*;

That Registry class compiles, but I did not run it.

If you are writing a standalone app, you need to include a JAR containing an implementation. You would ship that JAR as part of your app, provided you can abide by its license.

If you are writing a web app or web service to be deployed on a Jakarta EE service, then you would change your Maven POM to use the Jakarta Mail APIs alone without an implementation. The implementation would be provided at runtime by the Jakarta EE compliant server product you chose for deployment.

  • Related