Skip to content
Advertisement

How to send mail using Java in a Linux environment?

I have successfully sent mail using Java using apache james server in Windows. I have been trying to do the same in Linux, but couldn’t start the james server. I am a novice in Linux.

Is james server really necessary for sending mails? If so how to start james server in Linux?

Advertisement

Answer

For sending emails in java you can use the java mail api: http://www.oracle.com/technetwork/java/javamail/index.html
See this tutorial for infos about using it: http://www.tutorialspoint.com/java/java_sending_email.htm

They are using a mail server on localhost, but i wouldn’t recommend that. It is perfectly alright for testing or if your localhost is a server with proper DNS records. If you send emails from a IP where DNS reverse lookup does not work, chances are that your mail will be stopped by a spam filter.

If you want a mailserver on localhost you could install exim, postfix, ssmtp or any other MTA from your linux distribution. It’s possible that one of them is already installed and running.

I would use some other mail server for example the one you are using to send emails. This line of code from the tutorial configures the host:

  // Setup mail server
  properties.setProperty("mail.smtp.host", host);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement