Kirim Email dengan JavaMail

Dengan JavaMail, temen-temen dapat melakukan pengiriman Email dengan mudah melalui program Java yang dibuat. Pengiriman Email bisa dilakukan pada program Java Desktop ataupun Java Web, asalkan terdapat library JavaMail.

Untuk aplikasi Java Web, jika temen-temen menggunakan Glassfish maka JavaMail langsung terintegrasi. Akan tetapi jika menggunakan Apache Tomcat maka library JavaMail harus disertakan dalam project.

Kali ini, saya akan membuat project yang digunakan untuk mengirim Email dengan menggunakan Account dari Google Mail (gmail.com).


package achmad.lutfi.sendemail;

import java.util.Properties;
 import javax.mail.*;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;

/**
 *
 * @author New
 */
 public class SendMailSSL {

/**
 * @param args the command line arguments
 */
 public static void main(String[] args) {
 // TODO code application logic here
 Properties props = new Properties();
 props.put("mail.smtp.host", "smtp.gmail.com");
 props.put("mail.smtp.socketFactory.port", "465");
 props.put("mail.smtp.socketFactory.class",
 "javax.net.ssl.SSLSocketFactory");
 props.put("mail.smtp.auth", "true");
 props.put("mail.smtp.port", "465");

Session session = Session.getDefaultInstance(props,
 new javax.mail.Authenticator() {
 @Override
 protected PasswordAuthentication getPasswordAuthentication() {
 return new PasswordAuthentication("achmad","123456");
 }
 });

try {

Message message = new MimeMessage(session);
 message.setFrom(new InternetAddress("achmad@gmail.com"));
 message.setRecipients(Message.RecipientType.TO,
 InternetAddress.parse("achmadlutfi@gmail.com"));
 message.setSubject("Kirim Email dengan JavaMail");
 message.setText("Halo rek," +
 "\n\n email dengan JavaMail!");

Transport.send(message);

System.out.println("Terkirim");

} catch (MessagingException e) {
 throw new RuntimeException(e);
 }

}
 }

Sedikit sekali bukan kodenya, dengan kode diatas pengiriman email sudah dapat dilakukan.

, , ,

  1. #1 by Muhammad Zaky Erdiansyah on January 14, 2013 - 12:50 pm

    Apakah perlu koneksi dengan internet untuk menggunakan fitur pengiriman email di Netbeans?

    • #2 by Achmad on January 14, 2013 - 2:41 pm

      Tergantung dari server email yanng digunakan, kalo email server lokal ya gak perlu internet.
      tetapi untuk server email seperti gmail, yahoo, tentu saja membutuhkan akses internet

  2. #3 by fajar on May 2, 2013 - 8:16 am

    Assalamu’alaikum

    mas, kalau mau kirim emailnya plus attachment gmn?

    fungsi apa yg digunakan?

    thanks b4 😀

    • #4 by Achmad on June 2, 2013 - 5:58 am

      Pake Multipart gan. bisa dicoba. mudah kok.

  3. #5 by Jono on November 11, 2013 - 9:19 pm

    Gan ada eror kaya gini kenapa yah ?
    Exception in thread “main” java.lang.RuntimeException: javax.mail.MessagingException: Exception reading response;
    nested exception is:
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at Email.Send.SendingMailSSL.main(SendingMailSSL.java:54)
    Caused by: javax.mail.MessagingException: Exception reading response;
    nested exception is:
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    • #6 by Achmad on November 16, 2013 - 4:35 pm

      Connect ke mail apa ya? yahoo ato gmail?
      coba di cek tanggal komputernya, error itu krn sslnya gak sesuai.

  4. #7 by Eric Nursevan Wicaksono on June 22, 2014 - 10:45 pm

    permisi mas achmad mau tanya, di bagian ini PasswordAuthentication(“achmad”,”123456″); itu isi paswword bukan ya? kalo emang isi password kok ada 2 ya? maaf masih belajar hehe

    • #8 by Achmad on June 24, 2014 - 1:29 pm

      achmad : itu user nya
      123456 : password

      Memang gak normal user sama passwordnya, itu cuman contoh saja mas.

  5. #9 by Samsul Huda on July 4, 2014 - 11:28 pm

    error ky gini gmana mas bro..
    Exception in thread “main” java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
    at javax.mail.Session.initLogger(Session.java:227)
    at javax.mail.Session.(Session.java:212)
    at javax.mail.Session.getDefaultInstance(Session.java:315)
    at sendmail.main(sendmail.java:18)

    • #10 by Achmad on July 24, 2014 - 4:16 pm

      COba dikirim lengkap source codenya mas.

  6. #11 by Yudhi Setiawan on April 14, 2015 - 11:50 am

    Mas, kalau data datanya dari database bagaimana ya?

    pengirim, penerima, subjek dan isi email semuanya dari database, mengambil data dengan status N atau blm terkirim, nah setelah terkirim statusnya berubah jadi Y atau sudah terkirim

    • #12 by Achmad on April 14, 2015 - 11:59 am

      Data apa ya? kalo data pesan dan data email, bisa dengan menyeleksi dulu datanya dari database, kemudian dibuat perulangan untuk proses pengirimannya.

      Untuk status N atau Y, di databasenya dibuat status aja, jadi kalo udah dikirim diubah statusnya.

  7. #13 by akrilvha on June 14, 2015 - 8:11 pm

    mas kalo errornya ini kenapa ya ?

    Exception in thread “main” java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed;
    nested exception is:
    class javax.mail.AuthenticationFailedException
    at kirimemail.Kirimemail.main(Kirimemail.java:50)
    Caused by: javax.mail.SendFailedException: Sending failed;
    nested exception is:
    class javax.mail.AuthenticationFailedException
    at javax.mail.Transport.send0(Transport.java:218)
    at javax.mail.Transport.send(Transport.java:80)
    at kirimemail.Kirimemail.main(Kirimemail.java:45)
    Java Result: 1

    • #14 by Achmad on June 15, 2015 - 11:16 am

      username sama password’nya udah bener? coba di cek dulu

    • #15 by Achmad on July 11, 2015 - 8:31 am

      coba cek username atau passwordnya

  8. #16 by Yudi on August 6, 2015 - 4:25 pm

    kalo mau masukin email nya lebih dari satu menggunakan separator ; (titikkoma) bagaimana? misal contoh1@example.com;contoh2@example.com

    • #17 by Achmad on August 6, 2015 - 7:58 pm

      ini email tujuan ya?

  9. #18 by harri on December 22, 2015 - 3:13 pm

    gan, ini kenapa ya?

    Exception in thread “main” java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via your web browser and
    534-5.7.14 then try again.
    534-5.7.14 Learn more at
    534 5.7.14 https://support.google.com/mail/answer/78754 tb8sm44044924pab.22 – gsmtp

    at Java.Email.SendMailSSL.main(SendMailSSL.java:46)
    Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via your web browser and
    534-5.7.14 then try again.
    534-5.7.14 Learn more at
    534 5.7.14 https://support.google.com/mail/answer/78754 tb8sm44044924pab.22 – gsmtp

    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
    at javax.mail.Service.connect(Service.java:317)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at Java.Email.SendMailSSL.main(SendMailSSL.java:43)
    Java Result: 1

    • #19 by Achmad on December 26, 2015 - 6:32 am

      kyk’e gmailnya ada keamanan selain password ya gan? kyk lokasi gitu?

      javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via your web browser and

      udah dicoba login via web dulu gan?

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: