Kirim Email Pake Java

Lagi-lagi iseng aja pengen nulis sesuatu tapi bingung nulis apa ya?ya udah nulis ini aja barangkali bermanfaat buat gan-gan sekalian…jadi ditulisan ini pengen sharing aja gimana cara bikin source code java yang dapat kirim email pake smtp ssl gmail..langsung ke TKP aja yax..berikut bahan cendolnya…


import javax.mail.*;
import javax.mail.internet.*;

import java.util.Properties;


public class SendGmail {

    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final int SMTP_HOST_PORT = 465;
    private static final String SMTP_AUTH_USER = "msyadid@gmail.com";
    private static final String SMTP_AUTH_PWD  = "blablabla";

    public static void main(String[] args) throws Exception{
       new SendGmail().test();
    }

    public void test() throws Exception{
        Properties props = new Properties();

        props.put("mail.transport.protocol", "smtps");
        props.put("mail.smtps.host", SMTP_HOST_NAME);
        props.put("mail.smtps.auth", "true");
        // props.put("mail.smtps.quitwait", "false");

        Session mailSession = Session.getDefaultInstance(props);
        mailSession.setDebug(true);
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject("Testing SMTP-SSL");
        message.setContent("This is a test", "text/plain");

        message.addRecipient(Message.RecipientType.TO,
             new InternetAddress("adid_m@yahoo.com"));

        transport.connect
          (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

        transport.sendMessage(message,
            message.getRecipients(Message.RecipientType.TO));
        transport.close();
    }
}

selamat mencoba ya gan-gan sekalian ntar kalo dah sukses bikin cendolnya…jangan lupa bagi-bagi ya cendolnya …hehe..

o ya kelupaan belum kasih resepnya, berikut resep bikin cendolnya gan :
1. Buka IDE java kamyu (ex : Mr.Beans alias NetBeans)

2. Buat sebuah project java application baru (ex : Application1)

3. Isi nama projectnya misal Application1

4. Maka selanjutnya bakalan muncul tree folder project seperti ini :

5. Selanjutnya klik kanan pada source package dan pilih java class untuk membuat file java yang baru

6. Isikan nama file javanya /class name (ex : SendGmail)

7. Selanjutnya akan muncul file baru yang masih kosong seperti di bawah ini

8. Selanjutnya copy paste source code yang ada diatasnya
9. Jika muncul merah-merah, add library berikut : mail.jar DOWNLOAD DI SINI

10. caranya klik kanan pada project lalu pilih properties dan pilih library kemudian add jar, pilih file library mail.jar lalu klik ok.

11. coba run project caranya klik kanan pada project pilih Run Project..

12. jika sukses muncul log berikut

“PerHaTiAn-PeRhAtiAn tuToriaL bEriKuT diBuAt sEdEtAiL MuNgKin AgAr AgAn-AgAn yAng tErTaRik JaVa tApI BiNgUnG DaLam pRakTeKnyA dApAt dEnGan MuDaH mEncoBanYa, BaGi AgAn-AgAn yAnG uDaH MaStER MoHoN BiMbInGanYa..”

source : http://www.rgagnon.com

Advertisements

15 thoughts on “Kirim Email Pake Java

  1. Hi there! How are you today? It’s only that i like your site so much, and that i believe you could start making a bunch of money from it. I run a couple of thriving websites that i started making some cash from lately. They are using something called a content blocker widget, it makes guests fill out a survey inorder to get access to highly valued content or to download programs or tools that they want. And each time they do a easy quiz i make around two dollars. Pretty cool eh..? Been making alot more from it than google adsense. Feel free to email me, or you can check it out through my refferal link. http://www.tinyurl.com/yevwfst, Kind Regards, Henrik Z. Hannsen

  2. waaaaaaaaaaaahhhh kerenn isi blog nya
    IT abizzzzzzzz, java programmer sejati, ckckckck
    pgn nyobain ah tar d rumah…

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s