2

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;
}
3
  • How has javax mail library code been added to the NSF / server? Stack traces are always helpful for troubleshooting too. Also, verify the method you're using is expecting a java.io.File. Aug 20, 2019 at 10:22
  • This link provides ways to send an attachment . See if this link helps you. programcreek.com/java-api-examples/… Aug 20, 2019 at 10:30
  • That is what I have been using, but I have just reached a brick wall. Aug 20, 2019 at 10:46

1 Answer 1

2

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.