Friday 5 October 2012

Sending Attachment Mail using Java

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.MessagingException;
import javax.mail.Multipart;
import javax.mail.SendFailedException;
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 SendAttachMail {
    public static void main(String[] args) throws MessagingException {
        SendAttachMail s = new SendAttachMail();
       
    }

    public SendAttachMail() throws MessagingException {

        String host = "smtp.gmail.com";
       /***
        * Here you need to give password
        */
        String Password = "xxxxxxxxxxx";
       
       
        String from = "xxxxxxxxxxxxxxxxxxxxxxx";
        String toAddress = "xxxxxxxxxxxxxxxxxxxxx";
        //Need to give file path
        String filename = "F:/ex/RealUsage/build.xml";
        // Get system properties
        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);

        MimeMessage message = new MimeMessage(session);

        message.setFrom(new InternetAddress(from));

        message.setRecipients(Message.RecipientType.TO, toAddress);

        message.setSubject("JavaMail Attachment");

        BodyPart messageBodyPart = new MimeBodyPart();

        messageBodyPart.setText("Here's the file");

        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 {
            Transport tr = session.getTransport("smtps");
            tr.connect(host, from, Password);
            tr.sendMessage(message, message.getAllRecipients());
            System.out.println("Mail Sent Successfully");
            tr.close();

        } catch (SendFailedException sfe) {

            System.out.println(sfe);
        }
    }

}

39 comments:

  1. this post is much helpful for us. this is really very massive value to all the readers and it will be the only reason for the post to get popular with great authority.
    ROI Services in Chennai

    ReplyDelete
  2. your way of expressing views and ideas are really awesome. thus it is exclusive and very much interesting. i like your encryption and thanks a lot.

    Best Android Training Institute in Chennai

    ReplyDelete
  3. Superb i really enjoyed very much with this article here. Really its a amazing article i had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent

    article.thank you for sharing such a great blog with us. expecting for your updation.
    Hadoop Training in chennai

    ReplyDelete
  4. Great article,i get new ideas because of all given information very excellent and real words to implemented for you.
    Dot Net training in chennai

    ReplyDelete
  5. Someone essentially lend a hand to make severely posts I would state. That is the very first time I frequented your website page and thus far? I surprised with the analysis you made to create this particular submit incredible. Fantastic job!
    Best Interior Designers in Chennai
    Interior Designers in Chennai

    ReplyDelete
  6. I do believe all of the concepts you’ve introduced in your post. They’re very convincing and will definitely work. Nonetheless, the posts are too short for novices. May you please extend them a bit from subsequent time? Thank you for the post.
    AWS Training in Chennai
    Hadoop Training in Chennai
    Digital Marketing Training in Chennai

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Fertility clinics looks to the both males and females for diagnosis of fertility problems. Diagnosis has shown that fertility problems both of human affected and solve diagnosis solutions.

    Fertility Centre in OMR

    ReplyDelete
  9. It's Really A Great Post. Looking For Some More Stuff.



    shriram break free

    ReplyDelete
  10. Get Industrial Automation in robotics training by experienced faculty and Experts from Industry.Contact us +91-99534 89987, +91-97112 87737

    ReplyDelete
  11. Thanks for posting the information and it is useful to everyone
    Best Play and Pre School for kids in Hyderabad,India. To give your kid a best environment and learning it is the right way to join in play and pre school were kids can build there physically, emotionally and mentally skills developed. We provide programs to kids like Play Group, Nursery, Sanjary Junior, Sanjary Senior and Teacher training Program.
    Preschool in hyderabad

    ReplyDelete
  12. Nice explanation of the topic

    Sanjary Academy is the best Piping Design institute in Hyderabad, Telangana. It is the best Piping design Course in India and we have offer professional Engineering Courses like Piping design Course, QA/QC Course, document controller course, Pressure Vessel Design Course, Welding Inspector Course, Quality Management Course and Safety Officer Course.
    Piping Design Course in India­

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. Excellent explanation of the topic and clearly understandable

    Pressure Vessel Design Course is one of the courses offered by Sanjary Academy in Hyderabad. We have offer professional Engineering Course like Piping Design Course,QA / QC Course,document Controller course,pressure Vessel Design Course,Welding Inspector Course, Quality Management Course, #Safety officer course.
    Welding Inspector Course
    Safety officer course
    Quality Management Course
    Quality Management Course in India

    ReplyDelete
  16. The information which you have provided is very good. It is very useful who is looking for selenium online training
    iframes in selenium

    ReplyDelete
  17. It was so nice article.I was really satisfied by seeing this articleOracle Applications Training in Bangalore
    .

    ReplyDelete
  18. I found this informative and interesting blog so i think so its very useful and knowledge able.I would like to thank you for the efforts you have made in writing this article.
    interior decorators in chennai
    Modular kitchen in chennai

    ReplyDelete
  19. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. 
    Office interior designers in chennai
    living room interior designers in chennai
    False ceiling interiors designers in chennai

    ReplyDelete
  20. I simply want to give you a huge thumbs up for the great info you have got here on this post.
    interior designers in chennai

    ReplyDelete
  21. When it comes to providing best assignment service online,
    Unique Submission makes sure to complete assignment antecedently to offer scholars enough time to proof read the given assignment
    before submitting it to their tutors/professors. We never compromise to timely delivery and deadlines given by our customers.


    Assignment Help Services



    ReplyDelete
  22. When it comes to providing best assignment service online,
    Unique Submission makes sure to complete assignment antecedently to offer scholars enough time to proof read the given assignment
    before submitting it to their tutors/professors. We never compromise to timely delivery and deadlines given by our customers.


    Assignment helper

    ReplyDelete
  23. This comment has been removed by the author.

    ReplyDelete