Send an email through Java/Openmrs Module

Hello, for what i searched there was this module(“Messaging Module”) that is on beta and didnt receive any update since 3 or 4 years ago. I checked through this link, but no idea how that module works…

Any suggestions on how to send an email through Java/ using other module ?

Best Regards

If you just want to send an email from your module, then you do not need the messaging module for that. You could just do like what the reporting module does here: https://github.com/openmrs/openmrs-module-reporting/blob/4135a30c51ba2d15e21dcd8934a08a9d17c83fc2/api/src/main/java/org/openmrs/module/reporting/report/processor/EmailReportProcessor.java

1 Like

OpenMRS core has a messaging service that should be able to send email, there is SMTP related global properties you’d need to set to make it work, don’t know anyone that has been able to use it though.

1 Like

@dkayiwa I tried using the same code from Reporting omod as indicated above for sending email.

I am getting the following class cast exception error, which I think maybe due to classloader issues.

Caused by: java.lang.ClassCastException: com.sun.mail.handlers.text_plain cannot be cast to javax.activation.DataContentHandler at javax.activation.MailcapCommandMap.getDataContentHandler(Unknown Source) at javax.activation.MailcapCommandMap.createDataContentHandler(Unknown Source) at javax.activation.DataHandler.getDataContentHandler(Unknown Source) at javax.activation.DataHandler.writeTo(Unknown Source) at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:261) at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1321) at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1030) at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:416) at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1307) at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2074) at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2042) at javax.mail.Transport.send(Transport.java:117)

Any clue? This seems like the issue reported here

Is it possible to look at the module where you made this change?

Check here - the branch is “refactor-notification”.

Btw, I tried using OMRS MessageSender as well, here. Gives me the same error.

Java mail is a provided dependency to the module

Does it help if you replace this: openmrs-module-appointments/pom.xml at refactor-notification · Bahmni/openmrs-module-appointments · GitHub

With this? openmrs-module-xforms/pom.xml at 4.3.12 · openmrs/openmrs-module-xforms · GitHub

unfortunately no. :frowning:

Strange because i just copied and pasted your code in an existing reference application module and it worked. I run the code in the Activator.started() call back as below:

DefaultMailSender sender = new DefaultMailSender();

sender.send()

Probably it is something to do with the modules in your environment? What is the simplest way of reproducing it without having to load bahmni?

Got it. I am getting other error while connecting to SMTP server, but thats different issue.

So, I started checking other omods and dependencies. Turns out one of the modules were loading up commons-email which I assume uses javax.mail. Unloaded the other module and restart did the job.

Thanks for the help. Let me try to resolve the next steps :slight_smile: