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.....


No comments: