Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Can't send email from J2ME.

Thread view: 
Boki - 27 Nov 2006 17:41 GMT
sc = (SocketConnection)
           Connector.open("socket://"+"smpt.abcd.com"+":25");

        is = sc.openInputStream();
        os = sc.openOutputStream();

     os.write( ("USER " + "Boki" + "\r\n").getBytes());
     os.write( ("PASS " + "PASS1234" + "\r\n").getBytes());

        os.write(("HELO there" + "\r\n").getBytes());
        os.write(("MAIL FROM: "+ "Boki" +"\r\n").getBytes());
        os.write(("RCPT TO: "+ "bokiteam@ms21.hinet.net" +
"\r\n").getBytes());
        os.write("DATA\r\n".getBytes());
        // 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.

Could you please advice ?

Thank you very much!
Best regards,
Boki.
Martin Gregorie - 28 Nov 2006 01:17 GMT
>          sc = (SocketConnection)
>             Connector.open("socket://"+"smpt.abcd.com"+":25");
[quoted text clipped - 27 lines]
> Best regards,
> Boki.

Several reasons:

(1) No initial "From     sender@host" line
(2) No blank line between the headers and the message body
(3) No "." line at the end of the message

Those should do for starters.

Why don't you use JavaMail? Its easy enough to use and free from Sun

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Nigel Wade - 28 Nov 2006 11:37 GMT
>>          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());
[quoted text clipped - 7 lines]
>>
>> // 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 ?
>>
[quoted text clipped - 5 lines]
>
> (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.

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

Boki - 28 Nov 2006 16:49 GMT
> >>          sc = (SocketConnection)
> >>             Connector.open("socket://"+"smpt.abcd.com"+":25");
[quoted text clipped - 7 lines]
> >>       os.write( ("USER " + "Boki" + "\r\n").getBytes());
> >>       os.write( ("PASS " + "PASS1234" + "\r\n").getBytes());What is this?

1) I am using J2ME, can't use javamail.
2) I think I was wrong here, could you please provide the sequence that
I should follow to login email server.

or is there any free email server that I can just do a test ?

Thx a lot!

Best regards,
Boki.
Nigel Wade - 28 Nov 2006 17:09 GMT
>> >>          sc = (SocketConnection)
>> >>             Connector.open("socket://"+"smpt.abcd.com"+":25");
[quoted text clipped - 11 lines]
> 2) I think I was wrong here, could you please provide the sequence that
> I should follow to login email server.

You need to read the RFCs, and follow the protocol.

The RFC for SMTP is 2821, and the RFC for SMTP authentication is 554.

> or is there any free email server that I can just do a test ?

I have no idea. I would have thought your best bet would be your ISPs mail
server, although your ISP might begin to get suspicious at the amount of SMTP
protocol errors from your account.

If you really want to make it work why don't you setup your own SMTP server.
But, for all our sakes, don't connect it to the Internet.

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

Martin Gregorie - 28 Nov 2006 17:53 GMT
>>>>          sc = (SocketConnection)
>>>>             Connector.open("socket://"+"smpt.abcd.com"+":25");
[quoted text clipped - 10 lines]
> 2) I think I was wrong here, could you please provide the sequence that
> I should follow to login email server.

If you haven't already done so, grab a copy of RFC 2821 from
http://www.rfc-editor.org/rfcsearch.html and use it for reference.

> or is there any free email server that I can just do a test ?

Its difficult to recommend anything when you don't say what OS you're
using or what's already on your local LAN, but:

- if you're running Linux, FreeBSD or a UNIX or there's one on
  your LAN then there will be an SMTP MTA (server) running on it
  that you can use as a target. If its another host on your LAN,
  you may have to talk to its sysadmin to gain access.

- go look at the JavaMail site. There are a few SMTP MTAs,
  e.g. XM Server, listed under Third Party downloads. I haven't used
  any of them, so have no recommendation or opinion about them, but
  being Java applications, they should run on your development system
  whatever it may be.

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Boki - 28 Nov 2006 13:21 GMT
Martin Gregorie 寫道:

> >          sc = (SocketConnection)
> >             Connector.open("socket://"+"smpt.abcd.com"+":25");
[quoted text clipped - 42 lines]
> gregorie. | Essex, UK
> org       |

JavaMail ? support J2ME ? I am newbie ...

Could you please advice the sample code is ....

Best regards,
Boki.
Martin Gregorie - 28 Nov 2006 17:55 GMT
> Martin Gregorie 寫道:
>
[quoted text clipped - 48 lines]
>
> Could you please advice the sample code is ....

http://java.sun.com/products/javamail/index.jsp

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |



Free Magazines

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

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.