Showing posts with label swing in java. Show all posts
Showing posts with label swing in java. Show all posts

March 16, 2014

Java Program that sent Email with attachment


Send an Email with attachment in java window application (gmail smtp : Simple Mail Transfer Protocol)


You can also sent reminder email

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.swing.JOptionPane;

public class SendMailSSL {
    public static void main(String[] args) {
        String msg = " Mail sent successfully (java mail service application)";
        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.debug", "true");  // Debug the program
        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("senderMailId@gmail.com","senderPassword");
                }
            });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("cfedprice@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("receiverMailId@yahoo.in"));
            message.setSubject(" Details about Syrian Revolution (Civil War) ");
            // create the message part
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            //message body
            messageBodyPart.setText("Hi,  This application send email with attachment to Receiver mail id ");
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            //attachment
            messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource("D:\\FaceBook\\SyrianRevolution.txt");
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName("SyrianRevolution.txt");
            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);
            //message.setFileName("hackThroughEmail.java");

            Transport.send(message);

            //System.out.println("Email sent successfully using this java application");
        } catch (MessagingException e) {
            msg = "Error while sending";
            throw new RuntimeException(e);
        }
        JOptionPane.showMessageDialog(null, msg);
    }
}

This example in java shows how to mail using java mail service to sent a mail. User need to create a gmail account). Automatic mail sending application using google spreadsheet (google script)

Image for success mail sent
Java SMTP Mail Sent Application


if you are getting an error : authentication failed, or gmail blocked login attempt, then you should enable the access for less secure apps in your account

 Some devices and apps use insecure sign-in technology to access your data.
Choosing Disable prevents these less secure devices and apps from accessing your Google Account.
Choosing Enable increases your chances of unauthorized account access but allows you to continue using these less secure devices and apps. Learn more

Access for less secure apps should be enabled


You can download the working jar from here download link
filename : javabelazy10203.rar
password for rar file extraction is : 12345ABCDE


Output

Email Template




http://javabelazy.blogspot.in/

June 10, 2012

JFrame

 The Example demonstrate a login application using java swing. On button click user enter into next screen. copy, paste and Run it. Better use an Eclispe IDE
 Login JFrame Example

 /**
 *
 */
package com.blog.belazy.Frame;

import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Rectangle;

/**
 * @author Belazy *
 */
public class FrameLogin extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel jContentPane = null;
    private JButton loginButton = null;
    private MainFrame mainFrame = null;
    private FrameLogin thisClass = null;

    /**
     * This method initializes loginButton  
     *   
     * @return javax.swing.JButton  
     */
    private JButton getLoginButton() {
        if (loginButton == null) {
            loginButton = new JButton();
            loginButton.setBounds(new Rectangle(96, 67, 98, 41));
            loginButton.setText("Login");
            loginButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    mainFrame = new MainFrame();
                    mainFrame.setVisible(true);
                    dispose();
                }
            });
        }
        return loginButton;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                FrameLogin thisClass = new FrameLogin();
                thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                thisClass.setVisible(true);
            }
        });
    }

    /**
     * This is the default constructor
     */
    public FrameLogin() {
        super();
        initialize();
    }

    /**
     * This method initializes this
     *
     * @return void
     */
    private void initialize() {
        this.setSize(300, 200);
        this.setContentPane(getJContentPane());
        this.setTitle("JFrame");
    }

    /**
     * This method initializes jContentPane
     *
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(null);
            jContentPane.add(getLoginButton(), null);
        }
        return jContentPane;
    }
  


}









---------------------------------------------------------------------------------------------

/**
 *
 */
package com.blog.belazy.Frame;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Rectangle;

/**
 * @author Belazy *
 */
public class MainFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel jContentPane = null;
    private FrameLogin frame = null;
    private JButton jButton = null;

    /**
     * This is the default constructor
     */


    public MainFrame(FrameLogin f) {
       
        super();
        this.frame = f;
        initialize();
   
    }

    public MainFrame() {
        super();
        initialize();
    }

    /**
     * This method initializes this
     *
     * @return void
     */
    private void initialize() {
        this.setSize(300, 200);
        this.setContentPane(getJContentPane());
        this.setTitle("JFrame");
    }

    /**
     * This method initializes jContentPane
     *
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(null);
            jContentPane.add(getJButton(), null);
        }
        return jContentPane;
    }

    /**
     * This method initializes jButton   
     *    
     * @return javax.swing.JButton   
     */
    private JButton getJButton() {
        if (jButton == null) {
            jButton = new JButton();
            jButton.setBounds(new Rectangle(79, 69, 121, 38));
            jButton.setText("delete");
            jButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                   
                    deleteOld();
                    System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
                }
            });
        }
        return jButton;
    }

    protected void deleteOld() {
        frame.dispose();
       
    }

}

Thanking you....

January 07, 2012

TextField integer/Number validator in java swing

Example show how to create a text field in java swing that accepts only numbers
initializing variable

private JFormattedTextField expAmount_txt = null;
adding to container

panel.add(getExpAmount_txt(),null);
private JFormattedTextField getExpAmount_txt() {
if (expAmount_txt == null) {
Format general = NumberFormat.getInstance();
expAmount_txt = new JFormattedTextField(general);
expAmount_txt.setBounds(new Rectangle(120, 40, 120, 20));
expAmount_txt.setHorizontalAlignment(JFormattedTextField.RIGHT);
}
return expAmount_txt;
}



Thanking you..........

Facebook comments