Unable to send email using Java in Linux


 
Thread Tools Search this Thread
Top Forums Programming Unable to send email using Java in Linux
# 1  
Old 02-19-2016
Unable to send email using Java in Linux

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


Code:
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
Code:
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
Code:
mail -s "Some subject line" test@yahoo.com

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

Please help.

Thanks,
Megha
# 2  
Old 02-20-2016
Quote:
Originally Posted by Meghan2525
...
Code:
Process pr = rt.exec("mail -s " + recip);

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

...tried to send an email from the command line using
Code:
mail -s "Some subject line" test@yahoo.com

This works but the same doesn't work from the Java code.
...
Your Java statement doesn't match up with the Linux command.
It should be something like "mail -s" + subject + recipient.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use of java mail to send email from UNIX

Can someone help me to understand how to configure java mail on unix or what is syntex for that to put in script so email can be sent. earlier i was using 'send mail' to send email and not SMPT is stopped in unix machine and they asked us to use java mail. Before I was using this ... (0 Replies)
Discussion started by: mirwasim
0 Replies

2. Shell Programming and Scripting

Script to send email if Java process was done

Hi Team, I need script to send notification if java process was down. Can you please help on this. Thanks in adance. Regards, Nagesh (1 Reply)
Discussion started by: Nageswara Reddy
1 Replies

3. Shell Programming and Scripting

Shell scripting unable to send the sql query data in table in body of email

I have written a shell script that calls below sql file. It is not sending the query data in table in the body of email. spool table_update.html; SELECT * FROM PROCESS_LOG_STATS where process = 'ActivateSubscription'; spool off; exit; Please use code tags next time for your code and data.... (9 Replies)
Discussion started by: Sharanakumar
9 Replies

4. UNIX for Advanced & Expert Users

Unable to send the email in formatted way

Legends, I have following entries in /tmp/a.txt file GAP Ric XEOJ181477500.W Flag2 5 Sym XEO1418J775-E msg SEQ_NO:159 db:5 diff 154 process msg GAP Ric XEOJ181400.W Flag2 19 Sym XE418J815-E msg SEQ_NO:101 db:5 diff 96 process msg GAP Ric XEOJ18140.W Flag2 18 Sym XEO18J855-E msg SEQ_NO:88... (21 Replies)
Discussion started by: sdosanjh
21 Replies

5. Linux

Unable to send email with sendmail from PHP 5.3 on CentOS VM

I have a longstanding issue on my CentOS 5.6 VM where I am unable to send email from my PHP application. This is the last bunch of lines from my /var/log/maillog file. Feb 14 10:29:16 dev53 sendmail: s1EATEEo004637: Authentication-Warning: www.craig.dev-crmpicco.lan: apache set sender to... (2 Replies)
Discussion started by: crmpicco
2 Replies

6. Solaris

Send an email from Solaris using Linux email server

Hello everyone I have a problem and I need your help: I have a Solaris 10 and Solaris 8 UNIX Servers, and Linux Centos4 as email server. I need send an email from Solaris servers preferably using Centos4 email server. I have no mail service configured in my Solaris computers (1 Reply)
Discussion started by: aflores
1 Replies

7. Solaris

Unable to send email to the outside world

Hi all, Iam unable to send mail from my unix machine(solaris x86,version 5.10) to the outside world. I can able to access the internet,but not able to send to any yahoo or company email id. Before posting this,i have searched this forum many times,but could not get the results what i... (5 Replies)
Discussion started by: jayaprakash
5 Replies

8. Shell Programming and Scripting

unable to send an email attachment

i know its pretty repeated query, but i need to post it in this new thread coz i need it urgently uuencode /var/tmp/chandra/emaillist.txt emaillist.txt | mail -s "message with encoded attachment" am unable to send the attachment emaillist.txt present in the path /var/tmp/chandra/ Is... (11 Replies)
Discussion started by: cmaroju
11 Replies

9. UNIX for Advanced & Expert Users

Unable to send eMail from a UNIX-Host ( using mailx ) to a Outlook-email-addres(Win)

Hi A) I am able to send eMail using mailx from a UNIX ( solaris 8 ) host to my Outlook-email-ID : FName.Surname@Citigroup.com ( This is NOT my actual -eMail-ID). But in Outlook the "From :" eMail address is displayed as " usr1@unix-host1.unregistered.email.citicorp.com " .i.e the words... (2 Replies)
Discussion started by: Vetrivela
2 Replies
Login or Register to Ask a Question

Featured Tech Videos