import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.*;
import java.util.*;
public class EmailClient implements Runnable, CommandListener {
private Form f;
private StringItem si;
private SocketConnection sc;
private InputStream is;
private OutputStream os;
private String smtpServerAddress;
private String from1, to1, subject1, msg1;
private StringBuffer sb = null;
private Image image;
private byte[] temp_image1;
private byte[] temp_image2;
private byte[] temp_image3;
private byte[] temp_image4;
private Command back;
public EmailClient(String from, String to, String subject, String msg, byte[] image1_data) {
try {
from1 = from;
to1 = to;
subject1 = subject;
msg1 = msg;
temp_image1 = image1_data;
smtpServerAddress = "mail.smtp.yahoo.co.in"; // SMTP of ur mail
f = new Form("Email Client");
back = new Command("Back", Command.BACK, 3);
si = new StringItem("Please Wait", "until Get Response");
f.append(si);
f.setCommandListener(this);
f.setCurrent(f);
} catch (Exception e) {
showMsg.inform("Exception:" + e);
}
}
public void start() {
Thread t = new Thread(this);
t.start();
}
public void run() {
try {
sc = (SocketConnection) Connector.open("socket://" + smtpServerAddress + ":587");
is = sc.openInputStream();
os = sc.openOutputStream();
//pradeep
os.write(("EHLO" + "\r\n").getBytes());
os.write(("AUTH LOGIN" + "\r\n").getBytes());
//authentication
os.write((Base64Coder.encodeString( " Mail id of ur Mail ") + "\r\n").getBytes()); // message body // mail id here For Authentication
os.write((Base64Coder.encodeString("PassWord of ur id ") + "\r\n").getBytes()); // Password here For Authentication
//end of auth
os.write(("MAIL FROM: " + from1 + "\r\n").getBytes());
os.write(("RCPT TO: " + to1 + "\r\n").getBytes());
// os.write(("RCPT TO: " + "Mail id of recipent" + "\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: " + from1 + "\r\n").getBytes());
os.write(("To: " + to1 + "\r\n").getBytes());
// os.write(("Cc: " + "mail id to ADD CC" + "\r\n").getBytes());
os.write(("Subject: " + subject1 + "\r\n").getBytes());
//attachment
os.write(("Content-Type: multipart/mixed;" + "\r\n").getBytes());
os.write((" boundary= \"----=_Part_83_1842660023.1231184083648\"" + "\r\n").getBytes());
os.write(("\r\n").getBytes());
os.write(("------=_Part_83_1842660023.1231184083648" + "\r\n").getBytes());
os.write(("Content-Type: text/plain; charset=ISO-8859-1" + "\r\n").getBytes());
os.write(("Content-Transfer-Encoding: 7bit" + "\r\n").getBytes());
os.write(("Content-Disposition: inline\r\n").getBytes());
os.write(("\r\n").getBytes());
os.write((msg1 + "\r\n").getBytes()); // message body
os.write(("\r\n").getBytes());
//begin attachment
os.write(("------=_Part_83_1842660023.1231184083648" + "\r\n").getBytes());
//os.write(("").getBytes());
os.write(("Content-Type: image/jpg; name=image.jpg \r\n").getBytes());
os.write(("Content-Transfer-Encoding: base64" + "\r\n").getBytes());
os.write(("X-Attachment-Id: 0.1 \r\n").getBytes());
os.write(("Content-Disposition: attachment; filename= image1.jpg" + "\r\n").getBytes());
os.write(("\r\n").getBytes());
os.write(Base64.encode(temp_image1).toString().getBytes());
// os.write(temp_image);
os.write((" \r\n").getBytes());
os.write(("------=_Part_83_1842660023.1231184083648--" + "\r\n").getBytes());
//end of attachment
os.write(".\r\n".getBytes());
os.write("QUIT\r\n".getBytes());
//pradeep
sb = new StringBuffer();
int c = 0;
while (((c = is.read()) != -1)) {
sb.append((char) c);
}
si.setLabel("Response");
si.setText("SMTP server response - " + sb.toString());
f.addCommand(back);
} catch (IOException e) {
e.printStackTrace();
} 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) {
}
if (c == back) {
temp_image1 = null;
System.gc();
}
}
}
smtpServerAddress = "smtp.mail.yahoo.com";
ReplyDeletesc = (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".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());
Related Links To send Mail from the J2me Mobile.
ReplyDeletehttp://goinhanmailbb.googlecode.com/svn/trunk/Vi%20du%20ve%20goi%20mail%20tren%20j2me%20bang%20socket/EmailClient.java
http://www.buglabs.net/program_versions/846/program_version_sources/5614
http://developers.sun.com/mobility/midp/articles/midp2network/
thanks for the code its working......
ReplyDelete@ ITHELPDESK
ReplyDeleteI used your code by using following link
http://goinhanmailbb.googlecode.com/svn/trunk/Vi%20du%20ve%20goi%20mail%20tren%20j2me%20bang%20socket/EmailClient.java
but i am getting error in very firsr line of class...
private EmailMIDlet parent;
i got error in this line...."cannot find symbol
private EmailMIDlet parent;"
plz help me get out of this error...
Hi Chandan please check this link for source code
Deletehttps://docs.google.com/folder/d/0B8RVSy05WMZrMnhQVTczS0NqMzg/edit?usp=sharing
verify the below link for smtp Incoming and Outgoing Mail Server Settings
http://itminds-usefullinks.blogspot.in/2011/12/incoming-and-outgoing-mail-server.html
This comment has been removed by the author.
ReplyDelete( Base64Coder ) there's the error
ReplyDelete