This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes sending mail using servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "sending mail using servlet" Watch "sending mail using servlet" New topic
Author

sending mail using servlet

Muhammed Salah
Greenhorn

Joined: Dec 25, 2008
Posts: 8
Hi people,

i hope you can help me because i am new in network programming.

I am trying to send email from j2me to googlemail account I have 2 classes EmailMidlet (which has been tested with wireless Toolkit 2.5.2 and it works) and the second class is the servlet-class named EmailServlet:

when i call the EmailServlet, i get on the console:

Server: 220 mx.google.com ESMTP g28sm19313024fkg.21
Server: 250 mx.google.com at your service


this is the code of my EmailServlet


1.when i print "to" in EmailServlet also:


it show null on the console
2. ist this right in case of googlemail.com?



and this is my EmailMidlet:



I would be very grateful if somebody can help me.
ujjwal soni
Ranch Hand

Joined: Mar 28, 2007
Posts: 403
Hi,

Check with your firewall settings, you can add an exception to port 25 there.

To do this, go to run -> type firewall.cpl -> press enter

Cheers!!!

Ujjwal B Soni


Cheers!!!
Ujjwal B Soni <baroda, gujarat, india> <+919909981973>
"Helping hands are better than praying lips......"
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Just another cross poster.
http://discussion.forum.nokia.com/forum/showthread.php?p=521548
http://forums.sun.com/thread.jspa?threadID=5357472


luck, db
There are no new questions, but there may be new answers.
sumanta panda
Ranch Hand

Joined: Jun 23, 2008
Posts: 224
[Edit: thread hijack removed. Ask your own questions in your own topics, and be sure to post them in an appropriate forum.]
[ December 26, 2008: Message edited by: Bear Bibeault ]
Muhammed Salah
Greenhorn

Joined: Dec 25, 2008
Posts: 8
thank you all for replay.


@ujjwal soni

i did the following:
exception:
Name:SMTP (name correct?)
PortNr: 25

nothing change

i have notice the follwing warning for the method readline() in EmailServlet:
the method readline from the typ DataInputStream is deprecated.

could it be a problem?
Muhammed Salah
Greenhorn

Joined: Dec 25, 2008
Posts: 8
I have one quistion about transfering data between midlet-class servlet-class through DataInputStream and DataOutPutStream.

i write the following in the midlet-class:
DataOutputStream os = c.openDataOutputStream();
os.write(("to="+to).getBytes()); (where String to="myemail@googlemail.com"; )
os.write(("&subject="+subject).getBytes());
.....
....

and tried to call this in the servlet-class through the following:
request.getParameter("to");

it returns null
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by Muhammed Salah:
request.getParameter("to");

it returns null
That just means there wasn't any "to" parameter in the request. Nothing to do with e-mail or DataInputStream or any of those things, the user just didn't type anything into the "to" field.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60740
    
  65

Originally posted by Paul Clapham:
the user just didn't type anything into the "to" field.

That would result in an empty string, not a null. A null indicates that the text field was not part of the submitted form to begin with.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by Bear Bibeault:
A null indicates that the text field was not part of the submitted form to begin with.
And it appears that the posted code with DataOutputStreams is trying to do something like submitting a form. It looks like it's generating a "to" parameter, but the comments indicate the value of that parameter is something which should be URL-encoded, but isn't. And who knows? Maybe that's the wrong place to put the parameter. It's hard to tell from that fragment of code.
Muhammed Salah
Greenhorn

Joined: Dec 25, 2008
Posts: 8
thank you for your reply.

i write myemail@google.com to the "to"-feld and then press send.

String to in the midlet will be overwritten through to = toField.getString();

my Problem is the the exchange of data between servlet and midlet

and the notation:
DataOutputStream os = c.openDataOutputStream();
os.write(("to="+to).getBytes());

How can i call "to" from the servlet or how can i be sure, that servlet can call "to"?
Srikanth Nutigattu
Ranch Hand

Joined: Oct 30, 2007
Posts: 114
@Muhammed Salah

Not only the "To" field but even subject and message fields are not being passed.

Solution:

In your code, remove this line

os.flush();

from the midlet. I could retrieve the fields from the servlet after that...

HTH.
[ December 28, 2008: Message edited by: Srikanth Nutigattu ]
Muhammed Salah
Greenhorn

Joined: Dec 25, 2008
Posts: 8
Originally posted by Srikanth Nutigattu:

Solution:

In your code, remove this line

os.flush();

from the midlet. I could retrieve the fields from the servlet after that...

HTH.

[ December 28, 2008: Message edited by: Srikanth Nutigattu ][/QB]


Hi Srikanth Nutigattu

i did.

but the following print() in the servlet still return null:

out.print(request.getParameter("to"));
System.out.println(request.getParameter("to"));

the following print in the midlet returns "myEmail@googlemail.com"
System.out.println(to);
c = (HttpConnection)Connector.open(url);

i read in other forum about url-encude could be the problem.
any idea about this in case of connection.open(url)?
Muhammed Salah
Greenhorn

Joined: Dec 25, 2008
Posts: 8
oh i forgot = in ("to="+to).getBytes())


i solved my Problem

thank you so much you have saved me alive


now I can continue to next problem (sending email)
Muhammed Salah
Greenhorn

Joined: Dec 25, 2008
Posts: 8
now i am trying to find the IOException in the send(....)-method.

i wrote the send()method in java class and started eclipse debug.

the pointer jump from the line:
while ((responseLine = is.readLine()) != null) {....

to:

catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
e.printStackTrace();
}

and returns the following error:

IOException: Software caused connection abort: recv failed

java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.DataInputStream.readLine(Unknown Source)
at EmailSender.send(EmailSender.java:59)
at EmailSender.main(EmailSender.java:79)

i was researching about this error in google some people say firewall could be the problem.
i shut down my firewall and antivirus but nothing change.

any idea, what can i do?

thank you
Muhammed Salah
Greenhorn

Joined: Dec 25, 2008
Posts: 8
I have solved my Problem by using java mail.


I would like to thank everyone here, who tried to help me.........

Thank you so much,
thedaystar
firstName SecondName
Greenhorn

Joined: Mar 20, 2009
Posts: 15
Muhammed Salah wrote:I have solved my Problem by using java mail.


I would like to thank everyone here, who tried to help me.........

Thank you so much,
thedaystar

hi brother,

i saw your post regarding email sending from j2me.
can you please email me or paste here your running code of email sending .
Your help will be highly appreciable!
ahsan
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
He said that he solved it using JavaMail.
Here is its homepage: http://java.sun.com/products/javamail
Especially checkout the FAQ, it contains lot of information and code examples.
firstName SecondName
Greenhorn

Joined: Mar 20, 2009
Posts: 15
Bauke Scholtz wrote:He said that he solved it using JavaMail.
Here is its homepage: http://java.sun.com/products/javamail
Especially checkout the FAQ, it contains lot of information and code examples.

hi,
how can i use this javamail .
do you have any suggestions.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
As said, especially checkout the FAQ, it contains lot of information and code examples. You can find a link to the FAQ in the left navigatio menu.

It is not that hard. Just create a Java class which does the mailing task independently and test it as a Java application using main(). Once you got that work, just import/use that class in your J2ME application.
firstName SecondName
Greenhorn

Joined: Mar 20, 2009
Posts: 15
Bauke Scholtz wrote:As said, especially checkout the FAQ, it contains lot of information and code examples.

It is not that hard. Just create a Java class which does the mailing task independently and test it as a Java application using main(). Once you got that work, just import/use that class in your J2ME application.

Thanks for that!
I will do that!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sending mail using servlet
 
Similar Threads
Access Dbase with Http connection
MY server just hanged
HOW TO COMMUNICATE JAVA APPLICATION TO SERVLETS
JAVA SOCKET PROGRAMMING
simple socket programming