Advertisement

Udemy WW Udemy WW
Showing posts with label IO. Show all posts
Showing posts with label IO. Show all posts

Friday, 18 January 2013

Java Code to send mail with attachment


import java.io.File;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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;




public class Mail {

  public static void main(String args[]) {
 try{
 String host = "smtp.gmail.com";
   String from = "shivsriv@gmail.com";
   String pass ="Your password";
   Properties props = System.getProperties();
   props.put("mail.smtp.starttls.enable", "true"); // added this line
   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 = "jyotika@gmail.com"; // added this line
   System.out.println("preparing mail.....");
   Session session = Session.getDefaultInstance(props, null);
   MimeMessage message = new MimeMessage(session);

  // Get system properties
  Properties properties = System.getProperties();

  // Setup mail server
  properties.setProperty("mail.smtp.host", host);

  // Get the default Session object.


  // Define message

  message.setFrom(new InternetAddress(from));
   message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
  message.setSubject("Mail With Attachment");

  // Create the message part
  BodyPart messageBodyPart = new MimeBodyPart();

  // Fill the message
  messageBodyPart.setText("Hi this is demo mail with attachment");

  Multipart multipart = new MimeMultipart();
  multipart.addBodyPart(messageBodyPart);

  // Part two is attachment
  messageBodyPart = new MimeBodyPart();
  File filename = new File("F:/cassandra10.pdf");
  DataSource source = new FileDataSource(filename);
  messageBodyPart.setDataHandler(new DataHandler(source));
  messageBodyPart.setFileName("my.pdf");
  multipart.addBodyPart(messageBodyPart);

  // Put parts in message
  message.setContent(multipart);

  // Send the message
  Transport transport = session.getTransport("smtp");
  transport.connect(host, from, pass);
  System.out.println("Mail prepared ....");
  transport.sendMessage(message, message.getAllRecipients());
  transport.close();
   System.out.println("mail Sent ....");
 }catch (Exception e) {
 System.out.println("Mail failed ....");
e.printStackTrace();
}
  }
}

Friday, 9 December 2011

Sending and Receiving DatagramPacket in java("implementation Of UDP Protocol")

Sender
import java.net.*;
import java.io.*;

class Sender
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds=new DatagramSocket();
String s=hello!!! this is data"";
byte b[]=s.getBytes();
DatagramPacket dp=new DatagramPacket(b,b.length,InetAddress.getLocalHost(),8);
ds.send(dp);
}
}


Receiver

import java.net.*;
import java.io.*;

class Receiver
{
public static void main(String[] sd) throws Exception
{
DatagramSocket ds=new DatagramSocket(8);
byte b[]=new byte[1024];
DatagramPacket dp=new DatagramPacket(b,b.length,InetAddress.getLocalHost(),8);
ds.receive(dp);
String r=new String(dp.getData());
System.out.println("hi..........");
System.out.println("DATA"+r.trim());
}
}

Thursday, 1 December 2011

How can I make batch updates using JDBC?

//Batch Update
import java.sql.*;
class BatchUpdate
{
public static void main(String arg[])
{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection c=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","student","student");
Statement s=c.createStatement();
String s1="update Emp set name='Mukta' where salary>2300";
String s2="insert into Emp values(124 ,'Mayank',50000) ";
s.addBatch(s1);
s.addBatch(s2);
s.executeBatch();

}
catch(Exception e)
{
System.out.print(e);

}
}
}

Advertisement

Udemy WW