How do you send email from a Java app using Gmail?Solved

Image representing Gmail as depicted in CrunchBase





This is what I do when i want to send email with attachment, work fine. :)


public class NewClass {

    public static void main(String[] args) {
        try {
            Properties props = System.getProperties();
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465"); // smtp port
            Authenticator auth = new Authenticator() {

                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("username-gmail", "password-gmail");
                }
            };
            Session session = Session.getDefaultInstance(props, auth);
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("username-gmail@gmail.com"));
            msg.setSubject("Try attachment gmail");
            msg.setRecipient(RecipientType.TO, new InternetAddress("username-gmail@gmail.com"));
            //add atleast simple body
            MimeBodyPart body = new MimeBodyPart();
            body.setText("Try attachment");
            //do attachment
            MimeBodyPart attachMent = new MimeBodyPart();
            FileDataSource dataSource = new FileDataSource(new File("file-sent.txt"));
            attachMent.setDataHandler(new DataHandler(dataSource));
            attachMent.setFileName("file-sent.txt");
            attachMent.setDisposition(MimeBodyPart.ATTACHMENT);
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(body);
            multipart.addBodyPart(attachMent);
            msg.setContent(multipart);
            Transport.send(msg);
        } catch (AddressException ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MessagingException ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

44 thoughts on “How do you send email from a Java app using Gmail?Solved

  1. Hello to every one, the contents present at this website are truly amazing for people experience, well, keep up the
    good work fellows.

    Like

  2. This is some of the best work I’ve read on the Internet. Your points are unique, valid and very well thought out and written. I appreciate your hard work and thank you for writing it!

    Like

  3. With thanks a lot in order to be our advisor on that area. We enjoyed your actual article completely and the majority of all enjoyed reading the way you dealt with areas I regarded as being controversial. You’re always really nice towards visitors like me and help me in my everyday living. Thanks.

    Like

  4. Merely wanna comment on few general things, The website pattern is perfect, the content is rattling great : D.

    Like

  5. That is really interesting, You are an excessively skilled blogger. I’ve joined your feed and look ahead to looking for more of your wonderful post. Also, I’ve shared your web site in my social networks!

    Like

  6. Thanks a lot for sharing this with all folks you really recognise what you are talking about! Bookmarked. Please additionally discuss with my website =). We may have a hyperlink exchange arrangement among us!

    Like

  7. I claim that we all visitors actually are undoubtedly lucky to be in a fine community with lots of special people with beneficial solutions. I feel quite definitely grateful to have discovered your entire website and look forward to really a lot more fabulous times reading through here.Thanks yet again for all the details.

    Like

  8. I would like to thank you for the efforts you’ve put in writing this blog. I’m hoping the same high-grade web site post from you in the upcoming also. Actually your creative writing abilities has inspired me to get my own site now. Really the blogging is spreading its wings rapidly. Your write up is a good example of it.

    Like

  9. Hi, i read your blog occasionally and i own a similar one and i was just curious if you get a lot of spam feedback? If so how do you protect against it, any plugin or anything you can recommend? I get so much lately it’s driving me mad so any help is very much appreciated.

    Like

  10. Great work! This is the type of information that are meant to be shared across the internet. Shame on the seek engines for not positioning this post higher! Come on over and consult with my website . Thank you =)

    Like

  11. Fantastic submit admin! i bookmarked your internet webpage. i’ll look ahead in the event you can have an e-mail listing including.

    Like

  12. I’m truly enjoying the design and layout of your website. It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your theme? Fantastic work!

    Like

  13. Sweet blog! I found it while searching on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Thank you

    Like

  14. I’m still learning from you, as I’m making my way to the top as well. I certainly liked reading all that is written on your site.Keep the stories coming. I enjoyed it!

    Like

  15. Just wish to say your article is as astonishing. The clarity on your publish is just nice and that i could think you are knowledgeable on this subject. Well together with your permission let me to clutch your feed to keep up to date with drawing close post. Thanks a million and please carry on the rewarding work.

    Like

  16. I used to be curious about your upcoming post admin seriously needed this weblog super incredible webpage

    Like

  17. Just desire to say your article is as amazing. The clarity to your post is just nice and i can think you’re a professional in this subject. Fine along with your permission let me to snatch your feed to stay updated with imminent post. Thank you a million and please carry on the enjoyable work.

    Like

  18. I’ve learn several good stuff here. Certainly worth bookmarking for revisiting. I surprise how much attempt you set to create one of these wonderful informative web site.

    Like

  19. certainly like your web-site however you have to check the spelling on several of your posts. Several of them are rife with spelling issues and I in finding it very bothersome to tell the truth nevertheless I will surely come again again.

    Like

  20. Nice read, I just passed this onto a colleague who was doing some research on that. And he actually bought me lunch since I found it for him smile So let me rephrase that: Thank you for lunch! “We know what happens to people who stay in the middle of the road. They get run over.” by Ambrose Gwinett Bierce.

    Like

  21. You could certainly see your expertise within the paintings you write. The sector hopes for more passionate writers such as you who aren’t afraid to say how they believe. At all times follow your heart. “The only way most people recognize their limits is by trespassing on them.” by Tom Morris.

    Like

  22. I love this How do you send email from a Java app using Gmail?Solved Zeeshan Akhter website very much, Its a real nice post to read and get info . “You have to lead people gently toward what they already know is right.” by Philip.

    Like

  23. I prefer the dear information you provide in the articles. I’ll bookmark your blog site and look again here frequently. We’re quite certain I’ll learn many new stuff in this article! Have fun for one more!

    Like

  24. You could certainly see your expertise within the work you write. The arena hopes for even more passionate writers such as you who aren’t afraid to mention how they believe. Always follow your heart.

    Like

  25. Great write-up, I’m normal visitor of one’s web site, maintain up the nice operate, and It is going to be a regular visitor for a long time.

    Like

  26. Juicing is an age old art; since humans have eaten fruits and vegetables, we have juiced them. Things have changed since the first orange was juiced, but the benefits of juicing have not, especially when juicing is combined with a fast from all other food and fluid.

    Like

  27. Hmm is anyone else experiencing problems with the images on this blog loading? I’m trying to find out if its a problem on my end or if it’s the blog. Any feed-back would be greatly appreciated.

    Like

  28. Hey…this is a wonderful website buddy!!! i am new here and i found this site very interesting and informative ,, you are a professional blogger…thank you for the post buddy and keep on posting nice stuff like this…and hat off to your work

    Like

  29. Keep up the superb piece of work, I read few articles on this site and I believe that your weblog is rattling interesting and has sets of fantastic info.

    Like

  30. Hello I am so glad I found your webpage, I really found you by mistake, while I was browsing on Digg for something else, Nonetheless I am here now and would just like to say kudos for a marvelous post and a all round exciting blog (I also love the theme/design), I don’t have time to browse it all at the minute but I have book-marked it and also included your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the excellent work.

    Like

  31. Great blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple tweeks would really make my blog shine. Please let me know where you got your theme. With thanks

    Like

  32. Good ñ I should certainly pronounce, impressed with your site. I had no trouble navigating through all tabs as well as related information ended up being truly easy to do to access. I recently found what I hoped for before you know it in the least. Reasonably unusual. Is likely to appreciate it for those who add forums or anything, web site theme . a tones way for your customer to communicate. Excellent task.

    Like

  33. I like this weblog very much, Its a very nice spot to read and obtain info. “Our job is not to straighten each other out, But to help each other up.” by Neva Cole.

    Like

  34. I loved up to you’ll receive carried out right here. The caricature is attractive, your authored subject matter stylish. nonetheless, you command get bought an nervousness over that you want be delivering the following. in poor health unquestionably come further until now again since exactly the similar just about a lot regularly inside case you protect this hike.

    Like

This site uses Akismet to reduce spam. Learn how your comment data is processed.