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

Posted: February 16, 2012 in Java Posts, Random Posts
Tags: , , , , , , , , , , ,
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("", "");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465"); // smtp port
            Authenticator auth = new Authenticator() {

                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(""));
            msg.setSubject("Try attachment gmail");
            msg.setRecipient(RecipientType.TO, new InternetAddress(""));
            //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));
            Multipart multipart = new MimeMultipart();
        } 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);

  1. Usenet Download Free says:

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


  2. alfred beilin says:

    if anyones on here today im looking for alan leverton is he still on here
    alf beilin


  3. Byron Suchocki says:

    Sweet web site, super design, rattling clean and use genial.


  4. samsung 1080p hdtv says:

    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!


  5. pink lake australia says:

    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.


  6. says:

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


  7. certified nurse assistant says:

    Very interesting topic, thanks for posting.


  8. infant teething says:

    Some really prize posts on this site, bookmarked .


  9. music licensing services says:

    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!


  10. Rosanna Loewenstein says:

    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!


  11. sanjarica tumacenje snova says:

    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.


  12. hybrid car tax credit says:

    Only wanna state that this is invaluable , Thanks for taking your time to write this.


  13. Scrapebox says:

    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.


  14. Sexchat says:

    I like this post, enjoyed this one appreciate it for posting .


  15. nfl breast cancer steelers says:

    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.


  16. cheap vacations says:

    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 =)


  17. film izle says:

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


  18. keyboard rii says:

    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!


  19. super media store promo code says:

    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


  20. Eugene Krotzer says:

    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!


  21. seo software says:

    Very interesting information!Perfect just what I was searching for!


  22. independent music licensing says:

    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.


  23. handjob porn videos says:

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


  24. four micro onde says:

    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.


  25. accelerated nursing programs says:

    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.


  26. link building services says:

    As soon as I detected this web site I went on reddit to share some of the love with them.


  27. credit card consolidation says:

    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.


  28. Jamey says:

    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.


  29. berkeley astronomy 7b says:

    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.


  30. Financial Planning Process says:

    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.


  31. Ping Backlinks says:

    I reckon something really interesting about your website so I bookmarked .


  32. cheap facebook fans says:

    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!


  33. Kari says:

    I regard something genuinely interesting about your website so I saved to fav.


  34. credit card holder says:

    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.


  35. cuckold says:

    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.


  36. Juice Dieting says:

    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.


  37. Tammi Kiedrowski says:

    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.


  38. porn says:

    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


  39. Monty Sawer says:

    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.


  40. Lynelle Hellweg says:

    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.


  41. Breann Weatherholtz says:

    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


  42. money clip wallet says:

    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.


  43. blackjack games online says:

    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.


  44. Escort girl Paris says:

    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.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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