Q_Q
Q_Q

Reputation: 21

Connections with JavaMail

I am working with JavaMail for my plugin, within this plugin I am trying to send an email but my issue lies around the client.
The client can't handle the plugin connecting to the email server and sending an email it either crashes the entire server or the client gets kicked out.
My fix for this was instead of constantly connecting to the email server and sending an email why not simply keep one connection open when the plugin starts and grab that connection when I am wanting to send an email hopefully this will help in allowing the client and server to stay stable without any crashes.
If anyone can help me I am just curious on how I can go about keeping a a single connection open and grabbing it when it is needed and then closing it when the plugin gets disabled.

What I have tried:

private Session session;

public void connect() {

    String provider = plugin.getConfig().getString("EmailProvider");
    String email = plugin.getConfig().getString("Email");
    String password = plugin.getConfig().getString("Password");

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

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

private boolean checkSession() {
    try {
        if (session != null) {
            return true;
        } else {
            return false;
        }
    }
    return false;
}

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

    if (!checkSession()) {
        connect();
        System.out.println("connecting");
    }
    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        message.setSubject(subject);
        message.setText(text);

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

Upvotes: 0

Views: 1685

Answers (1)

Bill Shannon
Bill Shannon

Reputation: 29971

The simple answer is, you can't.

There's no way to force a connection to remain open. Connections can be closed for all sorts of reasons and your program needs to be prepared for that.

You can, however, cache an open connection and reuse it as long as it's still connected. But without knowing what your plugin plugs in to, it's hard to describe the best strategy for that.

Note also that mail servers really don't want you to keep a connection open if you're not using it, which is why they'll close it out from under you if you leave it open but idle for too long. And artificially keeping the connection active won't win you any points with the mail server either.

If your mail server crashes when you connect to it, it's probably time to get a new mail server.

Upvotes: 2

Related Questions