Re: Can't send email from J2ME.
Martin Gregorie wrote:
Boki wrote:
sc = (SocketConnection)
Connector.open("socket://"+"smpt.abcd.com"+":25");
is = sc.openInputStream();
os = sc.openOutputStream();
You've opened a connection to an SMTP server (I presume it's an SMTP server
since you are connecting on port 25) but have not read the server greeting.
You've jumped straight into sending commands. Read the response, it may tell
you something useful, like your connection has been rejected.
os.write( ("USER " + "Boki" + "\r\n").getBytes());
os.write( ("PASS " + "PASS1234" + "\r\n").getBytes());
What is this?
An SMTP conversation does not begin with USER/PASS. If you need to authenticate
you do so after the initial EHLO (not HELO, that doesn't support
authentication), and *only* if the server offers authentication as an option.
Even then it must be initiated by the AUTH command. I've never seen USER/PASS
as part of any SMTP authentication process, normally the client responds to
prompts from the server.
Further, since extended options are only offered in response to EHLO, and the
conversation is initiated with HELO, I doubt the server would offer
authentication. If it did it's broken, the 250- extended options MUST NOT
(capitalization from the RFC) be sent in response to HELO.
os.write(("HELO there" + "\r\n").getBytes());
You need to read the response to HELO.
os.write(("MAIL FROM: "+ "Boki" +"\r\n").getBytes());
You need to read the response to MAIL FROM:
os.write(("RCPT TO: "+ "bokiteam@ms21.hinet.net" +
"\r\n").getBytes());
You need to read the response from RCPT TO:
os.write("DATA\r\n".getBytes());
You need to read the response from DATA.
Ignoring these responses is considered rude, and many SMTP servers will refuse
to accept a message if you don't bother to read what they had to say in
response to your input. Not reading responses is a very good indicator of a
badly written client, very often a spam engine, and a good reason to terminate
immediately, or to tar-pit the client to slow the stream of spam.
// stamp the msg with date
os.write(("Date: " + date + "\r\n").getBytes());
os.write(("From: "+"From Boki"+"\r\n").getBytes());
os.write(("To:
"+"bokiteam@ms21.hinet.net"+"\r\n").getBytes());
os.write(("Subject: "+"Subject"+"\r\n").getBytes());
os.write((msg+"\r\n").getBytes()); // message body
os.write(".\r\n".getBytes());
os.write("QUIT\r\n".getBytes());
// I can't see this email on server.... it seems never send correctly.
You are right, it is not sent correctly ;-)
Could you please advice ?
Thank you very much!
Best regards,
Boki.
Several reasons:
(1) No initial "From sender@host" line
That's not part of the original mail message. The line you indicate is added by
a mail delivery agent, and is the envelope sender claimed in the MAIL FROM:
SMTP command. It's not part of the message body sent by the client.
(2) No blank line between the headers and the message body
Whilst an SMTP server can look at the contents of the message it isn't required
to do so. It might do so as an anti-spam, anti-virus measure, but not normally
to enforce RFC822 message semantics. It would be very strange for an SMTP
server to reject a message because the message body didn't conform.
(3) No "." line at the end of the message
What about:
os.write(".\r\n".getBytes());
Those should do for starters.
Why don't you use JavaMail? Its easy enough to use and free from Sun
That is a valid point. The OP clearly doesn't understand SMTP.
--
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