JavaMail Maven

Discussion in 'Spigot Plugin Development' started by DisCoder212, May 27, 2018.

  1. I used JavaMail with my plugin and that worked fine a couple of months ago. I now came back to do some more bug testing and for some reason when I try using JavaMail in my plugin the following error is thrown:

    Code (Text):

    Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
     

    My pom.xml current looks like this:
    Code (Text):

        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>

        <repositories>
            <repository>
                <id>spigot-repo</id>
                <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
            </repository>
        </repositories>

        <dependencies>
            <!--Spigot API-->
            <dependency>
                <groupId>org.spigotmc</groupId>
                <artifactId>spigot-api</artifactId>
                <version>1.12.2-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
            <!--Bukkit API-->
            <dependency>
                <groupId>org.bukkit</groupId>
                <artifactId>bukkit</artifactId>
                <version>1.12.2-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
            <!--Activation-->
            <dependency>
                <groupId>jaf</groupId>
                <artifactId>activation</artifactId>
                <version>1.0.2</version>
            </dependency>
            <!--Java Mail-->
            <dependency>
                <groupId>javax.mail</groupId>
                <artifactId>javax.mail-api</artifactId>
                <version>1.6.0</version>
            </dependency>
            <!--Google Authentication-->
            <dependency>
                <groupId>com.warrenstrange</groupId>
                <artifactId>googleauth</artifactId>
                <version>1.1.2</version>
            </dependency>
        </dependencies>

    </project>
     

    Method I am using with JavaMail:
    Code (Text):

    import javax.mail.*;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    import java.util.Properties;

    public class EmailSender {

        private Main plugin;

        public EmailSender(Main plugin) {
            this.plugin = plugin;
        }

        public void sendEmail(String to, String subject, String text) {

            String host = plugin.getConfig().getString("Email-Host");
            String port = plugin.getConfig().getString("Email-Port");
            String username = plugin.getConfig().getString("Email-Username");
            String password = plugin.getConfig().getString("Email-Password");

            Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", port);

            Session session = Session.getInstance(props,
                    new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(username, password); }});

            try {

                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress(username));
                message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
                message.setSubject(subject);
                message.setText(text);

                Transport.send(message);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    }
     
     
  2. electronicboy

    IRC Staff

    You need to configure the maven shade plugin to actually shade in the classes that you need from javamail
     
  3. How can I go about doing that?
     
  4. I added maven shade to my pom.xml but how do I configure it to shade in the classes I need from JavaMail because it still gives off the same error after I added maven shade to my pom.xml
     
  5. Try adding this inside the shade plugin configuration in pom
    Code (Text):
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                        </execution>
                    </executions>
    If that doesn't do it try adding an <artifactSet>
     
  6. Mhm weird its still not being able to shade in the class not sure if I am doing something wrong or what's going on but this is what I currently have in my pom.xml:

    Code (Text):

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <artifactSet>
                        <includes>
                            <include>org.apache.maven:*</include>
                        </includes>
                        <excludes>
                            <exclude>*:maven-core</exclude>
                        </excludes>
                    </artifactSet>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
     
     
  7. <includes> is like a whitelist for what to shade in

    You might need to add javax.mail to it (and probably remove that org.apache.maven since you probably don't want maven in your jar)

    Try removing the artifact set, it might work properly without it, if not change the includes to use that mail package instead
     
  8. Tried it with the artifact set, and included javax.mail it still didn't work:
    Code (Text):

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <artifactSet>
                        <includes>javax.mail</includes>
                    </artifactSet>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
     
    I also did get rid of the artifact set and that also didn't work:

    Code (Text):

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
     
     
    #9 DisCoder212, May 27, 2018
    Last edited: May 27, 2018
  9. You need to specify what you want from that package, eg javax.mail:* for all packages
     
  10. I tried that still is giving off the same error for some reason..