2

while sending a mail with pdf attachment in amazon server using javamail API, Its throwing null pointer exception in the logs. But same code is working in local.

public void sendMail(final String mailTo, final String mailSubject, final String mailText, final String filePath, final String fileName) {
    logger.info("Inside sendMail Method...");
    final Properties config = createConfiguration();

    // Creates a mail session. We need to supply username and password for Gmail authentication.
    final Session session = Session.getInstance(config, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(mailFrom, mailPassword);
        }
    });

    // Creates email message
    final MimeMessage message = new MimeMessage(session);
    try {
        message.setFrom(new InternetAddress(mailFrom));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
        message.setSubject(mailSubject);

        final BodyPart messagePart = new MimeBodyPart();
        messagePart.setContent(mailText, contentType);

        final MimeMultipart multipart = new MimeMultipart();
        multipart.addBodyPart(messagePart);

        if (filePath != null) {
            final MimeBodyPart attachmentPart = new MimeBodyPart();
            final URL url;
            try {
                url = new URL(filePath);
                final DataSource source = new URLDataSource(url);
                attachmentPart.setDataHandler(new DataHandler(source));
                attachmentPart.setFileName(fileName);
                multipart.addBodyPart(attachmentPart);
            } catch (MalformedURLException e) {
                logger.error("Malformed URL Exception: " + e.getMessage());
            }
        }
        message.setContent(multipart);
        // Send a message
        Transport.send(message);
        logger.info("Mail triggered successfully");
    } catch (final AddressException e) {
        logger.error("Address Exception: " + e.getMessage());
    } catch (final MessagingException e) {
        logger.error("Messaging Exception: " + e.getMessage());
    }
}

Please find below the exception generated on amozon server application log.

2014-03-20 19:01:30,936 [DefaultQuartzScheduler_Worker-2] INFO            net.app.api.jobs.MailJob - Error in triggering the mail : null
java.lang.NullPointerException
at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:226)
at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:299)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1375)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021)
at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:419)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1354)
at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2107)
at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2075)
at javax.mail.Transport.send(Transport.java:123)
at net.app.api.mail.MailTrigger.sendMail(MailTrigger.java:104)
at net.app.api.jobs.MailJob.execute(MailJob.java:41)
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)

Anybody please provide some feasible solution to this. Thanks in advance.

user1829110
  • 23
  • 1
  • 1
  • 4

2 Answers2

1

The problem may be that you can't access the URL in filePath.

Is filePath a "file:" URL? If so, why not just use a FileDataSource?

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • The file path in amazon server is like "https://www.exmaple.com/documents/samplefile.pdf" and we are able to access this filepath locally through browser as well as through code base. I tried with FileDataSource also but given the same error. Any other way can you please suggest ? Thanks. – user1829110 Mar 21 '14 at 06:58
  • The file is on the same machine as the JavaMail application, right? Assuming it's really a file stored on the server... Make sure you can access the file using a FileInputStream in your application, then use a FileDataSource when creating the message. If you can't access the file using a FileInputStream, you'll need to debug that problem. If it's not really a file stored on the server, but is just a resource accessed using a URL, make sure you can access it using URLConnection.getInputStream. – Bill Shannon Mar 21 '14 at 20:12
  • I had the same stack trace... after updating to a recent mail.jar, I got a more useful message - the file didn't exist. – Tilman Hausherr Apr 11 '17 at 11:21
0

I have changed the attachment code base part as

final MimeBodyPart attachmentPart = new MimeBodyPart();
final URL url;
try {
    url = new URL(filePath);
    final DataSource source = new URLDataSource(url);
    attachmentPart.setDataHandler(new DataHandler(source));
    attachmentPart.setFileName(fileName);
    attachmentPart.setDisposition(Part.ATTACHMENT);
    attachmentPart.setHeader("Content-Transfer-Encoding", "base64");
    multipart.addBodyPart(attachmentPart);
} catch (final MalformedURLException e) {
    logger.error("Malformed URL Exception: " + e.getMessage());
}

The null pointer exception issue got resolved and got a new exception as SSLProtocol Exception and came to know the difference on java jdk's installed in both machines as 1.6 in local machine and 1.7 in amazon cloud. So followed SSL handshake alert: unrecognized_name error since upgrade to Java 1.7.0 and now the mail is triggering in both servers as expected.

Community
  • 1
  • 1
user1829110
  • 23
  • 1
  • 1
  • 4