Advertisement

Udemy WW Udemy WW

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

5 comments:

  1. good to sample code

    ReplyDelete
  2. hello!,I really like your writing very a lot! percentage we be in contact more about your post
    on AOL? I require a specialist on this space to unravel my problem.
    Maybe that is you! Looking forward to see you.

    My web site: homepage

    ReplyDelete

Advertisement

Udemy WW