Solved

sending mail with attachment

Posted on 2004-08-05
13
1,522 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 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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
 
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

Industry Leaders: 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!

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

726 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