Wednesday, August 29, 2007

Mengirim dan Membaca Email dengan java mail api

Email atau elektronic mail merupakan aplikasi yang paling banyak digunakan pada internet hingga sekarang. Protocol yang digunakan pada email adalah SMTP untuk pengiriman dan POP3 untuk penerimaan.

Email dalam pengiriman maupun penerimaan menggunakan protocol TCP/IP. Sedangkan port yang digunakan adalah port 25 untuk pengiriman dan port 110 untuk penerimaan.

JavaMail API adalah standar komponen sejak java 1.1 tetapi membutuhkan komponen dari java, yaitu mail.jar, imap.jar, mailapi.jar, pop3.jar, dan smtp.jar yang dapat didownload pada http://java.sun.com/products/javamail/ .

JavaMail ini dapat diimplementasikan secara pemrograman murni java menggunakan socket dan stream.

Program Kirim Email

import javax.mail.*;

import javax.mail.internet.*;

import java.util.*;

public class SendEmail {

public static void main(String[] args) {

try {

String NamaHost = "202.155.73.xx";

String Kepada = "xxx@ism.com";

String Dari = "uchiha_didik@yahoo.com";

Properties props = System.getProperties();

props.put("mail.smtp.host", NamaHost);

Session session = Session.getInstance(props, null);

Message msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(Dari));

InternetAddress[] address = {new InternetAddress(Kepada)};

msg.setRecipients(Message.RecipientType.TO, address);

msg.setSubject("Testing");

msg.setContent("In program ngetest", "text/plain");

Transport.send(msg);

} catch (Exception e) {

e.printStackTrace();

}

}

}

Program Baca Email

import javax.mail.*;

import javax.mail.internet.*;

import java.util.*;

public class POP3Client {

public static void main(String[] args) {

Properties props = System.getProperties();

String host = "202.155.73.xx";

String username = "username";

String password = "xxxxxx"; //mau tau aja passnya

String provider = "pop3";

try {

//Koneksi POP3 mail server

Session session = Session.getDefaultInstance(props, null);

Store store = session.getStore(provider);

store.connect(host,username,password);

//buka folder

Folder inbox = store.getFolder("INBOX");

if (inbox == null) {

System.out.println("No Inbox");

System.exit(1);

}

inbox.open(Folder.READ_ONLY);

//Tampilkan Pesan Surat

Message[] messages = inbox.getMessages();

for (int i = 0; i <>

System.out.println("---- Message " + (i + 1) + " ----");

messages[i].writeTo(System.out);

}

//jangan lupa tutup koneksi

inbox.close(false);

store.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

Apabila program POP3Client dijalankan maka akan tampil

C:\j2sdk1.4.2_05\bin\java.exe -classpath "C:\j2sdk1.4.2_05\jre\lib\rt.jar;C:\j2sdk1.4.2_05\lib\tools.jar;C:\j2sdk1.4.2_05\javamail-1.3.2\mail.jar;C:\j2sdk1.4.2_05\javamail-1.3.2\lib\imap.jar;C:\j2sdk1.4.2_05\javamail-1.3.2\lib\mailapi.jar;C:\j2sdk1.4.2_05\javamail-1.3.2\lib\pop3.jar;C:\j2sdk1.4.2_05\javamail-1.3.2\lib\smtp.jar;C:\JBuilderX\lib\activation.jar;C:\data\belajar\bp4\test" POP3Client

---- Message 1 ----

Return-Path:

Delivered-To: xxx@ism.com

Received: (qmail 11857 invoked by uid 505); 21 Feb 2005 02:39:03 -0000

Received: from uchiha_didik@yahoo.com by mail.xxx.co.id by uid 502 with qmail-scanner-1.16

(clamscan: 0.54. Clear:.

Processed in 0.458176 secs); 21 Feb 2005 02:39:03 -0000

Received: from unknown (HELO notebookwdg) (192.168.12.57)

by mail.antara.co.id with SMTP; 21 Feb 2005 02:39:02 -0000

Message-ID: <27994366.1108961448718.javamail.administrator@notebookwdg>

From: uchiha_didik@yahoo.com

To: xxx@ism.com

Subject: TEsting

MIME-Version: 1.0

Content-Type: text/plain; charset=us-ascii

Content-Transfer-Encoding: 7bit

this is test only.,alias test doanks.....


Wednesday, August 22, 2007

Base64 With Java

The following source code is implementation of Base64 Algorithm using JAVA :


import java.util.Scanner;
import java.lang.*;
import java.io.*;
import java.nio.ByteBuffer;

public class Base64
{
//public static String B64Tablea = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
private static byte[] Encode(byte source[])
{
//String hrmm = "";
//for(int i = 0; i < source.length; i++)
// hrmm += B64Tablea.charAt((source[i] >> 2) & 0x3f);
//System.out.println("Test: " + hrmm);

byte binaryStream[] = BinaryStream(source, true); // Convert to binary stream
int binLen = binaryStream.length;
int padding = binLen % 3; // Calculate padding

byte encodeBlock[] = new byte[6]; // Encode blocks of 6 chars
ByteBuffer encodeData = ByteBuffer.allocate(binLen / 6 + padding + 1);
System.out.println("Converting data blocks to Base64 chars...");
for(int i = 0; i < binLen; i += 6)
{
for(int j = 0; j < 6; j++) // Fill block with binary data
{
if(i + j == binLen) break;
encodeBlock[j] = binaryStream[j + i];
}
encodeData.put((byte) Base64Table(encodeBlock)); // Add encoded data

for(int j = 0; j < 6; j++)
encodeBlock[j] = '0';
}
for(int i = 0; i < padding; i++) // Pad data
encodeData.put((byte)'=');
System.out.println("Encoding complete.");
return encodeData.array();
}

private static char Base64Table(byte encodeBlock[])
{
int bits[] = {32, 16, 8, 4, 2, 1}; // Bit values
String B64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int totalBits = 0;

for(int i = 0; i < 6; i++) // Calculate total bits
if(encodeBlock[i] == '1')
totalBits += bits[i];

return B64Table.charAt(totalBits); // Return char found in calculated index
}

private static byte[] BinaryStream(byte source[], Boolean isEncoding)
{
System.out.println("Streaming binary output...");
int srcLen = source.length;

byte asc = 0; int pad = 0; int val = 0; int encodeVal = 0;

if(isEncoding == true)
encodeVal = 8;
else
encodeVal = 6;

ByteBuffer binaryStream = ByteBuffer.allocate(srcLen * encodeVal);
int b[] = {1, 2, 4, 8, 16, 32, 64, 128, 255};
for(int i = 0; i < srcLen; i++)
{
//StringBuffer buffer = new StringBuffer(); // Stores binary values
asc = (byte) source[i]; // Retrieve ascii value

//pad = encodeVal - 8;
//for(int j = 0; j < pad; j++) // Fill the buffer with padding
// binaryStream.put((byte)'0');

for(int bit = 1; bit < encodeVal + 1; bit++) //New method to retrieve binary
binaryStream.put((asc & b[encodeVal - bit]) > 0 == true ? (byte) '1' : (byte) '0');


/*
while(true) // Compute and store binary values
{
val = asc % 2;

buffer.append( val == 0 ? '0' : '1');

asc >>= 1;

if(asc == 0)
break;

}


pad = encodeVal - buffer.length(); // Calculate padding

for(int j = 0; j < pad; j++) // Fill the buffer with padding
binaryStream.put((byte)'0');

binaryStream.put(buffer.reverse().toString().getBytes());
*/


}
System.out.println("Streaming binary complete.");
return binaryStream.array();

}

private static byte[] Decode(byte source[])
{
System.out.println("Converting Base64 Data to byte array...");
byte decodeTable[] = Base64Table2(source);
System.out.println("Conversion completed.");
byte binaryStream[] = BinaryStream(decodeTable, false);
System.out.println("Decoding binary stream to file data...");
byte decodeStream[] = DecodeStream(binaryStream);
System.out.println("File successfully decoded.");
return decodeStream;
}

private static byte[] Base64Table2(byte source[])
{
int srcLen = source.length;
ByteBuffer decodeTable = ByteBuffer.allocate(srcLen);
int bits[] = {32, 16, 8, 4, 2, 1};
String B64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int totalBits = 0;

for(int i = 0; i < srcLen; i++)
for(int j = 0; j < 64; j++)
if((char) source[i] == B64Table.charAt(j))
{
decodeTable.put((byte) j);
break;
}
return decodeTable.array();
}

private static byte[] DecodeStream(byte binaryStream[])
{
int srcLen = binaryStream.length;

int bits[] = {128, 64, 32, 16, 8, 4, 2, 1};
int totalBits = 0;

ByteBuffer decodeStream = ByteBuffer.allocate(srcLen / 8);

for(int i = 0; i < srcLen; i += 8)
{
totalBits = 0;
for(int j = 0; j < 8; j++)
{
if(i + j == srcLen) break;
if(binaryStream[i + j] == (byte)'1')
totalBits += bits[j];
}
decodeStream.put((byte) totalBits);
}
return decodeStream.array();
}

private static byte[] readFile(String fileName)
{
FileInputStream fis = null;
File input_file = new File(fileName);
int bytesRead = 0;
int fileLength = (int) input_file.length();
byte buffer[] = new byte[fileLength];

try{

fis = new FileInputStream(input_file);
}
catch(IOException e){
System.out.println("Could not read file: " + fileName);
}


try{
bytesRead = fis.read(buffer);
}
catch(IOException e){
System.out.println("Could not read file: " + fileName);
}

return buffer;
}

private static void writeFile(byte data[], String fileName)
{
FileOutputStream fos = null;
File output_file = new File(fileName);

try{
fos = new FileOutputStream(output_file);
}
catch(IOException e){
System.out.println("Could not create file: " + fileName);
}

try{
fos.write(data, 0, data.length);
}
catch(IOException e){
System.out.println("Could not write to file: " + fileName);
}

}
public static void main( String args[] )
{
/*
Arg # Argument Description
-----------------------------------------------------
args[0] -encode Encode Base64
-decode Decode Base64

args[1] -f Encoding/Decoding file
-s Encoding/Decoding string

args[2] -"" Path of source data
-"" Source Data string

args[3] -"" Define output location.
If undefined, outputs to
path of file ".base64"
unless -s argument is used
then it will be directly
outputted to console.
*/

if(args.length < 3)
DisplayHelp(10);
else
{
if(args[0].compareToIgnoreCase("-encode") == 0)
{
if(args[1].compareToIgnoreCase("-f") == 0)
{
byte encode_data[] = Encode(readFile(args[2]));
if(args.length != 4)
writeFile(encode_data, args[2] + ".base64");
else
writeFile(encode_data, args[3]);
}
else if(args[1].compareToIgnoreCase("-s") == 0)
{
byte encode_data[] = Encode(args[2].getBytes());
if(args.length != 4)
System.out.println("\n\nString Output: \n" + new String(encode_data) + "\n");
else
writeFile(encode_data, args[3]);
}
else
DisplayHelp(1);
}
else if(args[0].compareToIgnoreCase("-decode") == 0)
{
if(args[1].compareToIgnoreCase("-f") == 0)
{
byte decode_data[] = Decode(readFile(args[2]));
if(args.length != 4)
writeFile(decode_data, args[2] + ".base64");
else
writeFile(decode_data, args[3]);
}
else if(args[1].compareToIgnoreCase("-s") == 0)
{
byte decode_data[] = Decode(args[2].getBytes());
if(args.length != 4)
System.out.println("\n\nString Output: \n" + new String(decode_data) + "\n");
else
writeFile(decode_data, args[3]);
}
}
else
DisplayHelp(0);
}
/*
byte encoded_data[] = Encode(readFile("H:/zipper.zip"));
writeFile(encoded_data, "H:/zipper2.base64");
System.out.println();
System.out.print(new String(encoded_data));
System.out.println();
*/
}

private static void DisplayHelp(int argNum){
if(argNum != 10)
System.out.println("Arg[" + argNum + "] is not valid. Please read the table below: \n");
else
System.out.println("Not enough arguments have been passed. Read the table below: \n");

System.out.println("Arg #\tArgument\t\t\tDescription");
System.out.println("-----------------------------------------------------");
System.out.println("arg[0]\t-encode\t\t\t\tEncode Base64");
System.out.println("\t-decode\t\t\t\tDecode Base64\n");
System.out.println("arg[1]\t-f\t\t\t\tEncoding/Decoding file");
System.out.println("\t-s\t\t\t\tEncoding/Decoding string\n");
System.out.println("arg[2]\t (use quotes)\tPath of source data");
System.out.println("\t (use quotes)\tSource Data string\n");
System.out.println("arg[3]\t (use quotes)\tDefine output location. If");
System.out.println("\t\t\t\t\tundefined, outputs to");
System.out.println("\t\t\t\t\tpath of file *.base64");
System.out.println("\t\t\t\t\tunless -s argument is used");
System.out.println("\t\t\t\t\tthen it will directly");
System.out.println("\t\t\t\t\toutputted to console.\n");
System.out.println("Example of use (Encode File):\njava Base64 -encode -f \"C:/file.exe\" \"C:/encoded/file.base64\" ");
}
}
The Above source code is right to copy for anything purpose implementation,
That Source Code is created By : Didik Rawandi