Wednesday, May 16, 2012

Sending emails in java using yahoo and hotmail

Hello friends, previously we discussed how to send emails in java with gmail account. Today I'm going to show you how we can do the same task using yahoo and hotmail. Only few changes are there. Here's the code..
Remember, first you have to add jar file and headers as mentioned here.
http://mihirans.blogspot.com/2012/05/sending-emails-in-java-application.html

YAHOO



public void sendYahooMail(String sender, String passwd, String [] receiver, String sub, String body) throws MessagingException
    {
        String host = "smtp.mail.yahoo.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", "465");
        props.put("mail.smtp.auth", "true");

        String[] to = receiver; 

        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();
    }




HOTMAIL



public void sendHotMail(String sender, String passwd, String [] receiver, String sub, String body) throws MessagingException
    {
        String host = "smtp.live.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("smtp");
        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

Here's my Interface


And here is the button click even of "Send Email" button.

   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
        char [] passwd = jPasswordField1.getPassword();
        String spass = "";
        
        String Client = jComboBox1.getSelectedItem().toString();
        
        for (int i = 0; i < passwd.length; i++) 
        {
            spass += passwd[i];
        }
        
        
        String from = jTextField1.getText()+"@"+Client;
        String pass = spass.toString();
        String [] to = jTextField3.getText().toString().split(",");
        String sub = jTextField4.getText().toString();
        String body = jTextArea1.getText().toString();
        
        SendMail s = new SendMail();
        
        try
        {
            if(Client == "gmail.com"){
                s.sendGMail(from,pass,to,sub,body);
            }
            else if(Client == "yahoo.com"){
                s.sendYahooMail(from,pass,to,sub,body);
            }
            else{
                s.sendHotMail(from,pass,to,sub,body);
            }           
        }
        catch(Exception e)
        {
                JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }  


Ok guyz, that's it. See you again.
Thank You !

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 !