?
Solved

sending mail with attachment

Posted on 2004-08-05
13
Medium Priority
?
1,524 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
Technology Partners: 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

801 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