Solved

sending mail with attachment

Posted on 2004-08-05
13
1,519 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 115
github account with ecipse 1 48
object oriented programming comparison 5 72
hibernate jars 4 32
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…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

803 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