Java Forum / General / November 2007
How to use sendmail in Java?
void.no.spam.com@gmail.com - 02 Nov 2007 17:18 GMT I have the following Perl code that works for sending email from a Linux machine:
open(SENDMAIL, "|/usr/sbin/sendmail -t") or die "Unable to open sendmail"; print(SENDMAIL "To: $recipients\n"); print(SENDMAIL "Subject: Test Results\n"); print(SENDMAIL "\n"); print(SENDMAIL "here are the results\n"); close(SENDMAIL);
I want to do the same thing in Java, but when I try to open that file, I get "java.io.FileNotFoundException: !/usr/sbin/sendmail -t (No such file or directory)"
So then I tried to use the JavaMail API, and I got the following exception:
send failed, exception: javax.mail.SendFailedException: Sending failed; nested exception is: class javax.mail.MessagingException: Could not connect to SMTP host: 10.11.8.45, port: 25; nested exception is: java.net.ConnectException: Connection refused
I did a ps -fea and I see that sendmail is running and accepting connections. netstat -an shows that port 25 is listening. Iptables is not running so it isn't a firewall issue. I'm trying to run the Java program from the same machine where sendmail is running. Any idea why JavaMail can't connect?
This is the code I'm using:
java.util.Properties props = new java.util.Properties(); props.put("mail.smtp.host", "10.11.8.45"); props.put("mail.from", "sender@domain"); Session session = Session.getInstance(props, null);
try { MimeMessage msg = new MimeMessage(session); msg.setFrom(); msg.setRecipients(Message.RecipientType.TO, "test@ourdomain.com"); msg.setSubject("JavaMail hello world example"); msg.setSentDate(new java.util.Date()); msg.setText("Hello, world!\n"); Transport.send(msg); } catch (MessagingException mex) { System.out.println("send failed, exception: " + mex); }
Nigel Wade - 02 Nov 2007 17:30 GMT > I did a ps -fea and I see that sendmail is running and accepting > connections. netstat -an shows that port 25 is listening. Iptables [quoted text clipped - 6 lines] > java.util.Properties props = new java.util.Properties(); > props.put("mail.smtp.host", "10.11.8.45"); What does netstat show as the listening address? By default, unless you have a fully configured MTA, sendmail will only listen on the loopback address. The prevents it from acting as an open relay.
Try specifying 127.0.0.1 as the mail.smtp.host.
 Signature Nigel Wade, System Administrator, Space Plasma Physics Group, University of Leicester, Leicester, LE1 7RH, UK E-mail : nmw@ion.le.ac.uk Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
void.no.spam.com@gmail.com - 02 Nov 2007 18:14 GMT > What does netstat show as the listening address? By default, unless you have a > fully configured MTA, sendmail will only listen on the loopback address. The > prevents it from acting as an open relay. > > Try specifying 127.0.0.1 as the mail.smtp.host. You were right. Thanks!
Roedy Green - 02 Nov 2007 19:22 GMT On Fri, 02 Nov 2007 09:18:34 -0700, "void.no.spam.com@gmail.com" <void.no.spam.com@gmail.com> wrote, quoted or indirectly quoted someone who said :
> nested exception is: > class javax.mail.MessagingException: Could not connect to SMTP >host: 10.11.8.45, port: 25; > nested exception is: > java.net.ConnectException: Connection refused Sounds like you did not include your password. See http://mindprod.com/products.html#BULK look at my code for logging on in Bulk.java.
// note that send needs a password. Properties props = System.getProperties(); if ( NEED_PASSWORD_TO_SEND ) { props.setProperty( "mail.smtp.auth", "true" ); } ... store = session.getStore( RECEIVE_PROTOCOL ); store.connect( RECEIVE_HOST, RECEIVE_PORT, RECEIVE_LOGIN_ID, RECEIVE_PASSWORD );
... Transport transport = session.getTransport( SEND_PROTOCOL ); transport.connect( SEND_HOST, SEND_PORT, SEND_LOGIN_ID, SEND_PASSWORD );
Also try turning on debugging so you can watch it more detail what is happening.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Lothar Kimmeringer - 02 Nov 2007 22:03 GMT > On Fri, 02 Nov 2007 09:18:34 -0700, "void.no.spam.com@gmail.com" > <void.no.spam.com@gmail.com> wrote, quoted or indirectly quoted [quoted text clipped - 7 lines] > > Sounds like you did not include your password. How do you provide a password when opening a socket?
Regards, Lothar
 Signature Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
Always remember: The answer is forty-two, there can only be wrong questions!
Roedy Green - 02 Nov 2007 22:15 GMT On Fri, 2 Nov 2007 22:03:58 +0100, Lothar Kimmeringer <news200709@kimmeringer.de> wrote, quoted or indirectly quoted someone who said :
>How do you provide a password when opening a socket? I already posted the code. I pointed you to a complete program.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Arne Vajhøj - 03 Nov 2007 00:23 GMT > On Fri, 2 Nov 2007 22:03:58 +0100, Lothar Kimmeringer > <news200709@kimmeringer.de> wrote, quoted or indirectly quoted someone > who said : >> How do you provide a password when opening a socket? > > I already posted the code. I pointed you to a complete program. I think the important part above is "when opening a socket" based on the exception which was: java.net.ConnectException: Connection refused
Arne
Roedy Green - 03 Nov 2007 04:10 GMT >I think the important part above is "when opening a socket" >based on the exception which was: > java.net.ConnectException: Connection refused You don't open a socket. You play with JavaMail objects, such as Store, Transport and Session. JavaMail manages the socket.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Arne Vajhøj - 03 Nov 2007 04:32 GMT >> I think the important part above is "when opening a socket" >> based on the exception which was: >> java.net.ConnectException: Connection refused > > You don't open a socket. You play with JavaMail objects, such as > Store, Transport and Session. JavaMail manages the socket. True.
But the JavaMail classes eventually use a socket.
The point is that the exception indicates that it happen at connect time.
Before it is possible to even send a password.
Arne
Roedy Green - 03 Nov 2007 17:13 GMT >But the JavaMail classes eventually use a socket. > >The point is that the exception indicates that it >happen at connect time. I suggested turning on debug. You can then see if the password has even been sent. If you can't even make the raw socket connect, you may have the url or port wrong. Check that combo out with a commercial mail program.
I discovered my ISP, Telus, is BLOCKING my email to/from any but THEIR mail servers. The solution is to use a non-standard port so they don't see it as email. This is well-intentioned -- to discourage spammers, but it also stops you from having SMTP/POP3 accounts out on the web.
You may be having similar trouble.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Lothar Kimmeringer - 03 Nov 2007 17:08 GMT > On Fri, 2 Nov 2007 22:03:58 +0100, Lothar Kimmeringer > <news200709@kimmeringer.de> wrote, quoted or indirectly quoted someone [quoted text clipped - 3 lines] > > I already posted the code. Yes and if you point the program to an unused port, what error-message do you get? You will get exactly the exception class javax.mail.MessagingException: Could not connect to SMTP host: [somewhere], port: [someport]; nested exception is: java.net.ConnectException: Connection refused
Of course this needs a correctly adminstered network where requests to ports are not filtered by intermediate "firewalls". Otherwise you will get a "request timed out".
> I pointed you to a complete program. So? The program will most likely be the same as void.no.spam.com was using for his test resulting to the exception. Problem is that you don't really read the postings here but just see the keywords and post links to your pages all the time without caring if it fits the current problem or not.
Mostly it works, but quite often not. This is one example (of many).
Regards, Lothar
 Signature Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
Always remember: The answer is forty-two, there can only be wrong questions!
Lothar Kimmeringer - 02 Nov 2007 22:12 GMT > open(SENDMAIL, "|/usr/sbin/sendmail -t") or die "Unable to open > sendmail"; [quoted text clipped - 7 lines] > I get "java.io.FileNotFoundException: !/usr/sbin/sendmail -t (No such > file or directory)" In perl you're not opening a file in the first line. The pipe at the beginning of the "filename" signifies that you're executing a process, /usr/sbin/sendmail with the parameter "-t".
How to solve the whole thing in a purely java-based way you already found out (with the help of Nigel, so I show you how to do the above thing just to complete the topic ;-)
Process p = Runtime.getRuntime().exec(new String[]{ "/usr/sbin/sendmail", "-t" }); OutputStreamWriter os = new OutputStreamWriter(p.getOutputStream(), "8859_1"); os.write("To: "); os.write(recipients); os.write("\n"); os.write("Subject: Test Results\n"); os.write("\n"); os.write("here are the results\n"); os.close(); p.waitFor();
If sendmail is doing output on STDOUT or STDERR you have to read that (e.g. in a Thread to be able to wait for the end of the process). But because the JavaMail API is to be prefered this is purely for information and not intended to be used in production.
Regards, Lothar
 Signature Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
Always remember: The answer is forty-two, there can only be wrong questions!
microsoft_geek - 03 Nov 2007 10:47 GMT I am giving here the full source code by using this code i have successfully sent mail to any address. You have to set the classpath of the JavaMail API first... then try this code... with proper values in the fields
//here is the code //Author - A. B. Mondal(die4java@gmail.com)
import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*;
// Send a simple, single part, text/plain e-mail
public class TestEmail {
public static void main(String[] args) {
// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!! String to = "TO@domain.com"; String from = "from@domain.com";
// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!! String host = "mail.infobase.in";
// Create properties, get Session Properties props = new Properties();
// If using static Transport.send(), // need to specify which host to send it to props.put("mail.smtp.host", host);
// To see what is going on behind the scene props.put("mail.debug", "true"); Session session = Session.getInstance(props);
try { // Instantiate a message Message msg = new MimeMessage(session);
//Set message attributes msg.setFrom(new InternetAddress(from)); InternetAddress[] address = {new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject("UR SUBJECT"); msg.setSentDate(new Date());
// Set message content msg.setText("This is a test of sending a " + "plain text e-mail through Java.\n" + "BY YOU");
//Send the message Transport.send(msg); } catch (MessagingException mex) { // Prints all nested (chained) exceptions as well mex.printStackTrace(); } } }//End of class
Regards Aurobindo Java Developer
> I have the following Perl code that works for sending email from a > Linux machine: [quoted text clipped - 48 lines] > System.out.println("send failed, exception: " + mex); > } Roedy Green - 03 Nov 2007 17:14 GMT On Sat, 03 Nov 2007 02:47:51 -0700, microsoft_geek <aurobindo.mondal@gmail.com> wrote, quoted or indirectly quoted someone who said :
>I am giving here the full source code by using this code i have >successfully sent mail to any address. Nowadays you often need a password to send mail, unless that mail is to the domain of the mailserver.
There are several password schemes.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|