Sunday, June 28, 2015

2 Cara Mengirim Email Dari Gmail Di Java

Sunday, June 28, 2015

Persiapan


Ada beberapa hal yang haris Anda persiapkan terlebih dahulu sebelum memulai aktifitas coding, yaitu
  • Berdoa sebelum memulai aktifitas coding :)
  • Download libary mail.jar dan activation.jar
  • Java Editor
  • Kopi dan snack untuk menemani coding :)


1. Menggunakan TLS (Transport Layer Security)


Untuk dapat menggunakan metode TLS, port yang anda gunakan adalah port 587.

Di bawah ini adalah contoh program Java mengirim email dari Gmail menggunakan TLS

Nama File : SendEmailGmail.java

package com.nursalim.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmailGmail {
	

	public static void main(String[] args) {
		
		final String FROM_EMAIL = "fromemailgmail@gmail.com";
		final String PASSWORD = "password";
		final String TO_EMAIL = "toemailgmail@gmail.com";

		// get gmail setting
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.port", "587");

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(
								FROM_EMAIL, PASSWORD);
																			
					}
				});
		
		try{
			
			// compose message
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(FROM_EMAIL));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(TO_EMAIL));
			message.setSubject("Testing Email From Gmail");  
			message.setText("Hello every body... \nHow are you?");
			
			// send message
			Transport.send(message);
			
			System.out.println("Message has been sent....");
			
			
		}catch(MessagingException me){
			me.printStackTrace();
		}
	}
}

Running

Message has been sent....

2. Menggunakan SSL (Secure Socket Layer)

Untuk dappat menggunakan metode SSL, Anda harus menambahkan port 465 pada settinggan Properties nya.

Di bawah ini adalah contoh program Java untuk mengirim email dari Gmail menggunakan metode SSL.

Nama File : SendEmailGmailSSL.java

package com.nursalim.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmailGmailSSL {
	

	public static void main(String[] args) {
		
		final String FROM_EMAIL = "fromemailgmail@gmail.com";
		final String PASSWORD = "password";
		final String TO_EMAIL = "toemailgmail@gmail.com";

		// get SSL gmail setting
		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() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(
								FROM_EMAIL, PASSWORD);
																			
					}
				});
		
		try{
			
			// compose message
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(FROM_EMAIL));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(TO_EMAIL));
			message.setSubject("Testing Email From Gmail");  
			message.setText("Hello every body... \nHow are you?");
			
			// send message
			Transport.send(message);
			
			System.out.println("Message has been sent....");
			
			
		}catch(MessagingException me){
			me.printStackTrace();
		}
	}
}

Running

Message has been sent....

                                                    ~~~## Java Mail ##~~~

Sekian tutorial singkat tentang 2 Cara Mengirim Email Dari Gmail Di Java. Semoga bermanfaat & Happy Learning Java Mail.

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2015 {Ora~Kelar} | Ora Kelar Kelar. All rights resevered. Designed by Templateism

Back To Top