JavaMail

2 12 2007

Di dunia internet kita sering menggunakan email terbesar seperti Yahoo!, Hotmail, Gmail dan sebagainya. Untuk mengirim dan menerima email di jaringan internet.
Untuk membuat email dengan java, menggunakan JavaMail dibuat oleh Team Sun.
Di sini bahas menggunakan SMTP dan POP3 di gmail.

Send.java

Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465"); // smtp port
props.put("mail.smtp.socketFactory.port", "465"); // smtp socket port
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");

Session session = Session.getDefaultInstance(props, new Authenticator() {
	@Override
	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication("user", "passwd");
	}

});

MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("sender@gmail.com"));
msg.setSubject("Test send gmail");
msg.setText("Hai JavaMail!");
System.out.println("Sending ...");
Transport.send(msg);
System.out.println("Sent OK ...");

Receive.java

Properties props = System.getProperties();
props.put("mail.pop3.port", "995"); // pop port
props.put("mail.pop3.starttls.enable", "true");
props.put("mail.pop3.auth", "true");
props.put("mail.pop3.socketFactory.port", "995"); // pop socket port
props.put("mail.pop3.socketFactory.class",
		"javax.net.ssl.SSLSocketFactory");
props.put("mail.pop3.socketFactory.fallback", "false");

Session session = Session.getDefaultInstance(props, new Authenticator() {
	@Override
	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication("user", "passwd");
	}

});

Buka folder “INBOX” dan menampilkan message yang ada.

Store store = null;
Folder folder = null;
store = session.getStore("pop3s");
store.connect("pop.gmail.com", "user@gmail.com", "passwd");

folder = store.getDefaultFolder();
if (folder == null)
	throw new Exception("No default folder");
folder = folder.getFolder("INBOX");
if (folder == null)
	throw new Exception("No POP3 INBOX");
folder.open(Folder.READ_ONLY);

Message[] messages = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
	System.out.println("From : " + messages[i].getFrom()[0]);
	System.out.println("Subject : " + messages[i].getSubject());

	Part messagePart = messages[i];
	Object content = messagePart.getContent();
	if (content instanceof Multipart) {
		messagePart = ((Multipart) content).getBodyPart(0);
		System.out.println("test : " + messagePart.getContent());
	}

	InputStream is = messagePart.getInputStream();
	BufferedReader reader = new BufferedReader(
			new InputStreamReader(is));
	String thisLine = reader.readLine();
	System.out.println("BODY: \n");
	while (thisLine != null) {
		System.out.println(thisLine);
		thisLine = reader.readLine();
	}
}
System.out.println("-----------------------------");

Mudah khan? Selamat mencoba.
Untuk dapat source, download di sini.


Actions

Information

2 responses

3 12 2007
dhiku

wuhuuu thx ya. bermanfaat sekali nih.

14 05 2010
agung

pencerahane asyik bgtsz…
n klu pake yahooo…. settingnya yg d rubaha apa aja bang..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: