17 Sept 2012

Sending Email to Multiple Recipients in Java

/* send email in java example, send email in java using gmail, send email in java with attachment, Sending Email to Multiple Recipients in Java with attachment */

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
import javax.mail.SendFailedException;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.*;
import javax.mail.*;
import javax.activation.*;
import javax.mail.Authenticator;
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 java.io.*;

public class SendMail 
{

    public SendMail() throws javax.mail.MessagingException 
    {

        String host = "smtp.gmail.com";
        String Password = "1234567890";      //password
        String from = "abc@gmail.com";       // from address
        String toAddress = "abc@gmail.com";  // to address
        String filename = "D:/abc.txt"; // attaching a file
        String sub="Welcome.....";                   //subject
        String body="Hi............\n\n\nRegards..\n";// body
        // Get system properties
        java.util.Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtps.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance(props, null);
        java.io.File file = new java.io.File(filename);
        long filesize = file.length();
        long filesizeInKB = filesize / 1024;
        double megabytes = (filesizeInKB/ 1024);
        System.out.println("Size of File is: "+ filesizeInKB + " KB");
  System.out.println("Size of File is: "+ megabytes + " MB");
  double size1=20.1;
  if(megabytes>size1) //file size must be below 20MB
  System.out.println("File Size Must Below 20 MB");
  else
   {
        javax.mail.internet.MimeMessage message = new javax.mail.internet.MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, toAddress);
        java.util.Calendar currentDate = java.util.Calendar.getInstance();
        java.text.SimpleDateFormat formatter= new java.text.SimpleDateFormat("yyyy-MMM-dd HH:mm:ss a"); 
        java.lang.String dateToday = formatter.format(currentDate.getTime()).toLowerCase();
        message.setSubject("Mail: "+dateToday+" "+sub);
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText(body);
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
        messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(filename);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(filename);
        multipart.addBodyPart(messageBodyPart);
        message.setContent(multipart);

        try {
            javax.mail.Transport tr = session.getTransport("smtps");
            tr.connect(host, from, Password);
            System.out.println("Connected to Server......");
            System.out.println("Now Sending "+filename);
            tr.sendMessage(message, message.getAllRecipients());
            System.out.println("Mail Sent Successfully........");
            tr.close();

        } catch (javax.mail.SendFailedException sfe) 
        {

            System.out.println(sfe);
        }
   }
    }
 public static void main(String arg[])
 {
  try
  {
  SendMail sm=new SendMail();
  }
  catch(java.lang.Exception ee)
  {
    System.out.println(ee);
  }
 }
}


For ieee projects visit our site here

0 comments:

Post a Comment