import java.io.*;
import java.net.*;
public class SmtpClient {
public static void main (String argv[]) {
Socket smtpSocket = null;
DataOutputStream os = null;
BufferedReader is = null;
try {
smtpSocket = new Socket("mail.uajy.ac.id",25);
os = new DataOutputStream(smtpSocket.getOutputStream());
is = new BufferedReader(new InputStreamReader(smtpSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Nama komputer tidak dikenali");
} catch (IOException e) {
System.err.println("Tidak dapat melakukan operasi io");
}
if(smtpSocket!=null && os!=null && is!=null) {
try {
//mengirim dan mencetak pesan
System.out.println(is.readLine());
os.writeBytes("HELO\r\n");
System.out.print("HELLO\n");
System.out.println(is.readLine());
os.writeBytes("MAIL From:nyuyu7@gmail.com\r\n");
System.out.print("MAIL From:nyuyu7@gmail.com\n");
System.out.println(is.readLine());
os.writeBytes("RCTP To: kusnadi@mail.uajy.ac.id\r\n");
System.out.print("RCTP To: kusnadi@mail.uajy.ac.id\n");
System.out.println(is.readLine());
os.writeBytes("DATA\r\n");
System.out.print("DATA\n");
System.out.println(is.readLine());
os.writeBytes("From: nyuyu7@gmail.com\r\n");
System.out.print("From: nyuyu7@gmail.com\n");
System.out.println(is.readLine());
os.writeBytes("Subject: testing\r\n");
System.out.print("Subject: testing\n");
System.out.println(is.readLine());
os.writeBytes("Hi, ini percobaan kirim email\n");
System.out.print("Subject: testing\n");
System.out.println(is.readLine());
os.writeBytes("\r\n.\r\n");
System.out.print(".\n");
System.out.println(is.readLine());
os.writeBytes("QUIT\r\n");
System.out.print("QUIT\n");
System.out.println(is.readLine());
//Menutup semua objek
os.close();
is.close();
smtpSocket.close();
} catch (UnknownHostException e) {
System.err.println("Nama komputer tidak dikenali");
} catch (IOException e) {
System.err.println("Tidak dapt melakukan Operasi io");
}
}
}
}
1. Simpan dengan nama SmtpClient.java pada direktori mana saja, misal di D:\workspace\SmtpClient\src
2. Buka cmd (command prompt). ubah root-nya ke direktori penyimpanan kode program tadi (lihat Gambar 1).
3. Kompilasi kode program tersebut dengan perintah javac SmtpClient.java [enter]
4. Eksekusi kode programnya dengan perintah java SmtpClient [enter]
0 komentar: