EN VI

Java - I can not send attachment outside of NetBeans?

2024-03-13 08:30:06
Java - I can not send attachment outside of NetBeans

I have a problem, which is that when I send an attachment via Gmail inside NetBeans, the program works and sends messages as it should, but when I try to run the jar file from NetBeans, the program does not show the dialog box that prints the number 3. This means that the problem starts from this line MultiPartEmail email = new MultiPartEmail(); The program remains in execution without throwing the exception or printing the number 3.

Knowing that the program uses several libraries, all of which work as they should From jar file.

I also tried a library javax-mail.jar. I encountered the same problem, as the program works well inside NetBeans, but the jar file does not work in sending messages only.

This is my Full class code:

package FTPEmail;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;

import java.io.IOException;

public class SendAttachment {

public SendAttachment(String attachment, String subject, String msg) throws IOException {
    final String sender = "[email protected]";
    final String receiver = "[email protected]";
    final String password = "abc123abc123abc1";
    Tools.Text.dialogErrorNumber("1");
    try {
        EmailAttachment emailAttachment = new EmailAttachment();
        emailAttachment.setPath(attachment);
        emailAttachment.setDisposition(EmailAttachment.ATTACHMENT);
        emailAttachment.setDescription("Attachment");
        Tools.Text.dialogErrorNumber("2");
        MultiPartEmail email = new MultiPartEmail();
        Tools.Text.dialogErrorNumber("3");
        email.setHostName("smtp.gmail.com");
        Tools.Text.dialogErrorNumber("4");
        email.setSmtpPort(465);
        Tools.Text.dialogErrorNumber("5");
        email.setAuthenticator(new DefaultAuthenticator(sender, password));
        Tools.Text.dialogErrorNumber("6");
        email.setSSLOnConnect(true);
        Tools.Text.dialogErrorNumber("7");
        email.addTo(receiver);
        Tools.Text.dialogErrorNumber("8");
        email.setFrom(sender);
        Tools.Text.dialogErrorNumber("9");
        email.setSubject(subject);
        Tools.Text.dialogErrorNumber("10");
        email.setMsg(msg);
        Tools.Text.dialogErrorNumber("11");

        // attach the file
        email.attach(emailAttachment);
        Tools.Text.dialogErrorNumber("12");
        // send the email
        email.send();
    } catch (EmailException e) {
        Tools.Text.dialogErrorNumber(e.getMessage());
        Tools.Text.dialogErrorNumber("0060");
    }
}
}

Solution:

You are wrapping your code with a try/catch - good.

However you are catching a specific exception EmailException which my guess is that this is not getting thrown, so the exception bubbles up somewhere else.

If you changed EmailException to Exception, my guess is that you still won't catch the problem because my guess is that when you are running code outside of the NetBeans environment, some dependency is missing and this kind of error is thrown as a subclass of java.lang.Error - probably LinkageError. If you study the class hierarchy carefully, you will find that these errors are not subclasses of Exception.

My recommendation is to change the try/catch to catch Throwable... the highest point in the error/exception tree, then you will find out what is really going on.

Then you'll find that some dependency is missing which NetBeans is implicitly adding into the classpath for you.

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login