Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

File attachments using Java Mail API

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can anybody help me to send attachments using Java Mail API. I tried this before (i dont have the code with me thou) The attachment dose go but not in a redable fashion.
Thanks,
Triveni.
 
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Well check if u r setting de content type properly to text/html.. ne way I got a lil code dat needs some mod... ne way I have it fro an imap server.. please mail me bak asap if u r interested to gkasinath@hotmail.com
ne way meanwile ill try n get de code runnin smooth n post it to u..
Regds
Gautham Kasinath
U r not Alone
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also need to know how to attach a file to a MimeMessage. I have no idea how to do it. I'm guessing that you have to make a MimeBodyPart that contains the file and add it to the message, but there doesn't seem to be any method in the API to put a file in a MimeBodyPart.
Any help would be appreciated.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
u can attach a message using MimeBodyPart by doing individual Bodypart as many times as required.
MimeBodyPart mbp1= new MimeBodyPart();
MimeBodyPart mbp2= new MimeBodyPart();
Multipart mp=new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
msg.setContent(mp);

i hope this will help u.
But i have a doubt how do i get my mails from my yahoo inbox to my machine.when i tried to connect the system through pop.mail.yahoo.com and protcol as http,it throws a exception called
"NoSuchProviderException:http"
please help.
u r help will be much appreciated.
Thanks in advance,
sunil.s
 
Josh Johnson
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay. I see how you add MimeBodyParts to a MimeMultiPart, but where does the file you want to attach fit in to the picture? Can someone post a few lines of code that show how you would attach a file called "readme.txt" to a MimeMessage?
Thanks.
 
Josh Johnson
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I found the answer to my question, and I wanted to share with others.
In the code below, msgText1 is the text that appears in the message. readme.txt is the file that is attached.
***************************************************************
MimeMessage msg = new MimeMessage(session);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource("readme.txt");
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
***************************************************
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
asdf asdf asd asdf asdf asdf asd sda sda
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ther,
i wd suggest u to use com.oreilly package for file attachments...its working fine with java mail

Rao
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic