File Encryption Decreption with key (Single File or Multiple Files) – swings GUI

February 8, 2012

Note:

1) Main Class File: FileEncryptDecrypt.java

2) Result encrypted/decrypted file’s will be available in your java application files directory it self.

3) In case of  ” Multiple Files ” , make sure you have to put all the source files into your java application running directory (or) put your java .class file’s into your source directory where the files are available.

Source Code:

1. FileEncryptDecrypt.java


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FileEncryptDecrypt extends JFrame implements ActionListener{

JButton b1,b2,b3,b4;
 Font font;
 JOptionPane op=new JOptionPane();

FileEncryptDecrypt(){

ImageIcon b1i=new ImageIcon("E.gif");
 ImageIcon b2i=new ImageIcon("D.gif");
 b2=new JButton("Secure All Files",b2i);
 ImageIcon img2=new ImageIcon("java.gif");
 b3=new JButton("",img2);
 ImageIcon img3=new ImageIcon("brain.gif");
 ImageIcon img4=new ImageIcon("NLogo.gif");
 b1=new JButton("Secure One File",img3);
 b4=new JButton("",img4);
 b1.setBackground(Color.green);
 b2.setBackground(Color.green);

Container c=getContentPane();
 op.showMessageDialog(this,"Lets Protect Your Data...........");
 op.showMessageDialog(this,"By Mr.Nanddeswar(MCA)");
 font=new Font("times new roman",Font.BOLD,22);
 c.setLayout(null);
 b1.setBounds(10,10,400,200);
 b2.setBounds(600,10,350,200);
 b4.setBounds(10,620,300,150);
 b3.setBounds(860,620,120,150);
 b1.setFont(font);
 b2.setFont(font);
 b3.setFont(font);
 b4.setFont(font);
 c.add(b1);c.add(b2);c.add(b3);c.add(b4);
 b1.addActionListener(this);
 b2.addActionListener(this);
 b3.addActionListener(this);
 b4.addActionListener(this);

}
 public void actionPerformed(ActionEvent ae){
 Object o=ae.getSource();
 if(o==b1){

SingleFileEncrypt ne=new SingleFileEncrypt();
 ne.setTitle("Secure One File");
 ne.setSize(1000,800);
 ne.setVisible(true);
 //ne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

if(o==b2){

MultipleFilesEncrypt nv=new MultipleFilesEncrypt();
 nv.setTitle("Secure All Files");
 nv.setSize(1000,800);
 nv.setVisible(true);
 //nv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

if(o==b3){
 op.showMessageDialog(this,"Java Logo");
 }

if(o==b4){
 op.showMessageDialog(this,"Nandu Logo");
 }
 }

public static void main(String ar[])throws Exception{

FileEncryptDecrypt ned=new FileEncryptDecrypt();
 ned.setTitle("Nandu Experiment");
 ned.setSize(1000,800);
 ned.setVisible(true);
 ned.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}//end of file

2.SingleFileEncrypt.java


import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.util.zip.*;
import javax.swing.*;
import java.io.*;
import javax.swing.border.*;
public class SingleFileEncrypt extends JFrame implements ActionListener{
 JButton b1,b2,b3,b4,close;
 File f;
 Font font;
 JOptionPane op=new JOptionPane();
 String fname="";
 FileInputStream fis;
 JFileChooser fc;
 DeflaterOutputStream dos;
 int key=0;
 FileOutputStream fos;
 String ext="";
 InflaterInputStream iis;
 String output="";
 SingleFileEncrypt(){

ImageIcon b1i=new ImageIcon("E.gif");
 ImageIcon b2i=new ImageIcon("D.gif");
 b2=new JButton("Decrypt",b2i);
 ImageIcon img2=new ImageIcon("java.gif");
 b3=new JButton("",img2);
 ImageIcon img3=new ImageIcon("brain.gif");
 ImageIcon img4=new ImageIcon("NLogo.gif");
 b1=new JButton("Encrypt",img3);
 b4=new JButton("",img4);
 close=new JButton("Close Window");
 fc=new JFileChooser();
 b1.setBackground(Color.green);
 b2.setBackground(Color.green);
 Container c=getContentPane();
 op.showMessageDialog(this,"Lets Protect Your Data...........");
 op.showMessageDialog(this,"By Mr.Nanddeswar(MCA)");
 font=new Font("times new roman",Font.BOLD,33);
 c.setLayout(null);
 b1.setBounds(10,10,300,200);
 b2.setBounds(600,10,300,200);
 b4.setBounds(10,620,300,150);
 b3.setBounds(860,620,120,150);
 close.setBounds(650,620,100,50);
 b1.setFont(font);
 b2.setFont(font);
 b3.setFont(font);
 b4.setFont(font);
 close.setFont(font);
 c.add(b1);c.add(b2);c.add(b3);c.add(b4);c.add(close);
 b1.addActionListener(this);
 b2.addActionListener(this);
 b3.addActionListener(this);
 b4.addActionListener(this);
 close.addActionListener(this);
 }
 public void actionPerformed(ActionEvent ae){
 Object o=ae.getSource();
 if(o==b1){
 int n=fc.showOpenDialog(this);
 if(n==JFileChooser.APPROVE_OPTION){
 f=fc.getSelectedFile();
 try{
 try{
 key=Integer.parseInt(op.showInputDialog("Enter Key:"));
 }catch(NumberFormatException ne){
 key=2;
 op.showMessageDialog(this,"The default KEY is used");
 }
 fis=new FileInputStream(f);
 output=op.showInputDialog(this,"Enter the Output Encrypted file Name");
 if(output.equals(""))
 {
 output="Default";
 op.showMessageDialog(this,"The Default File Name is used");
 }
 ext=op.showInputDialog("Enter extention");
 if(!ext.equals(""))
 fos=new FileOutputStream(output+"."+ext);
 else
 {
 fos=new FileOutputStream(output+".exe");
 op.showMessageDialog(this,"File created with Default extension");
 }

dos=new DeflaterOutputStream(fos);
 int ed;
 while((ed=fis.read())!=-1) dos.write(ed+key);
 fis.close();dos.close();fis.close();
 op.showMessageDialog(this,"Encryption Successfully Completed");
 op.showMessageDialog(this,"Encrypted File will be placed at where the project file is existed ");
 ext="";
 output="";
 }catch(Exception e){e.printStackTrace();}
 }
 }
 if(o==b4){

 op.showMessageDialog(this,"Nandu Logo");
 }
 if(o==b2){
 int n=fc.showOpenDialog(this);
 if(n==JFileChooser.APPROVE_OPTION){
 f=fc.getSelectedFile();
 try{
 fis=new FileInputStream(f);
 try{
 key=Integer.parseInt(op.showInputDialog("Enter Key:"));
 }catch(NumberFormatException ne){
 key=2;
 op.showMessageDialog(this,"The default KEY is used");
 }
 iis=new InflaterInputStream(fis);
 output=op.showInputDialog(this,"Enter output Decrypted file name");
 if(output.equals(""))
 {
 output="Default";
 op.showMessageDialog(this,"The Default File Name is used");
 }
 ext=op.showInputDialog("Enter extention");
 if(!ext.equals(""))
 fos=new FileOutputStream(output+"."+ext);
 else
 {
 fos=new FileOutputStream(output+".txt");
 op.showMessageDialog(this,"File created with Default extension");
 }
 int dd;
 while((dd=iis.read())!=-1) fos.write(dd-key);
 fis.close();iis.close();fos.close();
 op.showMessageDialog(this,"Decryption Successfully Completed");
 op.showMessageDialog(this,"Decrypted File will be placed at where the project file is existed ");
 ext="";
 output="";
 }catch(Exception e){e.printStackTrace();}
 }
 }
 if(o==b3){

 op.showMessageDialog(this,"Java Logo");
 }

if(o==close){

 this.dispose();
 }
 }

}
//end of file

3.MultipleFilesEncrypt.java


import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.zip.*;
import javax.swing.border.*;
class MultipleFilesEncrypt extends JFrame implements ActionListener{
 Container c;
 JOptionPane jp;
 JButton v,av,b3,b4,close;
 String output="Default";
 Font font;
 int key=2;
 String ext="";
 String oext="";
 MultipleFilesEncrypt(){
 c=this.getContentPane();
 font=new Font("times new roman",Font.BOLD,33);
 c.setLayout(null);
 jp=new JOptionPane();
 v=new JButton("Encrypt All Files");
 c.add(v);

ImageIcon img2=new ImageIcon("java.gif");
 ImageIcon img4=new ImageIcon("NLogo.gif");

av=new JButton("Decrypt All Files");
 c.add(av);

close=new JButton("Close Window");
 c.add(close);

b3=new JButton("",img2);
 c.add(b3);

b4=new JButton("",img4);
 c.add(b4);

v.setBounds(10,10,300,200);
 av.setBounds(600,10,300,200);
 b4.setBounds(10,620,300,150);
 b3.setBounds(860,620,120,150);
 close.setBounds(650,620,100,50);
 v.setFont(font);
 av.setFont(font);
 b3.setFont(font);
 b4.setFont(font);
 close.setFont(font);
 v.addActionListener(this);
 av.addActionListener(this);
 b3.addActionListener(this);
 b4.addActionListener(this);
 close.addActionListener(this);
 }

public void actionPerformed(ActionEvent ae){

Object o=ae.getSource();

if(o==v){
 try{
 key=Integer.parseInt(jp.showInputDialog("Enter Key to Encrypt:"));

 }catch(NumberFormatException ne){
 key=2;
 jp.showMessageDialog(this,"The default KEY is used");
 }

output=jp.showInputDialog(this,"Enter the Output Encrypted file's Name:");
 if(output.equals(""))
 {
 output="Default";
 jp.showMessageDialog(this,"The Default File Name is used.");
 }
 ext=jp.showInputDialog("Enter extention of Input file's");
 if(ext.equals(""))
 {
 ext="jpg";
 jp.showMessageDialog(this,"The Default extention . JPG is used.");
 }

 oext=jp.showInputDialog("Enter extention of Output file's to generate:");
 if(oext.equals(""))
 {
 oext="exe";
 jp.showMessageDialog(this,"The Default extention . EXE is used.");
 }

jp.showMessageDialog(this,"Wait until all files are created in your folder...");

File avpath=new File("MultipleFilesEncrypt.class");
 String tep=avpath.getAbsolutePath();
 StringTokenizer st=new StringTokenizer(tep,"\\");
 int j=0;
 String tep2=tep.replace("\\","/");
 String tep3="";
 while(j<st.countTokens()-1){
 tep3+=st.nextToken()+"/";
 }
 String directory=tep3;
 java.util.List<String> files=new java.util.ArrayList<String>();
 File dir=new File(directory);
 for(File file:dir.listFiles()){
 if(file.getName().endsWith("."+ext)){
 files.add(file.getName());
 }
 }
 int listSize=files.size();
 for(int i=0;i<listSize;i++){
 try{
 String s=files.get(i);
 File delete=new File(directory+s);
 Thread.sleep(2000);

 FileInputStream fis=new FileInputStream(delete);
 FileOutputStream fos=new FileOutputStream(output+(i+1)+"."+oext);
 DeflaterOutputStream dos=new DeflaterOutputStream(fos);
 int ed;
 while((ed=fis.read())!=-1) dos.write(ed+key);
 fis.close();dos.close();fis.close();
 }catch(Exception e){e.printStackTrace();}
 }

jp.showMessageDialog(this,"Encryption Successfully Completed");
 jp.showMessageDialog(this,"Encrypted File's will be placed at where the project file is existed ");
 key=2;
 ext="";
 oext="";
 }//end of if(o==v) condition

if(o==av){

try{
 key=Integer.parseInt(jp.showInputDialog("Enter Key:"));

 }catch(NumberFormatException ne){
 key=2;
 jp.showMessageDialog(this,"The default KEY is used");
 }

 output=jp.showInputDialog(this,"Enter the Output Encrypted file's Name");
 if(output.equals(""))
 {
 output="Default";
 jp.showMessageDialog(this,"The Default File Name is used");
 }
 ext=jp.showInputDialog("Enter extention of Input file's");
 if(ext.equals(""))
 {
 ext="exe";
 jp.showMessageDialog(this,"The Default extention . EXE is used.");
 }

oext=jp.showInputDialog("Enter extention of Output file's to generate:");
 if(oext.equals(""))
 {
 oext="jpg";
 jp.showMessageDialog(this,"The Default extention . JPG is used.");
 }

 jp.showMessageDialog(this,"Please wait until all files are created in your folder...");

File avpath=new File("MultipleFilesEncrypt.class");
 String tep=avpath.getAbsolutePath();
 StringTokenizer st=new StringTokenizer(tep,"\\");
 int j=0;
 String tep2=tep.replace("\\","/");
 String tep3="";
 while(j<st.countTokens()-1){
 tep3+=st.nextToken()+"/";
 }
 String directory=tep3;
 java.util.List<String> files=new java.util.ArrayList<String>();
 File dir=new File(directory);
 for(File file:dir.listFiles()){
 if(file.getName().endsWith("."+ext)){
 files.add(file.getName());
 }
 }
 int listSize=files.size();
 for(int i=0;i<listSize;i++){
 try{
 String s=files.get(i);
 File delete=new File(directory+s);
 Thread.sleep(2000);
 FileInputStream fis=new FileInputStream(delete);
 InflaterInputStream iis=new InflaterInputStream(fis);
 FileOutputStream fos=new FileOutputStream(output+(i+1)+"."+oext);
 int dd;
 while((dd=iis.read())!=-1) fos.write(dd-key);
 fis.close();iis.close();fos.close();
 }catch(Exception e){e.printStackTrace();}
 }
 jp.showMessageDialog(this,"Decryption Successfully Completed");
 jp.showMessageDialog(this,"Decrypted File's will be placed at where the project file is existed ");
 key=2;
 ext="";
 oext="";
 }//end of if(o==av) condition

if(o==b3){

 jp.showMessageDialog(this,"Java Logo");
 }
 if(o==b4){

 jp.showMessageDialog(this,"Nandu Logo");
 }

if(o==close){

 this.dispose();
 }

}//end of actionPerformed() method

}
//end of file

//End of Code....
Share and Enjoy:
  • Twitter
  • Google Buzz
  • DZone
  • Digg
  • Reddit
  • Diigo
  • StumbleUpon
  • del.icio.us
  • Yahoo! Buzz
  • Google Bookmarks
  • LinkedIn
  • Mixx
  • email
  • PDF
  • Add to favorites

Java Mail – Swings GUI ( Send Email / See Inbox from Java Application )

February 8, 2012

Required jar files:
1. activation-1.1.1.jar
2. mail-1.4.1.jar

Note: 1) This example is applicable to Gmail account only as of now.If you want to configure another mail account , just change the properties in java files .

Source Code:

1. EmailLogin.java


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

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;
import com.sun.mail.pop3.POP3Store;
import javax.mail.Folder;

class EmailLogin extends JFrame implements ActionListener{

Container c;

 JLabel fromLabel;
 JLabel emailIdLabel;
 JLabel pwdLabel;

 JTextField emailIdText;
 JPasswordField pwdText;

 JButton login;
 JButton cancel;

 JButton sendMail;
 JButton inbox;

 JButton dispose;

 Properties properties;
 Properties props;

 POP3Store emailStore;

 Session emailSession;
 Session loginSession;

 String fromEmailId="";
 String fromEmailPwd="";

 String inboxMessagesString="";

 Font font;

 public EmailLogin(){
 c=this.getContentPane();
 c.setLayout(null);

 font = new Font("Times New Roman", Font.BOLD, 14);

 properties = new Properties();
 properties.put("mail.pop3.host", "pop.gmail.com");

 emailSession=null;
 emailStore=null;

 fromLabel=new JLabel("From: ");
 emailIdLabel=new JLabel("Email-ID: ");
 pwdLabel=new JLabel("Password: ");

 emailIdText=new JTextField(40);
 pwdText=new JPasswordField(40);

 login=new JButton("Login");
 cancel=new JButton("Cancel");

 sendMail=new JButton("Compose Mail");
 inbox=new JButton("Inbox");

 dispose=new JButton("Dispose");

 login.addActionListener(this);
 cancel.addActionListener(this);
 sendMail.addActionListener(this);
 inbox.addActionListener(this);
 dispose.addActionListener(this);

 emailIdLabel.setBounds(200,50,200,30);
 emailIdText.setBounds(300,50,200,30);
 emailIdText.setFont(font);

 pwdLabel.setBounds(200,100,200,30);
 pwdText.setBounds(300,100,200,30);
 pwdText.setFont(font);

login.setBounds(230,150,100,30);
 cancel.setBounds(390,150,100,30);
 sendMail.setBounds(230,250,130,30);
 inbox.setBounds(390,250,100,30);
 dispose.setBounds(670,420,100,30);
 sendMail.hide();
 inbox.hide();
 dispose.hide();

 c.add(fromLabel);

 c.add(emailIdLabel);
 c.add(emailIdText);

 c.add(pwdLabel);
 c.add(pwdText);

 c.add(login);
 c.add(cancel);
 c.add(sendMail);
 c.add(inbox);
 c.add(dispose);

 }

 public void actionPerformed(ActionEvent ae){

 Object o=ae.getSource();

 if(o==login){
 try{

 //Session emailSession = Session.getDefaultInstance(properties);

 props =System.getProperties();
 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");
 props.put("mail.smtp.user", emailIdText.getText());
 props.put("mail.smtp.password",pwdText.getText());
 props.put("mail.pop3.host", "pop.gmail.com");

 loginSession = Session.getDefaultInstance(props,
 new javax.mail.Authenticator() {
 protected PasswordAuthentication getPasswordAuthentication() {
 return new PasswordAuthentication(emailIdText.getText(),pwdText.getText());
 }
 });

 emailStore = (POP3Store) loginSession.getStore("pop3");
 emailStore.connect(emailIdText.getText(),pwdText.getText());

 Folder emailFolder = emailStore.getFolder("INBOX");
 emailFolder.open(Folder.READ_ONLY);

Message[] messages = emailFolder.getMessages();
 //System.out.println("messages.length : "+messages.length);

 //for (int i = 0; i < messages.length; i++) {
 for (int i = 0; i < 10; i++) {
 Message message = messages[i];
 inboxMessagesString+="==============================\n"+
 "Email #" + (i + 1)+"\n"+
 "Subject: " + message.getSubject()+"\n"+
 "From: " + message.getFrom()[0]+"\n"+
 "Text: " + message.getContent().toString()+"\n";
 /*
 System.out.println("==============================");
 System.out.println("Email #" + (i + 1));
 System.out.println("Subject: " + message.getSubject());
 System.out.println("From: " + message.getFrom()[0]);
 System.out.println("Text: " + message.getContent().toString());
 */
 }

emailFolder.close(false);
 emailStore.close();

 JOptionPane.showMessageDialog(this,"Login Success..");
 fromEmailId=emailIdText.getText();
 fromEmailPwd=pwdText.getText();

 emailIdText.setEditable(false);
 pwdText.setEditable(false);

 login.setEnabled(false);
 cancel.setEnabled(false);

 sendMail.show();
 inbox.show();
 dispose.show();

 }catch(Exception e){
 emailIdText.setText("");
 pwdText.setText("");
 JOptionPane.showMessageDialog(this,"Login Failure.Try again..");
 }

 }
 else if(o==cancel){
 try{
 if(emailStore!=null) emailStore.close();
 }catch(Exception e){}
 this.dispose();
 }

 if(o==sendMail){
 SendEmail se=new SendEmail(loginSession,fromEmailId,fromEmailPwd);
 se.setTitle("Send Email...");
 //se.setSize(width,height);
 se.setSize(900,750);
 se.setVisible(true);
 }

 if(o==inbox){
 InboxMail im=new InboxMail(fromEmailId,fromEmailPwd,inboxMessagesString);
 im.setTitle("Mail Inbox...");
 //im.setSize(width,height);
 im.setSize(900,750);
 im.setVisible(true);
 }
 if(o==dispose){
 this.dispose();
 }
 }

 public static void main(String ar[]){

 EmailLogin se=new EmailLogin();
 se.setTitle("Login Page...");
 //se.setSize(width,height);
 se.setSize(800,500);
 se.setVisible(true);

 }

}

2. SendEmail.java

 


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

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;

class SendEmail extends JFrame implements ActionListener{

 Container c;

 JLabel fromLabel;
 JLabel emailIdLabel;
 JLabel pwdLabel;
 JLabel toLabel;
 JLabel ccLabel;
 JLabel bccLabel;
 JLabel subjectLabel;
 JLabel textLabel;

 JTextField emailIdText;
 JPasswordField pwdText;
 JTextField toText;
 JTextField ccText;
 JTextField bccText;
 JTextField subjectText;

 JTextArea textArea;

 JButton sendButton;
 JButton discardButton;

 Font font;
 Session loginSession;

 String fromEmailId;
 String fromEmailPwd;

 public SendEmail(Session loginSession,String fromEmailId,String fromEmailPwd){

 this.loginSession=loginSession;
 this.fromEmailId=fromEmailId;
 this.fromEmailPwd=fromEmailPwd;

 c=this.getContentPane();
 c.setLayout(null);

 font = new Font("Times New Roman", Font.BOLD, 14);

 fromLabel=new JLabel("From: ");
 emailIdLabel=new JLabel("Email-ID: ");
 pwdLabel=new JLabel("Password: ");
 toLabel=new JLabel("To: ");
 ccLabel=new JLabel("CC: ");
 bccLabel=new JLabel("BCC: ");
 subjectLabel=new JLabel("Subject: ");
 textLabel=new JLabel("Text: ");

 emailIdText=new JTextField(40);
 pwdText=new JPasswordField(40);
 toText=new JTextField(60);
 ccText=new JTextField(60);
 bccText=new JTextField(60);
 subjectText=new JTextField(60);

 textArea=new JTextArea(10,60);

 sendButton=new JButton("Send");
 discardButton=new JButton("Discard");

 sendButton.addActionListener(this);
 discardButton.addActionListener(this);

 //component.setBounds(y,x,width,height);
 fromLabel.setBounds(10,10,200,30);

 emailIdLabel.setBounds(100,10,200,30);
 emailIdText.setBounds(200,10,200,30);
 emailIdText.setFont(font);
 emailIdText.setText(fromEmailId);
 emailIdText.setEditable(false);

 pwdLabel.setBounds(100,50,200,30);
 pwdText.setBounds(200,50,200,30);
 pwdText.setFont(font);
 pwdText.setText(fromEmailPwd);
 pwdText.setEditable(false);

 toLabel.setBounds(10,100,400,30);
 toText.setBounds(100,100,400,30);
 toText.setFont(font);

 ccLabel.setBounds(10,150,400,30);
 ccText.setBounds(100,150,400,30);
 ccText.setText("");
 ccText.setFont(font);

 bccLabel.setBounds(10,200,400,30);
 bccText.setBounds(100,200,400,30);
 bccText.setText("");
 bccText.setFont(font);

 subjectLabel.setBounds(10,250,400,30);
 subjectText.setBounds(100,250,400,30);
 subjectText.setFont(font);

 textLabel.setBounds(10,300,400,30);
 textArea.setBounds(100,300,700,200);
 textArea.setFont(font);

 sendButton.setBounds(100,600,200,30);
 discardButton.setBounds(400,600,200,30);

 c.add(fromLabel);

 c.add(emailIdLabel);
 c.add(emailIdText);

 c.add(pwdLabel);
 c.add(pwdText);

 c.add(toLabel);
 c.add(toText);

 c.add(ccLabel);
 c.add(ccText);

 c.add(bccLabel);
 c.add(bccText);

 c.add(subjectLabel);
 c.add(subjectText);

 c.add(textLabel);
 c.add(textArea);

 c.add(sendButton);
 c.add(discardButton);

 }

 public void actionPerformed(ActionEvent ae){

 Object o=ae.getSource();
 if(o==sendButton){
 /*
 Properties props =System.getProperties();
 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");
 props.put("mail.smtp.user", emailIdText.getText());
 props.put("mail.smtp.password",pwdText.getText());

 //Session session = Session.getDefaultInstance(props,null);

 Session session = Session.getDefaultInstance(props,
 new javax.mail.Authenticator() {
 protected PasswordAuthentication getPasswordAuthentication() {
 return new PasswordAuthentication(emailIdText.getText(),pwdText.getText());
 }
 });
 */
 try {

 Message message = new MimeMessage(loginSession);
 message.setFrom(new InternetAddress(emailIdText.getText()));

 String recipientsString=toText.getText();

 if(!(ccText.getText().equals(""))){
 recipientsString=recipientsString+","+ccText.getText();
 }
 if(!(bccText.getText().equals(""))){
 recipientsString=recipientsString+","+bccText.getText();
 }

 message.setRecipients(Message.RecipientType.TO,
 InternetAddress.parse(recipientsString));

 message.setSubject(subjectText.getText());
 message.setText(textArea.getText());

 Transport transport = loginSession.getTransport("smtp");
 transport.connect("smtp.gmail.com", emailIdText.getText(), pwdText.getText());
 transport.send(message);
 transport.close();

 JOptionPane.showMessageDialog(this,"Message send Success..");

 toText.setText("");
 ccText.setText("");
 bccText.setText("");
 subjectText.setText("");
 textArea.setText("");
 //System.out.println("Done");

 } catch (MessagingException e) {
 JOptionPane.showMessageDialog(this,"Message send Faild. Please check input details and send again..");
 //throw new RuntimeException(e);
 }
 }else if(o==discardButton){
 this.dispose();
 }
 }

}

3. InboxMail.java


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;

import java.util.*;

import com.sun.mail.pop3.POP3Store;

class InboxMail extends JFrame implements ActionListener{

 Container c;

 JLabel inboxLabel;

 JScrollPane scrolltxt;

 JTextArea textArea;

 JButton okButton;

 Font font;
 String fromEmailId="";
 String fromEmailPwd="";

 String inboxMessagesString="";

 public InboxMail(String fromEmailId,String fromEmailPwd,String inboxMessageString){

 this.fromEmailId=fromEmailId;
 this.fromEmailPwd=fromEmailPwd;
 this.inboxMessagesString=inboxMessageString;

 c=this.getContentPane();
 c.setLayout(null);

 font = new Font("Times New Roman", Font.BOLD, 14);

 inboxLabel=new JLabel("Mail Inbox: ");

 textArea=new JTextArea();

 scrolltxt = new JScrollPane(textArea);

 okButton=new JButton("Ok");

 okButton.addActionListener(this);

 inboxLabel.setBounds(10,10,200,30);
 scrolltxt.setBounds(100,50,700,500);
 textArea.setFont(font);
 textArea.setText(inboxMessagesString);

 okButton.setBounds(600,600,200,30);

 c.add(inboxLabel);

 c.add(scrolltxt);

 c.add(okButton);

 }

 public void actionPerformed(ActionEvent ae){
 Object o=ae.getSource();
 if(o==okButton){
 this.dispose();
 }
 }

}

//End of Code…..

 

 

Share and Enjoy:
  • Twitter
  • Google Buzz
  • DZone
  • Digg
  • Reddit
  • Diigo
  • StumbleUpon
  • del.icio.us
  • Yahoo! Buzz
  • Google Bookmarks
  • LinkedIn
  • Mixx
  • email
  • PDF
  • Add to favorites
 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org