Unable to send email from Java in Linux [message #648277] |
Fri, 19 February 2016 10:43 |
|
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
|
|
|
|