Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sending mail with attachment

Posted on 2004-08-05
13
Medium Priority
?
1,527 Views
Last Modified: 2008-03-17
Hi!
Can you help me with how to send using javax.mail (I'm using the spring of environment, but I'm not sure it matters) a mail with attachments.
I also want to have the ability so save the message (and its attached files) as a one byte array.
Thanks!
0
Comment
Question by:rzvika2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 280 total points
ID: 11725627
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11725672
Previous is through socket programming whereas this is web mailing to send attachments

http://www.developer.com/java/other/article.php/618471

Best Regards
Sudhakar
0
 
LVL 35

Accepted Solution

by:
girionis earned 400 total points
ID: 11725695
Have a look here: http://java.sun.com/developer/onlineTraining/JavaMail/

>  (I'm using the spring of environment, but I'm not sure it matters)

No it does not matter.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:girionis
ID: 11725704
> I also want to have the ability so save the message (and its attached files) as a one byte array.

Do you have access to the mail server code?
0
 

Assisted Solution

by:ikerasd
ikerasd earned 120 total points
ID: 11725710
Try with this code to send mail with attachments...

public class Mail{
        public static void main(String args[]) throws Exception{

                try{
                        String host ="your.smtp.server";
                        String from = "from@mail.com" ;
                        String to ="to@mail.com";
                        String subject = "This is the subject!";
                        String message = "Message comes here!";
                        String localfile = "c:\file.pdf";
                        String attachName = "file.pdf";

                        Properties prop =System.getProperties();
                        prop.put("mail.smtp.host",host);
                        Session ses1  = Session.getDefaultInstance(prop,null);
                        MimeMessage msg = new MimeMessage(ses1);
                        msg.setFrom(new InternetAddress(from));
                        msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
                        msg.setSubject(subject);

                        // Create the message part
                        BodyPart messageBodyPart = new MimeBodyPart();

                        // Fill the message
                        messageBodyPart.setText(message);

                        Multipart multipart = new MimeMultipart();
                        multipart.addBodyPart(messageBodyPart);

                        // Part two is attachment
                        messageBodyPart = new MimeBodyPart();
                        DataSource source = new FileDataSource(localfile);
                        messageBodyPart.setDataHandler(new DataHandler(source));
                        messageBodyPart.setFileName(attachName);
                        multipart.addBodyPart(messageBodyPart);

                        msg.setContent(multipart);
System.out.println("...sending mail....");
                        Transport.send(msg);
                }
                catch(javax.mail.MessagingException me){
                        me.printStackTrace();
                }
        }
}
0
 

Author Comment

by:rzvika2
ID: 11725732
No, but you can assume that I create the message and know the file names and path.
I assume I can use serialization, but I don't want to...
0
 
LVL 35

Expert Comment

by:girionis
ID: 11725747
> I also want to have the ability so save the message (and its attached files) as a one byte array.

I might have misunderstood your question, you obviously mean to save the message just before it leaves *your* mail server, don't you? Or do you mean to save the message on the receiving end mail server?
0
 

Author Comment

by:rzvika2
ID: 11725849
I have the MimeMessage object, for example, and I'm adding to it the MimeMultipart.
Now I want to save it to the DB as a byte array (BLOB) for persistency.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11725880
Ah I see, I thought you wanted to save it onto the hard disk. Are you using a prepared statement?
0
 

Author Comment

by:rzvika2
ID: 11725906
the bottom line is that I need it as a byte array, the DB issue is not relevant.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11726002
Yes you do, you need a ByteArrayInputSTream, I just asked for a ps in order to post some sample code. I am assuming you indeed have a ps, then you can do:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
out = new ObjectOutputStream(<your multipart object here>) ;
out.writeObject(object);
out.close();
byte [] bytes = bos.toByteArray();

ps = con.prepareStatement("insert into mytable values ?");
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ps.setBinaryStream(1, bais, bytes.length)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11726071
Thank you for accepting :)

BTW I have some mistakes in my code, it should be:

ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
out = new ObjectOutputStream(bos) ;
out.writeObject(<your multipart object here>);
out.close();
0
 

Author Comment

by:rzvika2
ID: 11726086
You're welcome, I understood the idea... ;-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question