Saturday, 14 July 2012

Schedule a Task(Upload Excel Data To Database) in a Specified Time Period



Schedule a Task(Upload Excel Data To Database) in a Specified Time Period




Get the source code form this path DOwnload Code

CallSchduler.java

//Main Class

package Scheduler;


import java.io.*;
import java.util.*;

public class CallSchduler {

UpdateSchduler upObj = null;

public void stopScheduler() {
upObj.cancelSchedule();
}

public static void main(String args[]) {
//        System.out.println("Scheduler is called by main method");
try {
File propertyFile = new File("D:\\Demo File\\Resources.properties");
if (propertyFile.exists()) {
Properties properties = new Properties();
FileInputStream fileInputStream = new FileInputStream(propertyFile);
properties.load(fileInputStream);
int minutes = Integer.parseInt(properties.getProperty("Minutes"));
String uploadFilePath = properties.getProperty("myExcelPath").toString();
String failedRecordsPath = properties.getProperty("failedFiles").toString();
UpdateSchduler upObj = new UpdateSchduler(minutes);
upObj.updateFile(uploadFilePath, failedRecordsPath);
} else {
System.out.println("File not found!");
}

} catch (Exception ex) {
System.out.println("in Exception: " + ex.getMessage());
}
}
}





UpdateSchduler.java


package Scheduler;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

public class UpdateSchduler {

private final Timer timer = new Timer();
private final int minutes;
private String uploadFilePath = "";
private String FailedRecordsPath = "";

public UpdateSchduler(int minutes) {
this.minutes = minutes;
}

public void start() {
timer.schedule(new TimerTask() {

public void run() {
fileSelection(uploadFilePath, FailedRecordsPath);
}
}, 0, minutes * 60 * 1000);
}

public void cancelSchedule() {
timer.cancel();
}

public void updateFile(String uploadFilePath, String FailedRecordsPath) {
this.uploadFilePath = uploadFilePath;
this.FailedRecordsPath = FailedRecordsPath;
start();
}

public void stopUpdating() {
cancelSchedule();
}

public void fileSelection(String uploadRecordPath, String failedRecordsPath) {
System.out.println("Upload File Path:" + uploadRecordPath);
System.out.println("Failed Record File Path:" + failedRecordsPath);
String filesInTheUplaodPath = null;
File uplaodFile = new File(uploadRecordPath);
if (uplaodFile.exists()) {
if (uplaodFile.listFiles().length != 0) {
File[] listOfFiles = uplaodFile.listFiles();

System.out.println("No. Files In the UPload Record path:" + listOfFiles.length);
for (int i = 0; i < listOfFiles.length; i++) {

if (listOfFiles[i].isFile()) {
filesInTheUplaodPath = listOfFiles[i].getName();
//System.out.println("the files are " + files);
JavaSheet readObj = new JavaSheet();
readObj.javaSheet(uploadRecordPath + filesInTheUplaodPath, failedRecordsPath + filesInTheUplaodPath);
}
}

} else {
System.out.println("No files Found");
}
} else {
System.out.println("File Path Not Found");
}
}
}




JavaSheet .java



package Scheduler;

import java.sql.Statement;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.List;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
*
* @author Sravan
*/
public class JavaSheet {

StringBuffer sb = new StringBuffer();
int count = 0;
String EmpName = "";
String Dept = "";
String mobileNO = "";
String file_name = "";
String extension = "";
InputStream input = null;
Connection con = null;
FileItemFactory factory = new DiskFileItemFactory();
// System.out.println("1");
ServletFileUpload upload = new ServletFileUpload(factory);
//System.out.println("2");
List items = null;
CallableStatement insertQuery;
DataInputStream in;
private boolean isFileWrite = false;
private FileInputStream fis;
private File inputWorkbook;

public void javaSheet(String file_name1, String failedFIle) {

this.file_name = file_name1;

sb.append("Employee Name" + ",");
sb.append("Department" + ",");
sb.append("MobileNO" + ",");
sb.append("Reason" + ",");
sb.append("\n");

extension = file_name.substring(file_name.indexOf("."), file_name.length());
System.out.println("" + extension);
try {

if (extension.equalsIgnoreCase(".xls") || extension.equalsIgnoreCase(".xlsx")) {
inputWorkbook = new File(file_name);
fis = new FileInputStream(inputWorkbook);
Workbook w;
ResultSet rs = null;
w = Workbook.getWorkbook(fis);
Sheet sheet = w.getSheet(0);
String reason[] = new String[sheet.getRows() + 1];
String rowValues[] = new String[sheet.getColumns()];
System.out.println("no of rows :" + sheet.getRows());
if (sheet.getRows() > 1) {
for (int row = 1; row < sheet.getRows(); row++) {
for (int col = 0; col < sheet.getColumns(); col++) {
Cell cell = sheet.getCell(col, row);

rowValues[col] = cell.getContents();

}


String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String connectionURL = "jdbc:sqlserver://Ip:portNo;databaseName=Dbname;user=sa;password=xyz";



Class.forName(driver);
con = DriverManager.getConnection(connectionURL);
reason[row] = "";

EmpName = rowValues[0];
Dept = rowValues[1];
mobileNO = rowValues[2];


if (EmpName == null || EmpName.trim().equals("")) {
reason[row] = reason[row] + "Emp Name is Null";
}


if (Dept == null || Dept.trim().equals("")) {
reason[row] = reason[row] + "Department Value is Null";
}

if (mobileNO == null || mobileNO.trim().equals("")) {

reason[row] = reason[row] + "Mobile No is Null";

}

if (reason[row] == null) {

String insertQuery = "insert into EMPTable (EMP_Name ,EMP_Dept ,EMP_MobileNO) values ('" + EmpName + "','" + Dept + "','" + mobileNO + "')";
System.out.println(insertQuery);
Statement stmt = con.createStatement();
int res = stmt.executeUpdate(insertQuery);
stmt.close();
if (res == 0) {
reason[row] = "Failed When Inserting";
} else {
count++;
System.out.println("rows inserted:" + count);
}



} else {
System.out.println(" In Fail records");
//
//                            sb.append("Employee Name" + ",");
//                            sb.append("Department" + ",");
//                            sb.append("MobileNO" + ",");
//                            sb.append("Reason" + ",");
//                            sb.append("\n");

//END

isFileWrite = true;
if (EmpName == null || EmpName.equals("")) {
EmpName = "  ";
}
if (Dept == null || Dept.equals("")) {
Dept = "  ";
}
if (mobileNO == null || mobileNO.equals("")) {
mobileNO = "  ";
}



sb.append(EmpName + ",");
sb.append(Dept + ",");
sb.append(mobileNO + ",");
sb.append(reason[row] + ",");
sb.append("\n");
}


System.out.println("ZZZZZZZZZ" + isFileWrite);
System.out.println("" + sb.length());
System.out.println("" + sb);
if (isFileWrite || sb.length() != 0) {
new FailedRecords().getFialedRecords(failedFIle, sb.toString());
}

}
System.out.println("END");
} else {
System.out.println(" Empty XLS File");
}
} else {
System.out.println(" Invalid XLS File");
}

} catch (Exception e) {

System.out.println("Pls Select The Given format of xl file" + e.getMessage());
e.printStackTrace();

} finally {

try {
if (insertQuery != null) {
insertQuery.close();
}
if (con != null) {
con.close();
}
if (fis != null) {
fis.close();
inputWorkbook.delete();
}

} catch (Exception e) {
System.out.println("EXCEPTION IN CLOSING CONNECTION:" + e);
}
}
System.out.println("END");
}
}




FailedRecords.java




package Scheduler;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;

/**
*
* @author Sravan
*/
public class FailedRecords {

public void getFialedRecords(String failedRecordsFile, String FailedRecords) {
System.out.println("Inisde Fialed Records");
String file_name = failedRecordsFile;

File file = new File(file_name);
System.out.println("Filename::::" + file_name);
String buffer = FailedRecords;
String date = "";
String filename = "";
SimpleDateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy-kk.mm.ss");
date = df1.format(new java.util.Date());


try {
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(buffer);
out.close();

} catch (Exception e) {
System.out.println("Ex:" + e);
}
}
}

Wednesday, 14 December 2011

Upload Excel sheet content into Database

Form.jsp
<%--
Document   : Form
Created on : Dec 12, 2011, 10:08:48 AM
Author     : pradeep
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form name="readxls" action="xls.jsp" method="post" enctype="multipart/form-data">
<center>
<h2> READ XLS FILE</h2>
<table border="0">
<tr><td>Company Name</td><td><input type ="text" name ="companyname"/></td></tr>
<tr><td>Company ID</td><td><input type ="text" name ="companyID"/></td></tr>
<tr><td>File Path</td><td><input type ="File" name ="filepath"/></td></tr>
<tr><td align="right"><input type="submit" value="Submit">   </td><td align="left">
<input type="reset" name="Reset" value="Reset"/></td></tr>

</table>

</center>

</form>
</body>
</html>

Xls.jsp
<%--
Document   : Xls
Created on : Dec 12, 2011, 10:49:54 AM
Author     : pradeep
--%>

<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.io.output.DeferredFileOutputStream" %>


<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="jxl.Cell"%>
<%@page import="jxl.Sheet"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="jxl.Workbook"%>
<%@page import="java.io.File"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.io.InputStream"%>

<%@page import="java.util.List"%>

<%@page import="java.util.Iterator"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>

<script type="text/javascript">

function download()
{
// var val=document.getElementById("data").value;
window.location="downloadfile.jsp?filename=errorreport";


}
</script>
</head>
<body>
<right>
<a onclick="download()">Download Error Report</a>

</right>
<center>
<table border ="1">
<tr >

<th>UserName</th>
<th>PassWord</th>
<th colspan="16">Failure Reason</th>
</tr>
<%
StringBuffer sb = new StringBuffer();
sb.append("UserName" + ",");
sb.append("Password" + ",");
sb.append("Company Name" + ",");
sb.append("Company ID" + ",");
sb.append("Failure Reason" + ",");
sb.append("\n");


int count = 0;
String companyname = "";
String companyID = "";
String file_name = "";
String extension = "";
InputStream input = null;
Connection con = null;

FileItemFactory factory = new DiskFileItemFactory();
System.out.println("1");
ServletFileUpload upload = new ServletFileUpload(factory);
System.out.println("2");
List items = null;
try {

items = upload.parseRequest(request);

} catch (Exception e) {
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();

if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
System.out.println(name + " : " + value);
if (name.equals("companyname")) {
companyname = value;
}
if (name.equals("companyID")) {
companyID = value;
}
} else {

file_name = item.getName();
System.out.println("file name" + item.getName());
input = item.getInputStream();

extension = file_name.substring(file_name.indexOf("."));
System.out.println(extension);
}
}

try {


if (extension.equalsIgnoreCase(".xls") || extension.equalsIgnoreCase(".xlsx")) {
String connectionUrl = "";
File inputWorkbook = new File(file_name);
Workbook w;
ResultSet rs;
w = Workbook.getWorkbook(input);
Sheet sheet = w.getSheet(0);
String reason[] = new String[sheet.getRows() + 1];
String rowValues[] = new String[sheet.getColumns()];
System.out.println("no of rows :" + sheet.getRows());
if (sheet.getRows() > 1) {
for (int row = 1; row < sheet.getRows(); row++) {
for (int col = 0; col < sheet.getColumns(); col++) {

Cell cell = sheet.getCell(col, row);
rowValues[col] = cell.getContents();

}


String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String connectionURL = "jdbc:sqlserver://Ip:portNo;databaseName=Dbname;user=sa;password=admin";
System.out.println(driver);
System.out.println(connectionURL);
Class.forName(driver);
con = DriverManager.getConnection(connectionURL);
Statement st = con.createStatement();

System.out.println("After connections ");
String username1 = rowValues[0];
String password1 = rowValues[1];



reason[row] = null;
System.out.println("Row values:" + rowValues[0]);
String queryUser = "select username from sampledata where username='" + rowValues[0] + "'";
System.out.println(queryUser);
rs = st.executeQuery(queryUser);
if (rs.next()) {
reason[row] = "userName : " + rowValues[0] + " is already Exists.";
}
st.close();

int res = 0; // for checking weather row is inserted or not

System.out.println("reason inside" + reason[row]);
//inserting the data into database
if (reason[row] == null) {
String insertQuery = "insert into sampledata (username,password,companyName,comapnyId)" 
+"values ('" + username1 + "','" + password1 + "','" + companyname + "','" + companyID + "')";
System.out.println(insertQuery);
Statement st2 = con.createStatement();
res = st2.executeUpdate(insertQuery);
st2.close();
if (res == 0) {
reason[row] = "Failed When Inserting";
} else {
count++;
System.out.println("one row inserted");
}


} else if (reason[row] != null || res == 0) {
//If any validation failed it should enter into the else
%>
<tr >
<td align="cener"><h4><font color="Red"/><%= username1%></h4></td>
<td align="cener"><h4><font color="Red"/><%= password1%></h4></td>
<td align="cener" colspan="16"><font color="Red"/> <h4><%= reason[row]%></h4></td>
</tr>
<%
sb.append(username1 + ",");
sb.append(password1 + ",");
sb.append(companyname + ",");
sb.append(companyID + ",");
sb.append(reason[row] + ",");
sb.append("\n");
}

}
%>

<h4>Successfully Inserted Records Are <%= count%></h4>
<h4>Below Records are Fail to Insert with reason please download</h4>

<%


} else {
%>
</table>

<table ><tr><td> <h4>You have uploaded <b><%=file_name%></b></td></tr><br><br>
<tr><td><a>No Data Found in the Uploaded file</a></h4></td></tr>

<td align="right" colspan="19">Click here to back to <b><u><a href="Form.jsp">Upload</a></u></b> Screen</td>
<%
}
}

} catch (Exception e) {
%>
<p align="center"> <% out.println("Pls Select The Given format of xl file");
e.printStackTrace();%> </p>
<%
// con.rollback();
} finally {

if (con != null) {
con.close();
}
if (input != null) {
input.close();
}
}
session.setAttribute("data", sb.toString());
%>

</table>


</center>
</body>
</html>

Downloadfile.jsp
<%--
Document   : Downloadfile
Created on : Dec 12, 2011, 2:23:42 PM
Author     : pradeep
--%>

<%@page import="java.io.OutputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.io.FilenameFilter"%>
<%@page import="java.awt.Frame"%>
<%@page import="java.awt.FileDialog"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFCell" %>
<%@page import="org.apache.poi.hssf.usermodel.HSSFCellStyle" %>
<%@page import= "org.apache.poi.hssf.usermodel.HSSFDataFormat" %>
<%@page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%@ page import="java.io.InputStream" %>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>
<body >

<table width="800" align="center"><tr><td>

<center><table width="700"><tr><td align="center"><h2><
font style="font-weight: 900;font-family:'Lucida Casual';font-size: 20px" color="#7E3517" >
Errors Report</font></h2></td></tr></table></center>
<%

String file_name = request.getParameter("filename");
System.out.println("Filename:"+file_name);
String buffer = session.getAttribute("data").toString();
String date = "";
String filename = "";
SimpleDateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy-kk.mm.ss");
date = df1.format(new java.util.Date());

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet worksheet = wb.createSheet("Rejected Data");
System.out.println("buffer:\n" + buffer);
StringTokenizer rowToken = new StringTokenizer(buffer, "\n");
int rowCount = 0;
HSSFRow row[] = new HSSFRow[rowToken.countTokens()];
while (rowToken.hasMoreTokens()) {
String rowData = rowToken.nextToken();
StringTokenizer columnToken = new StringTokenizer(rowData, ",");
row[rowCount] = worksheet.createRow((short) rowCount);
int col = 0;
while (columnToken.hasMoreTokens()) {
HSSFCell cellA1 = row[rowCount].createCell((short) col++);
cellA1.setCellValue(columnToken.nextToken());
}
rowCount++;
}
response.setContentType("application/vnd.ms-excel");
response.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
response.setHeader("Cache-Control", "max-age=0");
response.setHeader("Content-disposition", "attachment; filename=" + file_name + date + ".xls");
OutputStream output = response.getOutputStream();
wb.write(output);
output.close();

%>
<p align="center">File downloaded Successfully at location <%= filename%><br><br><br><br></p>

</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

</form>
</body>
</html>

Thursday, 1 December 2011

Beginners Code Examples

Hi friends find the link to get the sample codes in J2me


find the Following Links

Forms Examples

J2me Examples

JAD and JAR manifest attributes

Find the Link for the JAD And JAR Manifest attributes.

Image Zoomin And ZoomOut IN J2ME

private Image rescaleImage(Image image, int width, int height)
{
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();

Image newImage = Image.createImage(width, height);
Graphics g = newImage.getGraphics();

for(int y=0; y {
for(int x=0; x {
g.setClip(x, y, 1, 1);
int dx = x * sourceWidth / width;
int dy = y * sourceHeight / height;
g.drawImage(image, x-dx, y-dy, Graphics.LEFT | Graphics.TOP);
}
}

return Image.createImage(newImage);
}

Incoming and Outgoing Mail Server Settings for Hotmail, Yahoo! Mail, GMail, MSN, AOL and more

Hi friends here you get the incoming and outgoing Mail Server settings.

Hotmail Settings

Hotmail Incoming Mail Server (POP3) - pop3.live.com (logon using Secure Password Authentification - SPA, mail server port: 995)

Hotmail Outgoing Mail Server (SMTP) - smtp.live.com (TLS enabled, port 587)

Yahoo! Mail Settings

Yahoo Incoming Mail Server (POP3) - pop.mail.yahoo.com (SSL enabled, port 465)

Yahoo Outgoing Mail Server (SMTP) - smtp.mail.yahoo.com (SSL enabled, port 995)

POP Yahoo! Mail Plus email server settings

Yahoo Plus Incoming Mail Server (POP3) - plus.pop.mail.yahoo.com (SSL enabled, port 995)

Yahoo Plus Outgoing Mail Server (SMTP) - plus.smtp.mail.yahoo.com (SSL enabled, port 465, use authentication)

Google GMail Settings

Google Gmail Incoming Mail Server (POP3) - pop.gmail.com (SSL enabled, port 995)

Outgoing Mail Server - use the SMTP mail server address provided by your local ISP or smtp.gmail.com (TLS enabled, port 587)

MSN Mail Settings


MSN Incoming Mail Server (POP3) - pop3.email.msn.com (port 110, using Secure Password Authentication - SPA)

MSN Outgoing Mail Server - smtp.email.msn.com (select "My outgoing server requires authentication")

Lycos Mail Settings


Lycos Mail Incoming Mail Server (POP3) - pop.mail.lycos.com (port 110)

Outgoing Mail Server - smtp.mail.lycos.com or use your local ISP SMTP mail server

AOL Mail Settings

AOL Incoming Mail Server (IMAP) - imap.aol.com (port 143)

AOL Outgoing Mail Server - smtp.aol.com or use your local ISP SMTP mail server

Mail.com Mail Settings

Mail.com Mail Incoming Mail Server (POP3) - pop1.mail.com (port 110)

Outgoing Mail Server - use your local ISP SMTP mail server

Netscape Internet Service Mail Settings

Netscape Internet Service Incoming Mail Server (POP3) - pop.3.isp.netscape.com (port 110)

Netscape Internet Service Outgoing Mail Server - smtp.isp.netscape.com (port 25, using a secure SSL connection)

Tiscali Mail Settings

Tiscali Incoming Mail Server (POP3) - pop.tiscali.com (port 110)

Outgoing Mail Server - use your local ISP SMTP mail server

Freeserve Mail Settings

Freeserve Incoming Mail Server (POP3) - pop.freeserve.com (port 110)

Outgoing Mail Server - use your local ISP SMTP mail server

Supanet Mail Settings

Supanet Incoming Mail Server (POP3) - pop.supanet.com (port 110)

Outgoing Mail Server - use your local ISP SMTP mail server

Sending mail From J2me App wihtout Web Service

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();

}
}
}