Search code examples
javaxpagesjakarta-mailemail-attachments

Xpages - java.lang.NoSuchMethodError: javax/mail/internet/MimeBodyPart.attachFile(Ljava/io/File;)V


I want to include attachments when email is sent to a recipient. I have been able to set and get the attachments but when the attachFile method is called from the Javaxmail I get an error.

This code is written in Java using the javax library.

Everything seems fine, but fails. I've tried adding encoding as well but same results.

This code fails when the attachFile method is called.

attachPart.attachFile(f); // fails here...


try {
Message msg = new MimeMessage(propsSess);
msg.setFrom(new InternetAddress(this.defaultSenderAddress));

setRecipient(msg, this.sendTo, "to");
msg.setSubject(this.subject);
msg.setContent(this.bodyHtml, "text/html");
msg.setHeader("X-Mailer", "Java Agent");
msg.setSentDate(new Date());

if (!this.bodyAttach.isEmpty()) {
    BodyPart messageText = new MimeBodyPart();
    messageText.setText(this.bodyHtml);

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageText);

    // adds attachments 
    for (int index = 0; index < this.bodyAttach.size(); index++) {
        MimeBodyPart attachPart = new MimeBodyPart();
        try {
            System.out.println(this.bodyAttach.get(index)); // shows only 1st attachments
            File f = new File(this.bodyAttach.get(index));

            attachPart.attachFile(f); // fails here...
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        multipart.addBodyPart(attachPart);
    }

    // sets the multi-part as e-mail's content
    msg.setContent(multipart);
}
SMTPTransport transport = (SMTPTransport) propsSess.getTransport("smtp");
transport.connect(this.smtpServerAddress, null, null);
if (transport.isConnected()) {
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();
    this.processMessage.add("success");
}
} catch (Exception e) {
this.processMessage.add(databaseTitle + ": SmtpMail bean: Message or SMTPTransport error");
return false;
}

Solution

  • javax.mail is something of a land mine on Domino. It's kind of present, by way of "mail.jar" in the "ndext" directory as well as the com.ibm.designer.lib.javamail plugin in OSGi land, but that is version 1.3 of the spec. The method you're trying to use there was, unfortunately, added in 1.4.

    You may be able to get around it by adding a jar for a newer version of javax.mail+its implementation to your NSF, but I haven't tried that so I can't say for sure. That may be what you're already trying anyway, to get access to the classes. You could also try putting this code in an OSGi plugin and embedding the javax.mail jar inside that or bringing it along as a version-range-constricted external OSGi plugin.

    You could alternatively use the Notes API to send email, though then you'd be using the Domino server to route the email and not a custom SMTP server (unless Domino happens to be configured to route through that server anyway).