Tuesday, May 15, 2012

Sending emails in Java Application

Hello guys, Today i'm going to show you how to send emails from your desktop application in java. I'm using NetBeans IDE 7.1.2 for this.

Usage : In your projects, some times you'll have to send emails to your customers or clients from your desktop application. In that case this will be very useful. Try this out. Good Luck !

First of all you must have to download two file in order to do this task.


1.) Javasoft's JavaMail class files which can be downloaded from here
http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html#javamail-1.4.5-oth-JPR

&

2.) JavaBeansTM Activation Framework extension or JAF (javax.activation). It is available at
http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-plat-419418.html#jaf-1.1.1-fcs-oth-JPR

Extract two files and add the .JAR files (mail.jar, activation.jar) which are inside that folders into your project Libraries folder.







Ok. In my case, I'm having my main class called "Email.java", JFrame class called "MainEmail.java" which is the UI and "SendMail.java" which includes the methods.

Inside the SendEmail.java class I have the method which sends the emails.

Remember you have to import these header files first.

======================================================================

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;


public class SendMail {
    
public void sendGMail(String sender, String passwd, String [] receiver, String sub, String body) throws MessagingException
    {
        String host = "smtp.gmail.com";
        String from = sender;
        String pass = passwd;
        
        Properties props = System.getProperties();
    
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        String[] to = receiver; // added this line

        Session session = Session.getDefaultInstance(props, null);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));

        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for( int i=0; i < to.length; i++ ) { 
            toAddress[i] = new InternetAddress(to[i]);
        }
        System.out.println(Message.RecipientType.TO);

        for( int i=0; i < toAddress.length; i++) { 
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }
        message.setSubject(sub);
        message.setText(body);
        Transport transport = session.getTransport("smtps");
        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }





======================================================================



In here you can send emails from Gmail accounts only. In future, I'll show you how to send emails from others email clients like yahoo,hotmail etc..


Thank You !

No comments:

Post a Comment