Friday, 19 October 2012

Sending mail with attachment in j2ee

//bellow is the example for sending the mail with attachment and a custom message, the attachment i am passing is image


//the bellow method can be called in the doget method of the servlet

public void mail(HttpServletRequest request, String contextpath,HttpServletResponse response) throws MessagingException, IOException{
        //these all are required data depending on mail provider these details will change
         String SMTP_HOST_NAME = "smtp.gmail.com";
         String SMTP_PORT = "465";
         String emailMsgTxt = "Test Message Contents";
         String emailSubjectTxt = "A test from gmail";
         String emailFromAddress = "krao346789@gmail.com";
         String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
         String[] sendTo = {"ishita.agrawal@atech.com"};

        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        boolean debug = true;
        //We are making all our above details into a properties object
        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "true");
        props.put("mail.smtp.port", SMTP_PORT);
        props.put("mail.smtp.socketFactory.port", SMTP_PORT);
        props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.put("mail.smtp.socketFactory.fallback", "false");
        //here we are passing above properties object
        Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator() {
        //pass your userid and pwd here
        protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("krao346789@gmail.com", "979090");
        }
        });
        session.setDebug(debug);
      //making our multi purpose mail extention message
        MimeMessage msg = new MimeMessage(session);
        InternetAddress addressFrom = new InternetAddress("krao346789@gmail.com");
        msg.setFrom(addressFrom);

        InternetAddress[] addressTo = new InternetAddress[sendTo.length];
        for (int i = 0; i < sendTo.length; i++) {
        addressTo[i] = new InternetAddress(sendTo[i]);
        }
       //setting the reciever details
        msg.setRecipients(Message.RecipientType.TO, addressTo);

        // Setting the Subject and Content Type
        msg.setSubject(emailSubjectTxt);
        /*Image part*/

        MimeMultipart multipart = new MimeMultipart("related"); 

        // first part  (the html) 
        BodyPart messageBodyPart = new MimeBodyPart(); 
        String htmlText = "<H1>Hello</H1><img src=\"cid:image\">"; 
        messageBodyPart.setContent(htmlText, "text/html"); 

        // add it 
        multipart.addBodyPart(messageBodyPart); 

        // second part (the image) 
        messageBodyPart = new MimeBodyPart();

        System.out.println(request.getRealPath("/images/sample.jpeg"));
        File contextDir = new File(request.getRealPath("/images/sample.jpeg")); 
      System.out.println("Context path"+contextDir.length());

        DataSource fds = new FileDataSource(contextDir); 
        System.out.println("fds"+fds.toString());
        messageBodyPart.setDataHandler(new DataHandler(fds)); 
        messageBodyPart.setHeader("Content-ID","<image>"); 

        // add it 
        multipart.addBodyPart(messageBodyPart); 

        // put everything together 
        msg.setContent(multipart); 
        Transport.send(msg); //sending the mail
    }

No comments:

Post a Comment

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...