Solved

sending mail with attachment

Posted on 2004-08-05
13
1,514 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
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 70 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 100 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
 
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 30 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now