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);
}
}
}
Related articles
- Cut through the Email Clutter with the Android Gmail App (mycricket.com)
- Issue sending email using gmail’s smtp server and id of my website (stackoverflow.com)
- Getting Things Done [GTD] in Gmail with Google Tasks (e1evation.com)
- Using Gmail on a BlackBerry smartphone (blogs.blackberry.com)
- Send Free SMS Via Gmail (answers.com)
- Mysql creating a top 10 (stackoverflow.com)
- Android, services and threading. Why doesn’t the thread start running until other execution ends? (stackoverflow.com)
- Listing digits in an int, without loosing 0 (stackoverflow.com)
- All your infinite loop are belong to asyncore.poll (gavinroy.com)
- passing more than one variable from one page to another by a link (stackoverflow.com)
- Arithmetic Expansion and colon builtin command in Bash (yjl.im)
- Gmail + Thunderbird = spamfree email? (ask.metafilter.com)
- Gmail Power Tips: How To Integrate Your Facebook, Twitter, Yahoo, Friendfeed In Your Gmail (netwidz.com)
- Problems retrieving emails from gmail in Android app (stackoverflow.com)
- Help me organize my Gmail account (ask.metafilter.com)
- Schedule Emails to be Sent Later in Gmail (zenone.org)
- 5 Ways That Gmail’s New Priority Inbox Will Affect You (blogs.constantcontact.com)
- AutoIt And It’s Destructive Potential (ttppodcast.wordpress.com)
Hello to every one, the contents present at this website are truly amazing for people experience, well, keep up the
good work fellows.
LikeLike
if anyones on here today im looking for alan leverton is he still on here
alf beilin
LikeLike
Sweet web site, super design, rattling clean and use genial.
LikeLike
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!
LikeLike
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.
LikeLike
Merely wanna comment on few general things, The website pattern is perfect, the content is rattling great : D.
LikeLike
Very interesting topic, thanks for posting.
LikeLike
Some really prize posts on this site, bookmarked .
LikeLike
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!
LikeLike
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!
LikeLike
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.
LikeLike
Only wanna state that this is invaluable , Thanks for taking your time to write this.
LikeLike
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.
LikeLike
I like this post, enjoyed this one appreciate it for posting .
LikeLike
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.
LikeLike
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 =)
LikeLike
Fantastic submit admin! i bookmarked your internet webpage. i’ll look ahead in the event you can have an e-mail listing including.
LikeLike
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!
LikeLike
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
LikeLike
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!
LikeLike
Very interesting information!Perfect just what I was searching for!
LikeLike
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.
LikeLike
I used to be curious about your upcoming post admin seriously needed this weblog super incredible webpage
LikeLike
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.
LikeLike
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.
LikeLike
As soon as I detected this web site I went on reddit to share some of the love with them.
LikeLike
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.
LikeLike
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.
LikeLike
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.
LikeLike
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.
LikeLike
I reckon something really interesting about your website so I bookmarked .
LikeLike
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!
LikeLike
I regard something genuinely interesting about your website so I saved to fav.
LikeLike
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.
LikeLike
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.
LikeLike
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.
LikeLike
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.
LikeLike
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
LikeLike
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.
LikeLike
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.
LikeLike
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
LikeLike
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.
LikeLike
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.
LikeLike
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.
LikeLike