Showing posts with label Chatting application in java. Show all posts
Showing posts with label Chatting application 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/

March 17, 2012

Bi direction java networking chat application source code

Two way Chatting example in java using tcp socket


Java Chat application using TCP java socket programing example.  The program communicates with other through port number 3000 and 2000, developer can change this to what ever they want. avoid using first 1024 reserved ports. The program search the other system by its computer name, so please provide the name correctly. The program is so simple, easy to understand. The interface here used is to create two variable for storing port number.  And explaining the code:  Getting user input as string, converting them to bytes, storing it in a variable then sent through datagram socket as datagram packets through a port number specified. On the other hand the program keep on listening to that port number, if a request comes through that socket , it will receive it as byte convert to string the display the message. JAVA thread is used for listening


System 1

/**
Chatting example in java
javabelazy
*/



import java.io.*;
import java.net.*;
interface pen{
final int cp=3000;
final int sp=2000;
}
public class Cli5 implements pen{
public static void main(String[] args)throws Exception {

byte buf1[]=new byte[1024];
DatagramSocket d=new DatagramSocket();
DataInputStream b=new DataInputStream(System.in);
tchat t=new tchat();
t.start();
System.out.println("Chatting programme");
while(true){
String s=b.readLine();
buf1=s.getBytes();
d.send(new DatagramPacket(buf1,s.length(),InetAddress.getByName("Redmi"),cp));
if(s.equals("end"))
System.exit(0);

}
}
}
class tchat extends Thread implements pen{
tchat() {}
byte buf2[]=new byte[1024];
public void run(){
try{
DatagramSocket dr=new DatagramSocket(sp);
while(true){
DatagramPacket p=new DatagramPacket(buf2,buf2.length);
dr.receive(p);
String s =new String(p.getData(),0,p.getLength());
System.out.println("Server :"+s);
System.out.println("Client :");
if(s.equals("end"))
System.exit(0);
}
}
catch(Exception e)
{}
}


}

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

System 2



How to create a bi directional chat application using java  networking

/**
Chatting example in java
javabelazy
*/



import java.io.*;
import java.net.*;
interface pen{
final int cp=2000;
final int sp=3000;
}
public class Cli4 implements pen{
public static void main(String[] args)throws Exception {

byte buf1[]=new byte[1024];
DatagramSocket d=new DatagramSocket();
DataInputStream b=new DataInputStream(System.in);
tchat t=new tchat();
t.start();
System.out.println("Chatting program");
while(true){
String s=b.readLine();
buf1=s.getBytes();
d.send(new DatagramPacket(buf1,s.length(),InetAddress.getByName("Watsapp"),cp));
if(s.equals("end"))
System.exit(0);

}
}
}
class tchat extends Thread implements pen{
tchat() {}
byte buf2[]=new byte[1024];
public void run(){
try{
DatagramSocket dr=new DatagramSocket(sp);
while(true){
DatagramPacket p=new DatagramPacket(buf2,buf2.length);
dr.receive(p);
String s =new String(p.getData(),0,p.getLength());
System.out.println("client :"+s);

if(s.equals("end"))
System.exit(0);
}
}
catch(Exception e)
{}
}


}
The chat application currently work on your intranetwork

Thanking you....

Facebook comments