SMTP J2ME

Hi:

I want to develop an application to send emails from a cellphone.

I am using the las version of the Sun Java Wirelless Toolkit (2.5)

I look at http://developers.sun.com/mobility/midp/articles/midp2network/

to take some directions but this code doesn't work at all. I just want to know what's the best approach to follow to do this.

[368 byte] By [jonhya] at [2007-11-27 10:14:46]
# 1

Hi

I've done this using 2 methods.

1. Talk directly to a SMTP Server

2. Send arguments to a web page that sends the mail

The 2nd method is easier and requires less java code and is also easier to maintain as you do not have to change your app if you want to change the contents of a message, only the web page.

Sample code that lets you send via smtp:

msgstr="";

msgstr=msgstr + "From:name@domain.com\n";

msgstr=msgstr + "To:sendto@domain.com \n";

msgstr=msgstr + "Content-type: text/html;charset=US-ASCII\n";

msgstr=msgstr + "Content-transfer-encoding: 7BIT\n";

msgstr=msgstr + "Subject:Your Subjext\n";

msgstr=msgstr + "CC:ccto@domain.com \n";

msgstr=msgstr + "Content-desciption:Mail message body\n\n\n";

msgstr=msgstr + "Body Of Your Message"

sndml=new SndMl();

sndml.opensoc();

sndml.sendsoc("helo username",1,"]");

//the next 3 lines are only applicable if you need to log on to the mail server

sndml.sendsoc("AUTH LOGIN",1,"WU6");

sndml.sendsoc("c3RldmVqQHNhc25ldC5jby56aYQ==",1,"mQ6"); //depends if logon is encrypted

sndml.sendsoc("I3N0ZXZdlag==",1,"ful");

sndml.sendsoc("Mail From: name@domain.com",1,"OK");

sndml.sendsoc("Rcpt To: name@domain.com ",1,"Ok");

sndml.sendsoc("Rcpt To: name@domain.com ",1,mailto);

sndml.sendsoc("Rcpt To: name@domain.com",1,ccmailto);

sndml.sendsoc("data",1,".<CRLF>");

msgstr=msgstr + "\n\n";

sndml.sendsoc(msgstr,0,"0");

sndml.sendsoc(".",1,"delivery");

sndml.sendsoc("quit",0,"0");

sndml.closesoc();

public SndMl sndml;

class SndMl

{

SndMl()

{

}

void opensoc()

{

try

{

int osch=0;

String osocstr="";

sc = (SocketConnection)Connector.open(mailsvr);

sc.setSocketOption(SocketConnection.KEEPALIVE, 1);

is = sc.openInputStream();

os = sc.openOutputStream();

}

catch(IllegalArgumentException e)

{

OrderForm.append("ILLEGAL ARG");

}

catch(IOException je)

{

OrderForm.append("ILLEGAL IO");

}

}

void closesoc()

{

try

{

is.close();

os.close();

sc.close();

}

catch(IOException cje)

{

//System.out.print("ILLEGAL CLOSE");

}

}

/*

socmsg=data to send to smtp socket

wt 0= dont wait for reply

wt 1 =wait for reply

sretstr=the string that must be received if a reply is needed

*/

void sendsoc(String socmsg, int wt, String sretstr)

{

int socntr=0;

int sch=0;

int dumcntr=0;

String socstr="";

try

{

socmsg=socmsg + "\r\n";

dumcntr=0;

os.write(socmsg.getBytes());

os.flush();

sch = 0;

socstr="";

if (wt!=0)

{

while(socstr.indexOf(sretstr)==-1)

{

sch = is.read();

Character socc=new Character((char)sch);

if(sch>31 && sch<127)

{

socstr=socstr + socc.toString();

//OrderForm.append(socc.toString());

}

}

}

while(dumcntr<17000)

{

dumcntr++;

}

Form.append(".");

}

catch(IllegalArgumentException e)

{

OrderForm.append("ILLEGAL ARG");

}

catch(IOException je)

{

OrderForm.append("ILLEGAL IO");

}

}

}

}

hope it helps

Steve

stevejankoa at 2007-7-28 15:35:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

Thanks Steve:

It seems to me that the second method would be a great idea less code is always good when devices like cellphones are involved.

Well can you send me some information about how it works and if its possible the code. Also I want to know if this works for any kind of email recipient like; yahoo, hotmail, gmail etc etc.

jonhy

jonhya at 2007-7-28 15:35:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3

Hi Jonhy

The reason i did not send code for sending via a web page instead of coding smtp directly from j2me is, it depends on what scripting language your web pages are coded in and what mail server type you are speaking to.

An example can be found at: http://www.scriptmate.com/resources/articles/article.asp?id=11

Java code sample:

HttpConnection gprsconn = null;

.

.

.

gprsconn = (HttpConnection)Connector.open("http://www.host.com/webpage.asp?email=email@host.com&subj=subject&message=your message");

Regards

Steve

stevejankoa at 2007-7-28 15:35:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4

Thanks Steve:

Yeahp, that makes sense.

jonhya at 2007-7-28 15:35:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5

Thanks Steve;

Well I got this code and it runs well. I receive all the acknowledgement message from the server and everything. The problem is that never receive the email that I sent. Dou you have an idea what it could be ?

Here is the code:

import javax.microedition.midlet.*;

import javax.microedition.io.*;

import javax.microedition.lcdui.*;

import java.io.*;

import java.util.*;

public class EmailClient implements Runnable {

private EmailMIDlet parent;

private Display display;

private Form f;

private StringItem si;

private SocketConnection sc;

private InputStream is;

private OutputStream os;

private String smtpServerAddress = "mx1.hotmail.com";

private String from, to, subject, msg;

public EmailClient

(EmailMIDlet m, String from, String to, String subject, String msg) {

parent = m;

this.from = from;

this.to = to;

this.subject = subject;

this.msg = msg;

display = Display.getDisplay(parent);

f = new Form("Email Client");

si = new StringItem("Response:" , " ");

f.append(si);

display.setCurrent(f);

}

public void start() {

Thread t = new Thread(this);

t.start();

}

public void run() {

try {

sc = (SocketConnection)

Connector.open("socket://"+smtpServerAddress+":25");

is = sc.openInputStream();

os = sc.openOutputStream();

os.write(("EHLO" + "\r\n").getBytes());

os.write(("MAIL FROM: "+ from +"\r\n").getBytes());

os.write(("RCPT TO: "+ to + "\r\n").getBytes());

os.write("DATA\r\n".getBytes());

// stamp the msg with date

//os.write(("Date: " + new Date() + "\r\n").getBytes());

//os.write(("From: "+from+"\r\n").getBytes());

//os.write(("To: "+to+"\r\n").getBytes());

//os.write(("Subject: "+subject+"\r\n").getBytes());

os.write((msg+"\r\n").getBytes()); // message body

os.write("\r\n.\r\n".getBytes());

os.write("QUIT\r\n".getBytes());

// debug

StringBuffer sb = new StringBuffer();

int c = 0;

while (((c = is.read()) != -1) ) {

sb.append((char) c);

}

si.setText("SMTP server response - " + sb.toString());

} catch(IOException e) {

Alert a = new Alert ("TimeClient", "Cannot connect to SMTP server. Ping the server to make sure it is running...", null, AlertType.ERROR);

a.setTimeout(Alert.FOREVER);

display.setCurrent(a);

} finally {

try {

if(is != null) {

is.close();

}

if(os != null) {

os.close();

}

if(sc != null) {

sc.close();

}

} catch(IOException e) {

e.printStackTrace();

}

}

}

public void commandAction(Command c, Displayable s) {

if (c == Alert.DISMISS_COMMAND) {

parent.notifyDestroyed();

parent.destroyApp(true);

}

}

}

jonhya at 2007-7-28 15:35:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 6

Jonhy -

No offence, but it makes your posts much more readable if you use the code tags for your codes.

All the best, Darryl

Darryl.Burkea at 2007-7-28 15:35:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 7

No problem with that.

Thanks

jonhya at 2007-7-28 15:35:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 8

Hi Johny

The best way of debugging would be:

telnet mailserver.com 25

and pass all the same commands as you did in your code to it, this will show you where it fallse over.

Steve

stevejankoa at 2007-7-28 15:35:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 9

Thanks again Steve;

That's what I did. But for instance the program runs well and it seems that sends the message to hotmail. But then when I check the email isn't there.

jonhya at 2007-7-28 15:35:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 10

Hi Johny

Have you tried using a different SMTP Server?

Steve

stevejankoa at 2007-7-28 15:35:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 11

Yeahp, I have tried with:

smtp.gmail.comport: 587 & 465

smtp.mail.yahoo.comport 25

mx1.hotmail.comport 25

When I tried with telnet there's a different problem with each of them.

In hotmail the message appears to be send but then when I check my inbox the message isn't there. When I tried telnet with gmail it ask me for a command called 'starttls' but then when I put the command it seems that the smtp server is waiting for something else, I have tried to shutdown my firewall and also my antivirus but nothing. In yahoo it displays an error message with a link but in this link there's nothing useful.

I dont now what else to do. It seems like this is really painful without the javamail api.

jonhya at 2007-7-28 15:35:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 12

Hi Johny

I do not know what else to suggest other than letting a web page send the mail for you.

Regards

Steve

stevejankoa at 2007-7-28 15:35:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...