Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send email in via Java using Postfix

I am currently developing a web application based on Java EE, JSF, EJB etc. This application is deployd on a Debian 7.6 and there is Postfix installed.

I can use something like this to send emails via commandline:

/usr/bin/mailx -s "SUBJECT" -a "From: [email protected]" [email protected]

This is working so far.

My attempt is, to use this line and run it as linux command in Java. Is that correct?

Can I use JavaMail for this (send email via postfix) and if so, how can I set up the config for this?

I am really struggeling with this part of my webapplication. Maybe you can help me to find out, what the best solution is.

like image 673
alexander Avatar asked Sep 29 '22 06:09

alexander


1 Answers

You can use the ProcessBuilder class to execute the mailx command from within your Java program. You don't need to use JavaMail if all you want to do is execute the existing mailx command from within a Java program (assuming that all required infrastructure for sending an email using postfix is already in place)

You need to understand that JavaMail is a set of APIs (classes, interfaces, methods) that allow you to implement email functionality within a Java application. You either use ProcessBuilder to execute the mailx command or you completely scrap this idea and use the JavaMail API. You don't use both these together.

like image 72
Chetan Kinger Avatar answered Nov 02 '22 23:11

Chetan Kinger