6

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: from@email.de" to@email.de

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.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
alexander
  • 1,191
  • 2
  • 20
  • 40

2 Answers2

3

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.

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
  • Yes, postfix is configured and installed correctly (I did not do that! :)) Is there a way to use JavaMail in combination with postfix? If not, ProcessBuilder is the best option, isn't it? – alexander Apr 17 '15 at 15:50
  • Perfectly! To bad, I need to use Postfix. – alexander Apr 17 '15 at 15:57
  • FYI: I am using JavaMail, now. Postfix is using a smtp-server, for sending emails. I did not know that. I asked a server-admin about this and he said, that both options are valid (sending emails via postfix and run the linux command) or use JavaMail. – alexander Apr 21 '15 at 10:43
1

I don't think using ProcessBuiler and running executable commands for sending emails is called for, when JavaMail has already provided an API for mail (sending/receiving) clients such as your webapp. Assuming you have set up a mail server like SMTP, you can follow a tutorial like this to do this more seamlessly.

Kedar Mhaswade
  • 4,535
  • 2
  • 25
  • 34
  • I need to invoke this `mail` command. Is that possible with with JavaMail? Postfix is installed, is that a SMTP-Server? – alexander Apr 18 '15 at 15:44
  • Oh, I thought you 'need to send email'. If you need to send the email via your webapp, and you have postfix installed somewhere to which you have access, then yes, JavaMail can help. – Kedar Mhaswade Apr 18 '15 at 20:06
  • Well, I finally got it working. I found a mailserver in the postfix configuration. It is just simple! Thank you, for the hint! – alexander Apr 18 '15 at 21:12
  • I think you did not answer the question, he has the same doubt as I do, he wants to know how to use JavaMail to send e-mails (SMTP) but instead of sending directly you send thought Postfix. – José Mendes Nov 16 '16 at 14:33
  • @KedarMhaswade can u explain please how call from java to postfix? thanks! – Alberto Acuña Aug 27 '18 at 08:36
  • @AlbertoAcuña just use the JavaMail API and configure it to talk to your postfix installation. – Kedar Mhaswade Aug 27 '18 at 21:25
  • @KedarMhaswade can u share some tutorial i really cant understand how do it, i just have this downloaded -> postfix-3.3.1.tar.gz – Alberto Acuña Aug 28 '18 at 07:59
  • Maybe something like [this](https://blog.eduonix.com/java-programming-2/learn-use-java-mail-api-send-receive-emails/), @AlbertoAcuña? – Kedar Mhaswade Aug 29 '18 at 12:30