Home » Infrastructure » Linux » Unable to send email from Java in Linux (Red Hat Enterprise Linux Server release 6.6 )
Unable to send email from Java in Linux [message #648277] Fri, 19 February 2016 10:43 Go to next message
megha2525
Messages: 62
Registered: June 2012
Location: columbus
Member
Hi All,

We recently moved to Red Hat Enterprise Linux Server release 6.6 from Solaris 10.
The existing Java code to send emails in Solaris is

public class card_cardMessenger{

	/**
	* Send an e-mail message via the Runtime class
	* @see Runtime
	* @return boolean (success or failure of sending the mail)
	* @param String recipient
	* @param String subject
	* @param String e-mail message
	*/
	public static boolean sendMessage(String recip, String subj, String msg) {
		try {		
			Runtime rt = Runtime.getRuntime();
			Process pr = rt.exec("mail -tw " + recip);
		  	OutputStreamWriter out = new OutputStreamWriter(pr.getOutputStream());
			out.write("Subject: " + subj + "\n");
			out.write(msg);
			out.flush();
			out.close();
			return true;
		}
		catch (Exception e) {
			System.out.println("An exception occurred while sending message: " + e);
			e.printStackTrace();
			return false;
		}
	}


}


However, the above code doesn't work in Linux - An email is not sent to the mail recipient.
Hence, I modified the mail command in the above code to
Process pr = rt.exec("mail -s " + recip);

There is one issue with the above code. Now, an email is sent to the mail recipient but the subject line in the email is blank. The subject line appears in the body of the email.

Please let me know the right way to do this. I also read other thread on this site and tried to send an email from the command line using
mail -s "Some subject line" test@yahoo.com 

This works but the same doesn't work from the Java code.

Please help.

Thanks,
Megha


Re: Unable to send email from Java in Linux [message #648288 is a reply to message #648277] Fri, 19 February 2016 12:15 Go to previous message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

This question bears no real connection with Oracle database.
Please find a more appropriate forum.
The topic is locked. /forum/fa/448/0/
If I'm wrong, please, PM me (or report this message to a moderator, explain why you think it should be unlocked and it might be done).

Previous Topic: Unable to get data into a CSV file
Next Topic: patch
Goto Forum:
  


Current Time: Fri Mar 29 10:48:05 CDT 2024