Send email in via Java using Postfix
Asked Answered
P

2

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: [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.

Preciosa answered 17/4, 2015 at 15:38 Comment(0)
C
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.

Canning answered 18/4, 2015 at 15:1 Comment(8)
I need to invoke this mail command. Is that possible with with JavaMail? Postfix is installed, is that a SMTP-Server?Preciosa
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.Canning
Well, I finally got it working. I found a mailserver in the postfix configuration. It is just simple! Thank you, for the hint!Preciosa
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.Sambar
@KedarMhaswade can u explain please how call from java to postfix? thanks!Binny
@AlbertoAcuña just use the JavaMail API and configure it to talk to your postfix installation.Canning
@KedarMhaswade can u share some tutorial i really cant understand how do it, i just have this downloaded -> postfix-3.3.1.tar.gzBinny
Maybe something like this, @AlbertoAcuña?Canning
C
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.

Colloquium answered 17/4, 2015 at 15:48 Comment(3)
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?Preciosa
Perfectly! To bad, I need to use Postfix.Preciosa
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.Preciosa
C
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.

Canning answered 18/4, 2015 at 15:1 Comment(8)
I need to invoke this mail command. Is that possible with with JavaMail? Postfix is installed, is that a SMTP-Server?Preciosa
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.Canning
Well, I finally got it working. I found a mailserver in the postfix configuration. It is just simple! Thank you, for the hint!Preciosa
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.Sambar
@KedarMhaswade can u explain please how call from java to postfix? thanks!Binny
@AlbertoAcuña just use the JavaMail API and configure it to talk to your postfix installation.Canning
@KedarMhaswade can u share some tutorial i really cant understand how do it, i just have this downloaded -> postfix-3.3.1.tar.gzBinny
Maybe something like this, @AlbertoAcuña?Canning

© 2022 - 2024 — McMap. All rights reserved.